• ScalaTour 2.函数


    /**
      * 1. case class与模式匹配
      */
    object TestFunction extends App{
      def value(expr:Expr): Int = expr match {
        case FibonaciExpr(0) => 0
        case FibonaciExpr(1) => 1
        case FibonaciExpr(n) => value(SumExpr(FibonaciExpr(n-1),FibonaciExpr(n-2)))
        case SumExpr(a,b) => value(a) + value(b)
        case _ => 0
      }
    
      println(value(FibonaciExpr(3)))
    }
    
    abstract class Expr
    case class FibonaciExpr(n:Int) extends Expr{
      require(n >= 0)   //PreDef中的方法,用于参数校验
    }
    
    case class SumExpr(a:Expr,b:Expr) extends Expr
    
    
    /**
      * 2. scala中函数对代码的简化
      */
    object TestFun2 extends App{
      val list = List(1,2,3,4,5,6)
      println("list包含基数吗:"+list.exists(_ %2 ==1 ))
    
      val fileName = List("warn 2013 msg", "warn 2012 msg","error 2013 msg", "warn 2013 msgs")
      println("cat file | grep 'warn' | grep '2013' | wc : "+fileName.filter(_.contains("warn")).filter(_.contains("2013")))
    }
    
    /**
      * 3. scala的wordcount
      */
    object TestWordCount extends App{
      val file = List("warn 2013 msg", "warn 2012 msg","error 2013 msg", "warn 2013 msg")
      def countInStr(str:String):Int = str.split(" ").count("msg"==_)
      val num = file.map(countInStr).reduceLeft(_ + _)
      println(num)
    }
    
    /**
      * 4. 尾递归 : 函数的最后一步返回函数本身,没有其他的操作
      * foldLeft的尾递归实现
      */
    object TestTailRec extends App{
      val file = List("warn 2013 msg", "warn 2012 msg","msg 2013 msg", "warn 2013 msg")
      def countInStr(str:String):Int = str.split(" ").count("msg"==_)
      println(file.map(countInStr))  //List(1, 1, 2, 1)
      def foldLeft(list:List[Int])(init:Int)(f:(Int,Int) =>Int) :Int = {
        list match {
          case List() => init
          case head::tail => foldLeft(tail)(f(head,init))(f)
        }
      }
      println(foldLeft(file.map(countInStr))(0)(_+_))  // 1+1+2+1 = 5 [科里化]
    }
    
    /**
      * 5. 生成List的for循环
      */
    object TestFor2List extends App{
      val file = List("warn 2013 msg", "warn 2012 msg","msg 2013 msg", "warn 2013 msg")
      def countInStr(str:String):Int = str.split(" ").count("msg"==_)
      val counts = for(str <- file) yield countInStr(str)
      val num = counts.reduceLeft(_+_)
      println(num)
    }
    
    /**
      * 6. Option类型
      */
    object TestOption extends App{
      def getProperty(param:String): Option[String] ={
        val value = System.getProperty(param)
        value match {
          case null => None
          case _ => Some(value)
        }
      }
    
      val osName = getProperty("os.name")
    
      println(osName.getOrElse("none"))      //1. Option类型的getOrElse方法
      osName.foreach(println(_))             // 2. Some类型的值可以作为长度为1的List处理
      osName match {                         //3. Option支持类型匹配
        case Some(value) => println(value)
        case _ => println("null")
      }
    }
    
    
  • 相关阅读:
    UESTC 913 握手 Havel定理+优先队列
    UESTC 912 树上的距离 --LCA+RMQ+树状数组
    UESTC 901 方老师抢银行 --Tarjan求强连通分量
    UESTC 900 方老师炸弹 --Tarjan求割点及删点后连通分量数
    UESTC 899 方老师和农场 --双连通分量的构造
    UESTC 898 方老师和缘分 --二分图匹配+强连通分量
    ACdream OJ 1099 瑶瑶的第K大 --分治+IO优化
    Codeforces Zepto Code Rush 2014 -C
    Floyd判最小环算法模板
    POJ 1364 King --差分约束第一题
  • 原文地址:https://www.cnblogs.com/72808ljup/p/5488524.html
Copyright © 2020-2023  润新知