• 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")
      }
    }
    
    
  • 相关阅读:
    WUST Online Judge
    WUST Online Judge
    WUST Online Judge
    WUST Online Judge
    写在前面
    一丶Python简介
    七丶Python字典
    六丶Python列表操作
    五丶Python列表丶元组丶字典
    四丶Python运算符
  • 原文地址:https://www.cnblogs.com/72808ljup/p/5488524.html
Copyright © 2020-2023  润新知