reactive programming 1.5 monads
1.
Data structures with map and flatMap seem to be quite common.
In fact there’s a name that describes this class of a data structures
together with some algebraic laws that they should have.
They are called monads.
带有map和flatMap的数据结构非常常见
具有这类数据结构加上一些代数规则的类就叫做monads
2. flatMap和Unit
A monad M is a parametric type M[T] with two operations, flatMap and
unit, that have to satisfy some laws.
trait M[T] { def flatMap[U](f: T => M[U]): M[U] } def unit[T](x: T): M[T]
3.Examples of Monads
List is a monad with unit(x) = List(x)Set is monad with unit(x) = Set(x)Option is a monad with unit(x) = Some(x)Generator is a monad with unit(x) = single(x)
flatMap is an operation on each of these types, whereas unit in Scala is
different for each monad
4. 在monads上用flatMap和Unit定义map
m map f == m flatMap(x => unit(f(x)))
5. monads laws
结合律
m flatMap f flatMap g == m flatMap (x => f(x) flatMap g)
左单位
unit(x) flatMap f == f(x)
右单位
m flatMap unit = m
6.monads与for表达式
结合律使得下面两个语句等价
for (y <- for (x <- m; y <- f(x)) yield y z <- g(y)) yield z== for (x <- m; y <- f(x) z <- g(y)) yield z
右单元使得
for (x <- m) yield x == m
7. Try类型
abstract class Try[+T] case class Success[T](x: T) extends Try[T] case class Failure(ex: Exception) extends Try[Nothing]
An expression composed from ‘Try‘, ‘map‘, ‘flatMap‘ will never
throw a non-fatal exception.
由try map flatMap构成的语句永远不会抛出非致命的异常
这个性质叫做 "bullet prooof"原理
(至今不太理解)
转载于:https://blog.51cto.com/dingbo/1591761
reactive programming 1.5 monads相关推荐
- Principles of Reactive Programming 之Actors are Distributed (1)
week7中的前两节课的标题是"Actors are Distributed",讲了很多Akka Cluster的内容,同时也很难理解. Roland Kuhn并没有讲太多Akka ...
- 未来:spring响应式编程 Hands-On Reactive Programming in Spring 5 ,为啥需要响应式编程
Why Reactive Spring? 为什么 使用响应式 的spring In this chapter, we are going to explain the concept of react ...
- 什么是反应式编程? 这里有你想要了解的反应式编程 (Reactive programming)
理解反应式编程 你曾有过订阅报纸或者杂志的经历吗?互联网的确从传统的出版发行商那儿分得了一杯羹,但是过去订阅报纸真的是我们了解时事的最佳方式.那时,我们每天早上都会收到一份新鲜出炉的报纸,并在早饭时间 ...
- 什么是响应式编程(Reactive Programming)
1.什么是响应式编程(Reactive Programming) Wikipedia上这样说: In computing, reactive programming is an asynchronou ...
- Reactive Programming with RxJava,介绍一本书和Rx
Reactive Programming with RxJava,介绍一本书和Rx 标签(空格分隔): book RxJava Who Should Read This Book Reactive P ...
- 未来:spring响应式编程 Hands-On Reactive Programming in Spring 5(二)------Basic Concepts
看完这篇文章你会有很大收获! 好学近乎知,力行近乎仁,知耻而后勇. The previous chapter explained why it is important to build reacti ...
- 响应式编程(Reactive Programming)是什么?
简介 在计算中,响应式编程(Reactive Programming)是一种面向数据流和变化传播的声明式编程范式. 这意味着可以在编程语言中很方便地表达静态或动态的数据流,而相关的计算模型会自动将变化 ...
- 设计模式--谈谈Reactive Programming 响应式编程
1. Reactive Programming 响应式编程 1.1. 什么是什么是响应式编程 维基百科的解释如下: In computing, reactive programming is an a ...
- reactive programming
In computing, reactive programming is a declarative programming paradigm concerned with data streams ...
最新文章
- 【斩获7枚offer,入职阿里平台事业部】横扫阿里、美团、京东、 去哪儿之后,我写下了这篇面经!...
- 获取手机当前显示的ViewController
- Java多线程详解(二)
- HTML中a标签/超链接标签的下划线怎么去掉
- gEdit: 打造简洁,小巧的编程环境
- 一台PoE工业交换机可以给多少设备供电?
- java空格键_Java KeyPressed-如果其他键也太旧,则无法检测是否按下了空格键
- 针对数据科学家和数据工程师的4条SQL技巧
- pdm vault 使用_如何使用Key Vault连接器更好地保护Logic Apps中的秘密
- Retrofit的使用教程(二)
- vb还是python强大-VB已死,Python当立 | 忆云竹
- python编程语言-python编程语言基础知识总结
- mysql tar.gz_记centos 安装 mysql5.7.23.tar.gz教程
- CSS3学习案例1——超级链接类型标示图标
- Android关于Paint你所知道的和不知道的一切
- 【python】微信朋友圈分析
- heic照片格式怎么转换jpg?heic转换成jpg方法分享
- GPT-3、Stable Diffusion一起助攻,让模型听懂甲方修图需求
- 文献翻译--《Siamese Regression Tracking With Reinforced Template Updating》--增强模板更新的孪生回归跟踪,IEEE-2021
- 侦测卡 变色龙侦测卡 chameleon-Mini(迷你变色龙) (二)