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

What is an example that is not a functor? #324

Open
FredericChow00 opened this issue Nov 28, 2022 · 3 comments
Open

What is an example that is not a functor? #324

FredericChow00 opened this issue Nov 28, 2022 · 3 comments
Labels
📚 Lecture 10 Queries with regards to Lecture 10

Comments

@FredericChow00
Copy link

a functor is associative where

functor.map(g.compose(h)) ≡ functor.map(h).map(g)

Is anyone able to give me an example where a context is not functor?

Wouldn't every functor the does functor.map(g.compose(h)) always be equal to functor.map(h).map(g)?

For example if g is x -> x * 2
h is x -> x + 2

both left and right side give the same result

@FredericChow00 FredericChow00 added the 📚 Lecture 10 Queries with regards to Lecture 10 label Nov 28, 2022
@RyanTYT
Copy link

RyanTYT commented Nov 28, 2022

I believe if you add something wonky like below as the map function, then it wouldn't be a functor

class functor<...> {
    ...
    map(Function<..., ...> mapper) {
        return mapper.apply(mapper.apply(this.x));
    }
}

@yewey2
Copy link

yewey2 commented Nov 29, 2022

that's true, if it's wonky just check the results!

alternatively as an example, for functors that deal specifically with integers

class Functor<Integer> {
    ...
    map(Function<..., ...> mapper) {
        return mapper.apply(x) + 1;
    }
}

let g = x -> x + 2 and h = x -> x * 2
and functor = new Functor(1)
functor.map(h).map(g) = (1 * 2 + 1) + 2 + 1 = 3 + 3 = 6
functor.map(g.compose(h)) = ((1 * 2) + 2) + 1 = 4 + 1 = 5

@FredericChow00
Copy link
Author

I see! Thanks so much for all the replies, I understand now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📚 Lecture 10 Queries with regards to Lecture 10
Projects
None yet
Development

No branches or pull requests

3 participants