You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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?
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.
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]
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]
flip::forallfirstArgsecondArgreturnType
. (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 simplifiedflip::forallabc.
. (a->b->c)
->b->a->c
flip f b a = f a b
Activity
milesfrain commentedon Dec 21, 2020
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
constis 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 commentedon Jan 2, 2021
I think
flipwould make more sense to cover in Ch4 when discussingmap.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.
This first attempt is not quite right:
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:It's more concise with
div's infix operator/:But most concise by using
_as an operator section:flipis another way to swap the ordering of a function's arguments:JordanMartinez commentedon Apr 28, 2021
Would this orientation help?