Skip to content

Instantly share code, notes, and snippets.

@inamiy
Last active October 17, 2020 15:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save inamiy/2cc0ac5dd91e8051dd754c0b1f48ad5d to your computer and use it in GitHub Desktop.
Save inamiy/2cc0ac5dd91e8051dd754c0b1f48ad5d to your computer and use it in GitHub Desktop.
flipAll2 :: (t1 -> t2 -> t3) -> t2 -> t1 -> t3
flipAll2 = flip
-- flip 1st & 2nd arg
flipAll3 :: (t1 -> t2 -> t3 -> t4) -> t3 -> t2 -> t1 -> t4
flipAll3 = flipAll2 . (flip .) . flip
-- flip 1st & 2nd arg, 2nd & 3rd arg, goto flipAll2
flipAll4 :: (t1 -> t2 -> t3 -> t4 -> t5) -> t4 -> t3 -> t2 -> t1 -> t5
flipAll4 = flipAll3 . ((flip .) .) . (flip .) . flip
-- flip 1st & 2nd arg, 2nd & 3rd arg, 3rd & 4th arg, goto flipAll3
flipAll5 :: (t1 -> t2 -> t3 -> t4 -> t5 -> t6) -> t5 -> t4 -> t3 -> t2 -> t1 -> t6
flipAll5 = flipAll4 . (((flip .) .) .) . ((flip .) .) . (flip .) . flip
-- well, you know the pattern!
-- Tips: Here are some useful n-ary function mappings
\f g -> (f .) . g :: (b -> c) -> (a1 -> a2 -> b) -> a1 -> a2 -> c
\f g -> ((f .) .) . g :: (b -> c) -> (a1 -> a2 -> a3 -> b) -> a1 -> a2 -> a3 -> c
\f g -> (((f .) .) .) . g :: (b -> c) -> (a1 -> a2 -> a3 -> a4 -> b) -> a1 -> a2 -> a3 -> a4 -> c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment