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

PYP 2021 q4c #585

Open
muhdjabir opened this issue Apr 26, 2022 · 9 comments
Open

PYP 2021 q4c #585

muhdjabir opened this issue Apr 26, 2022 · 9 comments

Comments

@muhdjabir
Copy link

muhdjabir commented Apr 26, 2022

Source

AY 20/21 q4c
image
image

Description

Hi everyone Im having issues with justifying question 4c. I know there 2 laws/criteria in being a functor. But the s.toUpperCase() throws me off abit. Does that affect it being a functor? and If not, is there anything that proves that it is not a functor

Screenshots (if any):

Insert Images here if necessary

@Zhenyubbx
Copy link

Are you able to show the question for part c?

@pohja1999
Copy link

pohja1999 commented Apr 26, 2022

You can try putting these tests into your shell and see if both results yield true; if yes, then its a functor, else no.
Identity: Box.of("TestString").equals(Box.of("TestString").map(x -> x ));
** x -> x is the identity func;
Assoc: Box.of(f.apply("TestString")).equals(Box.of("Some other String").map(f));

@muhdjabir
Copy link
Author

@Zhenyubbx edited to include part c!

@calvinseptyanto
Copy link

I checked with my TA, I think the Box class doesn't seem to obey both Functor requirements

The easiest counter example is from the identity law

Box.of("cs2030").map(x -> x).equals(Box.of("cs2030"));

The code above yields false since map function will return new Box object, therefore it is not a functor

@muhdjabir
Copy link
Author

@calvinseptyanto Thanks alot calvin! That clears up a bit for me

@pohja1999
Copy link

pohja1999 commented Apr 27, 2022

mmmhmmmm yea I still think it is a functor. we need to override the default equality method by objects to check for equality if not it'll just be checking for the address of the objects.... maybe someone more knowledgeable can clarify on this. :-( I cant think of a way for Optional map(Function<T,S> ) to return the same object as another Optional without overriding the equals method.
image

image

@weiming21
Copy link

weiming21 commented Apr 27, 2022

Hi I think I can offer an alternative counterexample that might be more specific to this Box implementation as it has to do with the toUpperCase part

Function<String, String> f1= x -> "a"
Function<String, String> f2= x -> x == "a"?"1":"2"
Box.of("a").map(f2.compose(f1)).equals(Box.of("a").map(f1).map(f2))
//returns false -> hence does not obey functor associative law

CMIIW thanks!

@pohja1999
Copy link

Hi I think I can offer an alternative counterexample that might be more specific to this Box implementation as it has to do with the toUpperCase part

Box.of("a").map(f2.compose(f1)).equals(Box.of("a").map(f1).map(f2))
//returns false -> does not obey functor associative law

CMIIW thanks!

Sick hehe, may i know what are your f1 and f2 functions hehe

@sinteary
Copy link

Hi I think I can offer an alternative counterexample that might be more specific to this Box implementation as it has to do with the toUpperCase part

Box.of("a").map(f2.compose(f1)).equals(Box.of("a").map(f1).map(f2))
//returns false -> does not obey functor associative law

CMIIW thanks!

Sick hehe, may i know what are your f1 and f2 functions hehe

f1 and f2 are shown in the lines above the one you quoted. But I believe you can also test with any dummy functions that could modify the string, eg. substring, string concatenation, toUpper/LowerCase, just as a proof of concept.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants