Function(高阶函数式编程)
Function一个可以进行高阶函数式编程的模块。
chain
def chain[a](fs: Seq[(a) ? a]): (a) ? a
把一些列的方法串起来,挨个执行,每个方法的结果,回作为下一个方法的入参
/**定义两个函数*/
def fun1 = (v:Int) => {
val result = v *
println(result)
result
}
def fun2 = (v:Int) => {
val result = v *
println(result)
result
}
//使用
val funs = Seq(fun1,fun2)
Function.chain[Int](funs)() 运行原理:fun1的结果作为fun2的入参
结果打印
const
def const[T, U](x: T)(y: U): T
这是一个返回常量的方法,直接返回 x 值
假如我们要把一个序列中的元素替换成同一个值,可以使用
List(, , , , ).map(_=>)
我们可以用const来处理
List(, , , , ).map(Function.const())
tupled
def tupled[a1, a2, b](f: (a1, a2) ? b): ((a1, a2)) ? b
将二元函数转换为一个一元函数,参数为Tuple2类型
def tupleFunc = (a:Int,b:Int) => {
a+b
}
/**使用*/
val funs = Function.tupled(tupleFunc)
val v = (,)
println(v.getClass.getName)
println(funs(v))
def tupled[a1, a2, a3, b](f: (a1, a2, a3) ? b): ((a1, a2, a3)) ? b
同 tupled[a1, a2, b] def tupled[a1, a2, a3, a4, b](f: (a1, a2, a3, a4) ? b): ((a1, a2, a3, a4)) ? b
同 tupled[a1, a2, b]
def tupled[a1, a2, a3, a4, a5, b](f: (a1, a2, a3, a4, a5) ? b): ((a1, a2, a3, a4, a5)) ? b
同 tupled[a1, a2, b]
def untupled[a1, a2, b](f: ((a1, a2)) ? b): (a1, a2) ? b
def tupleFunc = (v:Tuple2[Int,Int]) => {
v._1 + v._2
}
//转换
val funs = Function.untupled(tupleFunc)
println(funs(,)) /** 8 */
def untupled[a1, a2, a3, b](f: ((a1, a2, a3)) ? b): (a1, a2, a3) ? b
同 tupled[a1, a2, b] def untupled[a1, a2, a3, a4, b](f: ((a1, a2, a3, a4)) ? b): (a1, a2, a3, a4) ? b
同 tupled[a1, a2, b]
def untupled[a1, a2, a3, a4, a5, b](f: ((a1, a2, a3, a4, a5)) ? b): (a1, a2, a3, a4, a5) ? b
同 tupled[a1, a2, b]
uncurried
def uncurried[a1, a2, b](f: (a1) ? (a2) ? b): (a1, a2) ? b
把一个柯里化函数转换为但 2 个参数的函数
/**声明一个柯里化函数*/
def curriedSum = (x:Int)=>(y:Int) => x + y
//转换
val funs = Function.uncurried(curriedSum)
println(funs(,))
结果:
def uncurried[a1, a2, a3, b](f: (a1) ? (a2) ? (a3) ? b): (a1, a2, a3) ? b
同 uncurried[a1, a2, b] def uncurried[a1, a2, a3, a4, b](f: (a1) ? (a2) ? (a3) ? (a4) ? b): (a1, a2, a3, a4) ? b
同 uncurried[a1, a2, b]
def uncurried[a1, a2, a3, a4, a5, b](f: (a1) ? (a2) ? (a3) ? (a4) ? (a5) ? b): (a1, a2, a3, a4, a5) ? b
道理同 uncurried[a1, a2, b]
unlift
def unlift[T, R](f: (T) ? Option[R]): PartialFunction[T, R]
将 A => Option[B] 类型的函数转换为 PartialFunction[T, R] 类型函数
def optionFun(x : Int) = {
println(x)
if (x > ) Some(x*) else None
}
//使用
val funs = Function.unlift(optionFun)
val chars = Array(,,)
//collect需要PartialFunction类型参数
val newchars = chars.collect(funs)
println(newchars.mkString(","))
/**输出为 10,20,30 */
Function(高阶函数式编程)的更多相关文章
- python的高阶函数式编程
首先 函数式编程≠函数编程,就跟计算机≠计算,因为计算机基于硬件,计算基于算法,所以函数式编程是倾向于算法. 高阶函数定义: 一个函数接受的这个参数,而这个参数也是一个函数,称之为高阶函数 例如: ...
- python高阶函数式编程
from functools import reduce def str2int(s): def fn(x, y): return x * 10 + y def char2num(s): return ...
- guava function and predicate 函数式编程
@Test public void function(){ List<String> list = Lists.newArrayList("1","2&quo ...
- Python---进阶---函数式编程---按照权重排序
一. 权重是100 价格占的权重是40%,销量占的权重是17%,评级站的权重是13%,评论占的权重是30% ---------------------------------------------- ...
- Python---进阶---函数式编程---lambda
一. 利用map()函数,把用户输入的不规范的英文,变成首字母大写,其他小写的规范的名字:比如说["ADMAm", "LISA", "JACK&quo ...
- Python修饰器的函数式编程
Python的修饰器的英文名叫Decorator,当你看到这个英文名的时候,你可能会把其跟Design Pattern里的Decorator搞混了,其实这是完全不同的两个东西.虽然好像,他们要干的事都 ...
- swift之函数式编程(四)
文章内容来自<Functional Programing in Swift>,具体内容请到书中查阅 Map, Filter, Reduce Functions that take func ...
- Scala学习教程笔记三之函数式编程、集合操作、模式匹配、类型参数、隐式转换、Actor、
1:Scala和Java的对比: 1.1:Scala中的函数是Java中完全没有的概念.因为Java是完全面向对象的编程语言,没有任何面向过程编程语言的特性,因此Java中的一等公民是类和对象,而且只 ...
- 从函数式编程到Promise
译者按: 近年来,函数式语言的特性都被其它语言学过去了.JavaScript异步编程中大显神通的Promise,其实源自于函数式编程的Monad! 原文: Functional Computation ...
随机推荐
- 【BZOJ1084】dp
题目很简单 分析蛮无聊的一道题.状态转移十分显然然后就做完了. #include <bits/stdc++.h>#define sc(n) scanf("%d",&am ...
- mongodb 查询指定字段
@AutowiredMongoDatabase database; @Overridepublic List<Grid> getAdditionalGrid(String collecti ...
- super_blocks没有导出
在模块中,通过查询super_blocks列表,来遍历系统中的所有super_block,但是出现与下面类似的错误: http://stackoverflow.com/q/5051111/941650 ...
- 2019 ACM-ICPC 上海网络赛 B. Light bulbs (差分)
题目链接:Light bulbs 比赛链接:The Preliminary Contest for ICPC Asia Shanghai 2019 题意 给定 \(N\) 个灯泡 (编号从 \(0\) ...
- Git 学习第三天(二)
默认情况下, Git合并是采用"fast forward"模式,但这种模式下,如果删除分支,会丢掉分支信息 禁用 fast forward 模式: git merge --no-f ...
- rpm升级时spec文件执行的流程
转自:https://www.cnblogs.com/zafu/p/7423758.html %pre 和 %post 脚本片段分别在软件包安装前和安装后执行.%preun 和 %postun 脚本片 ...
- java-day25
. 标签学习: 1. 文件标签:构成html最基本的标签 * html:html文档的根标签 * head:头标签.用于指定html文档 ...
- python初学者学习工具安装教程&安装步骤详解
一.python安装: 版本:3.6.8 下载地址:https://www.python.org/downloads/ 安装步骤截图: 1.点击python安装包,出现下图所示界面,注意勾选A ...
- hadoop备战:hbase的分布式安装经验
配置HBase时,首先考虑的肯定是Hbase版本号与你所装的hadoop版本号是否匹配.这一点我在之前 的博客中已经说明清楚,hadoop版本号与hbase版本号的匹配度,那是官方提供的.以下的实验就 ...
- 浅析vue响应式原理
图很清晰 当我们把一个普通的 JavaScript 对象传给 Vue 实例的 data 选项,Vue 将遍历此对象所有的属性,并使用 Object.defineProperty 把这些属性全部转为 g ...