Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Definition of Functor #564

Open
Mildred-ui opened this issue Nov 29, 2020 · 3 comments
Open

Definition of Functor #564

Mildred-ui opened this issue Nov 29, 2020 · 3 comments
Labels
🖱️ Java General Questions with regards to Java or its API

Comments

@Mildred-ui
Copy link

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 :)

@Mildred-ui Mildred-ui added the 🖱️ Java General Questions with regards to Java or its API label Nov 29, 2020
@muhammad-khair
Copy link

muhammad-khair commented Nov 30, 2020

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).

  • Left identity is basically if you apply a Monad.of(x).flatMap(f) it should be equivalent to f.apply(x).
  • Right identity is that if you perform a flatMap that returns "itself" Monad.of(x).flatMap(x -> Monad.of(x)), it should give Monad.of(x).
  • Associativity is whether 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.

@fans2619
Copy link

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).

  • Left identity is basically if you apply a Monad.of(x).flatMap(f) it should be equivalent to f.apply(x).
  • Right identity is that if you perform a flatMap that returns "itself" Monad.of(x).flatMap(x -> Monad.of(x)), it should give Monad.of(x).
  • Associativity is whether Monad.of(x).flatMap(f).flatMap(g) is equivalent to Monad.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**))?

@muhammad-khair
Copy link

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).

  • Left identity is basically if you apply a Monad.of(x).flatMap(f) it should be equivalent to f.apply(x).
  • Right identity is that if you perform a flatMap that returns "itself" Monad.of(x).flatMap(x -> Monad.of(x)), it should give Monad.of(x).
  • Associativity is whether Monad.of(x).flatMap(f).flatMap(g) is equivalent to Monad.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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🖱️ Java General Questions with regards to Java or its API
Projects
None yet
Development

No branches or pull requests

3 participants