zoukankan      html  css  js  c++  java
  • scala隐式转换&尾递归

    隐式转换

    //隐式转换调用类中本不存在的方法
    class SwingType{
      def  wantLearned(sw : String) = println("兔子已经学会了"+sw)
    }
    
    object swimming{
      implicit def learningType(s : AminalType) = new SwingType
    }
    
    class AminalType{
    }
    
    import swimming.learningType
    
    object ImplicitDemo {
      def main(args: Array[String]): Unit = {
        //隐式值:
         implicit val p1 = "mobin1"
         //implicit val p2 = "mobin2"
         def person(implicit name : String) = name
         println(person)
         
         //隐式视图    隐式转换为目标类型:把一种类型自动转换到另一种类型
         def foo(msg : String) = println(msg)
         implicit def intToString(x : Int) = x.toString
         foo(3)
         
         val rab = new AminalType
         rab.wantLearned("仰泳")
      }
    }
    
    
    
    
    
    class Swing_Type{
      def  wantLearned(sw : String) = println("兔子已经学会了"+sw)
    }
    
    package swimmingPage{
      object swimming{
        implicit def learningType(s : Aminal_Type) = new Swing_Type  //将转换函数定义在包中
      }
    }
    
    class Aminal_Type
    
    object Aminal_Type extends  App{
      import swimmingPage.swimming._  //使用时显示的导入
      val rabbit = new Aminal_Type
      rabbit.wantLearned("breast stroke")         //蛙泳
    }
    object Stringutils {
      implicit class StringImprovement(val s : String){   //隐式类
          def increment = s.map(x => (x + 1).toChar)
      }
    }
    
    object Main extends App{
      import Stringutils._
      
      println("mobin".increment)
      
      println()
    }

    尾递归

    
    public class RecursiveDemo {
        public static void main(String[] args) {
        	System.out.println(tail_func(5, 1));
    	}
    
        int sum(int limit){
        	int res = 0;
        	for(int i = 0; i < limit; i++){
        		res += i;
        	}
        	return res;
        }
        
        //普通递归
    	static int func(int n) {
    	    if (n <= 1) 
    	    	return 1;
    	    return (n * func(n-1));
    	}
    
    	//尾递归是指递归调用是函数的最后一个语句,而且其结果被直接返回,这是一类特殊的递归调用。
    	static int tail_func(int n, int res) {
    	     if (n <= 1)
    	    	 return res;
    	     return tail_func(n - 1, n * res);
    	}
    /*
     * n(5)      res(1)
     * n(4)      res(5*4)
     * n(3)      res(5*4*3)
     * n(2)      res(5*4*3*2)
     * n(1)      res(5*4*3*2*1)
     */
    }
    import scala.annotation.tailrec
    
    object RecursiveDemo2 {
      def main(args: Array[String]): Unit = {
        println(factorial_(40))
      }
      
      def factorial(n: BigInt): BigInt = {
        if (n <= 1)
          1
        else
          n * factorial(n-1)
      }
      
      def factorial_(n: Int): BigInt = {
         @tailrec
         def loop(acc: BigInt, n: Int): BigInt = {
           println("loop n:" + n + " acc:" + acc);
           if (n == 1)
             acc
           else 
             loop(n * acc, n - 1) 
         }
         loop(1, n)
      }
    }
  • 相关阅读:
    关于游戏分布式或者多服管理的想法
    surfaceView
    ackerman递归
    netbeans环境的建立
    copy-浅及深的复制操作
    使用VMware安装CentOS6.8详细教程
    Python在线资源优先级排序
    Python导入模块,Python import用法
    编码
    Python清屏命令
  • 原文地址:https://www.cnblogs.com/apppointint/p/8885290.html
Copyright © 2011-2022 走看看