由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 谁能通俗易懂地讲讲trait和monad的概念?
相关主题
Scala的map和flatmap什么区别?看来跳了Scala的坑是对的
想学FP最好不要从Scala开始scala和monad
Scala,F#或haskell怎么用DI?为什么fp很难火
一个partial specialization的问题STM到底解决了什么问题?
Traited Python object attributes -for java people我来挖坑, 谈谈OOP/FP/SQL和人类思维习惯
我对为什么使用FP的理解 (补)monad抽象程度有点高
大牛给讲讲monad吧?C++中怎么传递std::hex这样的参数啊
这次Scala没有入选有点意外呀子类的assignment operator 怎么访问父类的private member
相关话题的讨论汇总
话题: flatmap话题: monad话题: unit话题: context
进入Programming版参与讨论
1 (共1页)
A*******e
发帖数: 2419
1
始终没搞清楚。
c******o
发帖数: 1277
2
scala trait is similar to ruby mixin, and a little bit more complex than
java
interface
From:
http://www.scala-lang.org/old/node/126
Similar to interfaces in Java, traits are used to define object types by
specifying the signature of the supported methods. Unlike Java, Scala allows
traits to be partially implemented; i.e. it is possible to define default
implementations for some methods. In contrast to classes, traits may not
have constructor parameters.
important usage difference between trait and abstract class:
check here:
http://stackoverflow.com/questions/1991042/what-is-the-advantag
c******o
发帖数: 1277
3
On the other hand, Monad is HARD to explain:
As a formal definition of a structure, Monad is
basically a group of types that can implement
unit/flatMap function that match the signature in
def unit[A](a: => A): F[A]
def flatMap[A,B](ma: F[A])(f: A => F[B]): F[B]
But that is not enough, we also have law that
Monad type need to fulfill so that we know our
implementation of unit/flatMap is correct.
// Monad Law
// left identity: f(a) == flatmap(unit(a), f)
// right identity: a == flatMap(a, x => unit(x))
// associativity: flatMap(a, x => flatMap(f(x), g)) == flatMap(flatMap(a, f)
, g)
As the name suggest, associative law means flatMap
operation obey the associative law similar to plus/multiple
(a+b) + c = a + (b+c)
As the name suggest, identity laws basically means we
have a unit function that server as a identity in monad, like
0 in addition, which similar to x + 0 = x, and 0 + x = x
c******o
发帖数: 1277
4
The 通俗易懂地 explanation is:
Now see all these formal definition, still what is a Monad for us programmer
? and Why we need them?
def unit[A](a: => A): M[A]
def flatMap[A,B](ma: M[A])(f: A => M[B]): M[B]
A bit long explanation here:
Monad seems just like a wrapper, it wraps in a basic type (A here), and put
into a context M, generate
a richer type (M[A] here). unit function does this.
We care about the value of type A in context M, but we hate part of the
context that is troublesome.
The troublesome part in the context M make us lose the composability for
values of type A in
context M (make us not be able to combine functions generate value of type A
). So we wrap in the
value and troublesome part together into context M, and now we can combine
functions that generate
M[A], just as if the troublesome part is gone. That is what flatMap does.
Using unit and flatMap, we regain the composability for values of type A in
context M, which is
kind of what monad brings us, and it specifically useful in pure FP as side
effect are the things prevent
clean combination of functions.
Context | Troublesome Part
List | multiple value
Option | can have empty value
Try | can have error
Either | can be another unintended (error message etc.) value
Stream | multiple value and also not accessible until touched
Future | can have error and also have latency to be availuable
IO | input/output side effect
State | internal states side effect
d****n
发帖数: 1637
5
顺风问一个nodejs里面的linq?好用的,支持好的
l**********n
发帖数: 8443
6
rxjs

【在 d****n 的大作中提到】
: 顺风问一个nodejs里面的linq?好用的,支持好的
d****n
发帖数: 1637
7
thanks

【在 l**********n 的大作中提到】
: rxjs
1 (共1页)
进入Programming版参与讨论
相关主题
子类的assignment operator 怎么访问父类的private memberTraited Python object attributes -for java people
有人能解释一下这段C++代码吗我对为什么使用FP的理解 (补)
谁记得一个讨论c++的网站大牛给讲讲monad吧?
C++一个string的小问题这次Scala没有入选有点意外呀
Scala的map和flatmap什么区别?看来跳了Scala的坑是对的
想学FP最好不要从Scala开始scala和monad
Scala,F#或haskell怎么用DI?为什么fp很难火
一个partial specialization的问题STM到底解决了什么问题?
相关话题的讨论汇总
话题: flatmap话题: monad话题: unit话题: context