compose&andThen

两个函数组装为一个函数,compose和andThen相反

    def f(test: String):String = {
"f(" + test + ")"
}
def g(test: String):String = {
"g(" + test + ")"
}
val composeFunction = f _ compose g _
println("compose result:%s".format(composeFunction("compose")))
val andThenResult= f _ andThen g _
println("andThen result:%s".format(andThenResult("compose")))

执行结果

compose result:f(g(compose))
andThen result:g(f(compose))

PartialFunction

对给定的输入参数类型,偏函数只能接受该类型的某些特定的值。一个定义为(Int) => String 的偏函数可能不能接受所有Int值为输入。

isDefinedAt 是PartialFunction的一个方法,用来确定PartialFunction是否能接受一个给定的参数。

    val one: PartialFunction[Int, String] = { case 1 => "one" }
println(one.isDefinedAt(1))
println(one.isDefinedAt(2))
val two: PartialFunction[Int, String] = { case 2 => "two" }
val three: PartialFunction[Int, String] = { case 3 => "three" }
val wildcard: PartialFunction[Int, String] = { case _ => "something else" }
//PartialFunctions可以使用orElse组成新的函数,
//得到的PartialFunction反映了是否对给定参数进行了定义。
val partial = one orElse two orElse three orElse wildcard
println(partial(1))
println(partial(4))

执行结果:

true
false
one
something else

广告

点击Spark加入群Spark,分享更多Spark相关信息

最新文章

  1. 故障重现(内存篇2),JAVA内存不足导致频繁回收和swap引起的性能问题
  2. Windows快捷键
  3. 微软亚洲实验室一篇超过人类识别率的论文:Delving Deep into Rectifiers: Surpassing Human-Level Performance on ImageNet Classification ImageNet Classification
  4. 转载:移动前端开发之viewport的深入理解
  5. POJ 3207 Ikki's Story IV - Panda's Trick
  6. canvas画时钟
  7. Java,javascript,html,css的关系
  8. Linux Makefile自动生成--config.h
  9. json对象与字符串的相互转换,数组和字符串的转换
  10. MVC中Json的使用:Controller中Json的处理【转】
  11. Python编程从入门到实践笔记——操作列表
  12. CPP笔记_函数返回局部变量
  13. Sitecore8.2 Tracker.Current is not initialized错误
  14. Hibernate 连接不同数据库的方言
  15. java读取配置文件信息
  16. 斐波那契数列-java编程:三种方法实现斐波那契数列
  17. NoSQL: Cassandra, HBase, RocksDB
  18. TFS应用层服务器获取F5用户的真实IP地址(高可用性)
  19. R1(下)—数据挖掘—关联规则理论介绍与R实现
  20. bzoj 3669: [Noi2014]魔法森林(并查集+LCT)

热门文章

  1. 需求:加一个下拉框选择条件改变饼图内外环 饼图:百度echarts提供
  2. Selenium_python自动化第一个测试案例(代码基本规范)
  3. HDFS(0.20.2)运营中急救方案
  4. 20155236 《Java程序设计》实验三(敏捷开发与XP实践)实验报告
  5. 20155323刘威良第一次实验 Java开发环境的熟悉(Linux + IDEA)
  6. week8课上实践
  7. hasOwnProperty()函数
  8. 【SAP BI】BW如何连接SQLSERVER数据库
  9. hexo部署
  10. itchat个人练习 语音与文本图灵测试例程