Skip to content

Instantly share code, notes, and snippets.

@escherlies
Last active December 14, 2022 14:55
Show Gist options
  • Save escherlies/de92514ecdc295f0a098d41609d8677a to your computer and use it in GitHub Desktop.
Save escherlies/de92514ecdc295f0a098d41609d8677a to your computer and use it in GitHub Desktop.
{-| Helper function to partially apply an "infix" a function. Same as flip but is more comprehensable for some cases, imho.
Unfortunatly you can not to partial function application with infix functions, let a lone use any function as infix.
So this is not possible in elm, which is unfortunate because the Haskell way is much more tacit.
-- Haskell, not possible in elm
List.map (- 10) [ 10, 20, 30 ] --> [0,10,20]
-- Purecript, not possible in elm
List.map (_ - 10) [ 10, 20, 30 ] --> [0,10,20]
-- Substract 10. Reads as if the `-` is actually partially applied the infixed way like in the haskell example.
-- It also losely resembleds purescripts anonymous function arguments
List.map (i_ (-) 10) [ 10, 20, 30 ]
--> [0,10,20] : List number
-- Does the same as before, but is harder to comprehend
List.map (flip (-) 10) [ 10, 20, 30 ]
--> [0,10,20] : List number
-- Without "correction", accidently substract x from 10.
List.map ((-) 10) [ 10, 20, 30 ]
--> [0,-10,-20] : List number
-- Substract 10, without any helpers. Very noisy.
List.map (\x -> x - 10) [ 10, 20, 30 ]
--> [0,-10,-20] : List number
-}
i_ : (c -> b -> a) -> b -> c -> a
i_ =
flip
flip : (c -> b -> a) -> b -> c -> a
flip a b c =
a c b
{-| Examples used in the description
-}
examples : List (List number)
examples =
-- Substract 10. Reads as if the `-` is actually partially applied the infixed way like in the haskell example.
-- It also losely resembleds purescripts anonymous function arguments
[ List.map (i_ (-) 10) [ 10, 20, 30 ] --> [0,10,20] : List number
-- Does the same as before, but is harder to comprehend
, List.map (flip (-) 10) [ 10, 20, 30 ] --> [0,10,20] : List number
-- Substract 10, without any helpers. Very noisy.
, List.map (\x -> x - 10) [ 10, 20, 30 ] --> [0,-10,-20] : List number
-- Without "correction", accidently substract x from 10.
, List.map ((-) 10) [ 10, 20, 30 ] --> [0,-10,-20] : List number
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment