日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

reactive programming 1.5 monads

發布時間:2025/3/20 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。