Scala函数式程序设计 week1 For Expressions and Monads
来源:互联网 发布:php二手交易网站源码 编辑:程序博客网 时间:2024/06/07 14:33
For表达式
For表达式的翻译
Scala编译器将for
表达式翻译成map
,faltMap
,filter
的懒变形。
以下函数都可以用for表达式的形式定义
for(x <- e1) yield e2//翻译成e1.map(x=>e2)//-----------------------------------for(x <- e1 if f; s) yield e2//f是一个filters是一个可能为空的生成器或filter序列,翻译成for(x<-e1.withFilter(x=>f);s) yield e2//翻译会在新的表达上继续进行`withFilter`是`filter`的一种变种,不产生中间列表。//-----------------------------------for(x <- e1; y<- e2;s) yield e3//翻译成e1.flatMap(x => for (y <- e2;s) yield e3)
For表达式中模式匹配的翻译
Monads
具有map
和flatMap
的数据结构很常见。
事实上有一个名字描述这种带有一些代数法则的数据结构的类,叫做monada.
monads 和map
map可以在每个monad中定义成flatMap和unit的组合
m map f == m flatMap (x => unit(f(x))) == m flatMap (f andThen unit)
monad法则
要证明一个monad,一个类型必须满足三个条件
- 结合性
m flatMap f flatMap g == m flatMap (x => f(x) flatMap g)
- Left unit
unit(x) flatMap f == f(x)
- Right unit
m flatMap unit == m
阅读全文
0 0
- Scala函数式程序设计 week1 For Expressions and Monads
- Scala函数式程序设计原理 week1 Getting Started + Functions & Evaluation
- 【函数式】Monads模式初探——for解析式
- Scala函数式程序设计原理 week3 Data and Abstraction
- Scala函数式程序设计原理 week4 Types and Pattern Matching
- Scala函数式程序设计 week3 Functions and State
- Expressions and phases for writing
- Monads之仿函数
- Scala函数式程序设计原理 week5 List
- Scala函数式程序设计原理 week6 collections
- Scala函数式程序设计 week2 Lazy Evaluation
- Scala函数式程序设计 week4 Timely Effects
- tips for constexpr and Constant Expressions
- 【函数式】Monads模式初探——Monoids
- 【函数式】Monads模式初探——Functor
- 【函数式】Monads模式初探——Endofunctor
- 【函数式】Monads模式初探——Monad概念
- 【函数式】Monads模式初探——Option Monad
- MYSQL性能分析工具 show profiling
- 如何解决webpack打包后,dist文件过大的问题
- eclipse创建Maven失败
- JqGrid之Checkbox选中不生效-yellowcong
- 名词解释
- Scala函数式程序设计 week1 For Expressions and Monads
- NeatBean8.2去掉特殊符号
- 手机上的照片删除了能恢复吗?教你怎么从小白变高手!
- 地理信息数据格式
- 数字IC设计经典书籍
- java 编译器的部分快捷键
- log4j 打印异常日志到文件中
- 例1.3 演示使用new和delete的例子
- Pyhon 获取文件夹的大小统计