scala函数式变程,curry,读取文件,字符串插值,隐式转换,模式匹配

scala函数式变程

object AdFunctionApp {
  def main(args: Array[String]): Unit = {
    val l = List(1,2,3,4)

    列表里面的元素*2
    for(ele <- l){
      println(ele*2)
    }

    对列表中每一个元素都做一个操作
    println(l.map((x:Int) => x*2))
    println(l.map(x => x*2))
    println(l.map(_ * 2))

    println换行,print不换行
    l.map(_*2).foreach(x => println(x))
    l.foreach( x => println(x*2))

    筛选咯
    println(l.map(_*2).filter(_ >8))

    取前3个
    println(l.take(3))

    求和
    println(l.reduce(_+_))
    1-2-3-4
    println(l.reduce(_-_))

    用下面代码试试是什么意思
    println(l.reduceLeft(_-_))
    println(l.reduceRight(_-_))

    1,2
    -1,3
    -4,4
    l.reduceLeft((x,y) => {
      println(x+","+y)
      x-y
    })


    3,4
    2,-1
    1,3
    l.reduceRight((x,y) => {
      println(x+","+y)
      x-y
    })

    curry
    2相当于初始值,最后加上后面的结果
    println(l.fold(2)(_+_))
    相减看下面代码,结果为0
    println(l.fold(10)(_-_))

    10,1
    9,2
    7,3
    4,4
    l.fold(10)((x,y) =>{
      println(x+","+y)
      x-y
    })

    里面要有判断条件
    println(l.count(_>1))


    扁平化操作
    val f=List(List(1,2),List(3,4))
    List(List(1, 2), List(3, 4))
    println(f)
    List(1, 2, 3, 4)
    println(f.flatten)
    List(2, 4, 6, 8)
    println(f.flatMap(_.map(_*2)))


  }

  def sum(a:Int,b:Int)=a+b
  curry
  def add(a:Int)(b:Int)=a+b
}

scala读取文件

//注意导这个包
import scala.io.Source

object FileApp {
  def main(args: Array[String]): Unit = {
    //读取文件
    val file = Source.fromFile("f:/asd.txt")

    for(line <- file.getLines()){
      println(line)
    }
  }
}

字符串插值

object StringApp {
  def main(args: Array[String]): Unit = {
    val name  ="asd"
    println("hello:"+name)
    //字符串插值
    println(s"hello:$name")

    //""""""好多输出
    val b =
      s"""
        |huanying asd
        |fuck you
        |$name
      """.stripMargin

    println(b)
  }
}

scala隐式转换

import scala.io.Source
//导入java的File类
import java.io.File

import ImplicitAspect._

object ImplicitApp {
  def main(args: Array[String]): Unit = {
    //隐式转换,传进来个普通对象,赋予他高级对象的功能
//    implicit def man2superman(man: Man):Superman=new Superman(man.name)
//    val man =new Man("xxx")
//    //如果原函数有fly方法,优先使用原函数的
//    man.fly()

//    implicit def file2RedFile(file:File):RichFile=new RichFile(file)
    val file = new File("f:/asd.txt")
    val content =file.read()
    println(content)

  }
}

class Man(var name:String){
//  def fly()={
//    println("asd")
//  }
}

class Superman(var name: String){
  def fly()={
    println("niubi")
  }
}

class RichFile(val file:File){
  def read() =Source.fromFile(file.getPath).mkString
}

//额外增加,将ord隐式转换为Ording[T],T是泛型
//implicit ord: Ordering[T]

上面那段程序可以把隐式转换单独包装,然后程序里导入这个包

import java.io.File

object ImplicitAspect {
  implicit def man2superman(man:Man):Superman = new Superman(man.name)
  implicit def file2RichFile(file:File):RichFile = new RichFile(file)
}

模式匹配,偏函数,try catch

import scala.util.Random



object MatchApp {
  def main(args: Array[String]): Unit = {

    val teachers = Array("qwe","asd","zxc")
    //随机选一个
    val teacher=teachers(Random.nextInt(teachers.length))

//    for(i <- 1.to(4)){
//      //随机选一个
//      val teacher=teachers(Random.nextInt(teachers.length))
//      println(teacher)
//    }

    //模式匹配
//    teacher match {
//      case "qwe"=> println("ewq")
//      case "asd"=> println("dsa")
//      case "zxc"=> println("cxz")
//    }

    //偏函数
    def saysomething(name:String)= name match {
      case "qwe"=> println("ewq")
      case "asd"=> println("dsa")
      case "zxc"=> println("cxz")
    }

    saysomething("qwe")
//进去是个字符串类型,出来也是字符串类型
    //模式匹配和偏函数,区别,被包在花括号没有match的一组case语句是偏函数
    def saysomething2:PartialFunction[String,String]={
      case "qwe"=> "ewq"
      case "asd"=> "dsa"
      case "zxc"=> "cxz"
    }

    println(saysomething2("asd"))



    def greeting(array: Array[String])={
      array match {
        case Array("zhangsan") => println("hi:zhangsan")
        case Array(x,y)=> println(s"hi:$x,$y")
        case Array("zhangsan",_*) => println("hi asd")
        case _=>println("welcome..")

      }
    }

    greeting(Array("zhangsan","asd"))

    try{
          val i=1/0
          println(i)
        }catch {
      //Ar毕竟是Exception的子类,但是如果写的更前,报Ar
          case e:Exception => println("aaaa")
          case e:ArithmeticException =>println("cuola")
        }finally {
          println("这个finall是一定要执行的")
        }

  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值