A*******e 发帖数: 2419 | | 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
|
|