Skip to content

Better explanation of flip function #260

Closed

Description

@a1300

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?

Activity

  1. milesfrain commented on Dec 21, 2020

    @milesfrain
    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.

  2. milesfrain commented on Jan 2, 2021

    @milesfrain
    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]
    
  3. JordanMartinez commented on Apr 28, 2021

    @JordanMartinez

    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  
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions