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

Better explanation of flip function #260

Closed
a1300 opened this issue Dec 21, 2020 · 5 comments · Fixed by #446
Closed

Better explanation of flip function #260

a1300 opened this issue Dec 21, 2020 · 5 comments · Fixed by #446

Comments

@a1300
Copy link

a1300 commented Dec 21, 2020

As a complete beginner I had a very hard time to understand the example for flip. I didn't saw any practical use in the example.

I found a great example and explanation on stackoverflow which opened my eyes on the use of flip

The licence for the stackoverflow post would be CC BY-SA 3.0 according to stackoverflow/licensing. Is this compatible with the current licence? Any thoughts on including the stackoverflow post?

@milesfrain
Copy link
Member

Excellent feedback. Thanks.

I recall flip being pretty mind-melting for me when learning Elm the first time, but having paid those FP dues, I didn't catch this as being confusing in the PureScript book.

I think the solution is to fix the library docs and perhaps link to the Pursuit entry from the book. I'm not a fan of the existing Pursuit example, since const is pretty weird too. Opened purescript/purescript-prelude#237, which is up for grabs. Let me know if you want to tackle it, otherwise I can make the PR.

Also, great workaround for linking to lines in markdown. Annoying that isaacs/github#297 has been on the wish list for the past 6 years.

@milesfrain
Copy link
Member

I think flip would make more sense to cover in Ch4 when discussing map.
It's also a good compliment to operator sections, which is not covered yet in the book. Not sure if the book should cover this syntax in detail, or if we can just link to the language reference.

Something like this could be a good sequence:


Let's say we want to perform truncating division by 5 on each element of an array of integers.

> arr = range 0 20
> arr
[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]

This first attempt is not quite right:

> map (div 5) arr
[0,5,2,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

We're mixing up the argument order and dividing 5 by each element of the array instead.
We can fix that by indicating that each array element needs to be the first argument to div:

> map (\n -> div n 5) arr
[0,0,0,0,0,1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4]

It's more concise with div's infix operator /:

> map (\n -> n / 5) arr
[0,0,0,0,0,1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4]

But most concise by using _ as an operator section:

> map (_ / 5) arr
[0,0,0,0,0,1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4]

flip is another way to swap the ordering of a function's arguments:

> map (flip div 5) arr
[0,0,0,0,0,1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4]

@danielrlc
Copy link

+1 on Chapter 3 suddenly getting very difficult when flip got introduced (rather arbitrarily?). Suddenly being confronted by this: forall a b c. (a -> b -> c) -> b -> a -> c was tough! Some kind of encouraging comment like: "Don't worry about understanding all of this code right now" might help there? (But only if you really DON'T need to understand it all at that early stage, of course!)

@JordanMartinez
Copy link

Would this orientation help?

flip :: forall firstArg secondArg returnType
   . (firstArg -> secondArg -> returnType)
  -> secondArg
  -> firstArg
  -> returnType
flip functionABToC b a = functionABToC a b  

append "hello" "world" == "helloworld"

flip append "hello" "world" == "worldhello"

-- flip's type signature can be simplified
flip :: forall a b c.
   . (a -> b -> c)
  -> b
  -> a
  -> c
flip f b a = f a b  

@danielrlc
Copy link

danielrlc commented Apr 29, 2021

I like the first two lines or so:

flip :: forall firstArg secondArg returnType
  . (firstArg -> secondArg -> returnType)

... but then I'm getting lost in the rest. For me at least, flip feels too complicated to introduce so early in the book. Maybe something simpler like a sum function that just added two numbers could work better?

As a PureScript beginner coming from JavaScript, I'd like to spend a bit more time getting used to the syntax this early on, as it's pretty different to JavaScript. Getting the hang of the functional nature of PureScript could maybe come a bit later? Just me thinking out loud though... :)

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

Successfully merging a pull request may close this issue.

4 participants