Open Mildred-ui opened 3 years ago
For the functors, you're right that a functor must follow the 2 laws (identity and associativity), but I'm not so sure about the of()
static method. From what I see with all the functors we have, ArrayList<T>
, Stream<T>
, ArrayList<T>
, they all have the of()
method. So I presume that it's required as it needs to "box" the datatype.
As for the monads, you need to see whether it has the .of()
method once again, as well as the 3 rules on (left identity, right identity, associativity).
Monad.of(x).flatMap(f)
it should be equivalent to f.apply(x)
. Monad.of(x).flatMap(x -> Monad.of(x))
, it should give Monad.of(x)
. Monad.of(x).flatMap(f).flatMap(g)
is equivalent to Monad.of(x).flatMap(x -> f.apply(x).flatMap(g))
.You can see in this post (#539) helps explain how to evaluate. Not all monads need to be functors; you need to see what methods they have and whether they follow the rules. Edit: Post #518 also helps to explain this! Edit 2: Fixed the associativity law.
For the functors, you're right that a functor must follow the 2 laws (identity and associativity), but I'm not so sure about the
of()
static method. From what I see with all the functors we have,ArrayList<T>
,Stream<T>
,ArrayList<T>
, they all have theof()
method. So I presume that it's required as it needs to "box" the datatype.As for the monads, you need to see whether it has the
.of()
method once again, as well as the 3 rules on (left identity, right identity, associativity).
- Left identity is basically if you apply a
Monad.of(x).flatMap(f)
it should be equivalent tof.apply(x)
.- Right identity is that if you perform a flatMap that returns "itself"
Monad.of(x).flatMap(x -> Monad.of(x))
, it should giveMonad.of(x)
.- Associativity is whether
Monad.of(x).flatMap(f).flatMap(g)
is equivalent toMonad.of(x).flatMap(x -> g.apply(x).flatMap(f))
.You can see in this post (#539) helps explain how to evaluate. Not all monads need to be functors; you need to see what methods they have and whether they follow the rules. Edit: Post #518 also helps to explain this!
I think the equivalence of associativity should be monad.of(x).flatMap(x -> **f**.apply(x).flatMap(**g**))
?
For the functors, you're right that a functor must follow the 2 laws (identity and associativity), but I'm not so sure about the
of()
static method. From what I see with all the functors we have,ArrayList<T>
,Stream<T>
,ArrayList<T>
, they all have theof()
method. So I presume that it's required as it needs to "box" the datatype. As for the monads, you need to see whether it has the.of()
method once again, as well as the 3 rules on (left identity, right identity, associativity).
- Left identity is basically if you apply a
Monad.of(x).flatMap(f)
it should be equivalent tof.apply(x)
.- Right identity is that if you perform a flatMap that returns "itself"
Monad.of(x).flatMap(x -> Monad.of(x))
, it should giveMonad.of(x)
.- Associativity is whether
Monad.of(x).flatMap(f).flatMap(g)
is equivalent toMonad.of(x).flatMap(x -> g.apply(x).flatMap(f))
.You can see in this post (#539) helps explain how to evaluate. Not all monads need to be functors; you need to see what methods they have and whether they follow the rules. Edit: Post #518 also helps to explain this!
I think the equivalence of associativity should be
monad.of(x).flatMap(x -> **f**.apply(x).flatMap(**g**))
?
Omg yes my bad! Thanks for checking! Shall edit my post
Hi all! I'm confused about how to decide whether something is a functor. So far my understanding is that a functor should have a constructor, a map() mathod, and follows the functor laws stated in lectures. However, I'm not sure about the constructor part, like is "new A(sth)" accepted? Or it has to has construction methods like "of(sth)" or "make(sth)". Also, is it correct to just take monad as "functors with flatmap() mathod"? Thx :)