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(高阶函数式编程)的更多相关文章

  1. python的高阶函数式编程

    首先   函数式编程≠函数编程,就跟计算机≠计算,因为计算机基于硬件,计算基于算法,所以函数式编程是倾向于算法. 高阶函数定义: 一个函数接受的这个参数,而这个参数也是一个函数,称之为高阶函数 例如: ...

  2. python高阶函数式编程

    from functools import reduce def str2int(s): def fn(x, y): return x * 10 + y def char2num(s): return ...

  3. guava function and predicate 函数式编程

    @Test public void function(){ List<String> list = Lists.newArrayList("1","2&quo ...

  4. Python---进阶---函数式编程---按照权重排序

    一. 权重是100 价格占的权重是40%,销量占的权重是17%,评级站的权重是13%,评论占的权重是30% ---------------------------------------------- ...

  5. Python---进阶---函数式编程---lambda

    一. 利用map()函数,把用户输入的不规范的英文,变成首字母大写,其他小写的规范的名字:比如说["ADMAm", "LISA", "JACK&quo ...

  6. Python修饰器的函数式编程

    Python的修饰器的英文名叫Decorator,当你看到这个英文名的时候,你可能会把其跟Design Pattern里的Decorator搞混了,其实这是完全不同的两个东西.虽然好像,他们要干的事都 ...

  7. swift之函数式编程(四)

    文章内容来自<Functional Programing in Swift>,具体内容请到书中查阅 Map, Filter, Reduce Functions that take func ...

  8. Scala学习教程笔记三之函数式编程、集合操作、模式匹配、类型参数、隐式转换、Actor、

    1:Scala和Java的对比: 1.1:Scala中的函数是Java中完全没有的概念.因为Java是完全面向对象的编程语言,没有任何面向过程编程语言的特性,因此Java中的一等公民是类和对象,而且只 ...

  9. 从函数式编程到Promise

    译者按: 近年来,函数式语言的特性都被其它语言学过去了.JavaScript异步编程中大显神通的Promise,其实源自于函数式编程的Monad! 原文: Functional Computation ...

随机推荐

  1. AcWing 138. 兔子与兔子 hash打卡

    很久很久以前,森林里住着一群兔子. 有一天,兔子们想要研究自己的 DNA 序列. 我们首先选取一个好长好长的 DNA 序列(小兔子是外星生物,DNA 序列可能包含 26 个小写英文字母). 然后我们每 ...

  2. 简单理解Ext.extend

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. jQuery选择器 (详解)

    1. 基础选择器 Basics 名称 说明 举例 #id 根据元素Id选择 $("divId") 选择ID为divId的元素 element 根据元素的名称选择, $(" ...

  4. yum 快速LAMP/LNMP 安装(centos7+mysql5.7+apache+php5.6 (缺点:好多模块没有加载)

    1.安装Apache 安装centos7默认自带(Apache2.4.6)版本 yum -y install httpd 2.开启apache服务 systemctl start httpd.serv ...

  5. 如何查看bug属于前端还是后端

    1.F12下如何查看bug属于前端还是后端?前后端分离的项目,通过ajax向后端请求数据,如果后端返回的数据有问题,那么问题就是候选,如果返回的数据没有问题,但是展示结果异常那么问题一般就出在前端. ...

  6. JAVA学习之跨平台性

    Java语音的特点:跨平台性什么是跨平台性通过Java语音编写的应用程序再不同的系统平台上都可以运行. 原理是什么只要在需要运行Java应用程序的操作系统上.先安装一个Java虚拟机(JVM Java ...

  7. CodeForces-1249C2-Good Numbers (hard version) -巧妙进制/暴力

    The only difference between easy and hard versions is the maximum value of n. You are given a positi ...

  8. sanic连接mongo

    方法一: #没有密码,就是没有用户和用户密码 settings={"MOTOR_URI":"mongodb://127.0.0.1:27017/zzy"} ap ...

  9. zic2xpm - 将 ZIICS 象棋片段 (chess pieces) 转换为 XBoard (XPM/XIM) 片段的工具。

    总览 SYNOPSIS zic2xpm file1 [file2 ...] 描述 zic2xpm 将一个或多个 ZIICS 片段文件转换为 XBoard 可用的格式.如果你给出一个以上的文件名,小心同 ...

  10. lua redis 操作

    https://redis.io/commands/keys 遍历redis里面的所有key ,还能进行模糊匹配, 这样就省去了对key的手动过滤了 比如 keys term_info* ) &quo ...