https://github.com/sourcevault/flip
flip all arguments for a function
https://github.com/sourcevault/flip
curry ramda
Last synced: 24 days ago
JSON representation
flip all arguments for a function
- Host: GitHub
- URL: https://github.com/sourcevault/flip
- Owner: sourcevault
- Created: 2016-07-08T19:29:49.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2016-07-10T20:29:25.000Z (over 9 years ago)
- Last Synced: 2026-01-18T14:19:19.881Z (27 days ago)
- Topics: curry, ramda
- Language: LiveScript
- Size: 1.95 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readMe.md
Awesome Lists containing this project
README
```haskell
flip :: (a -> b -> c ...... x -> y -> z) -> (z -> y -> x ...... c -> b -> a)
```
###Install
```npm install @partially-applied/flip```
###Usage
```livescript
flip = require '@partially-applied/flip'
f = (a,b,c) -> (a + b)*c # generic functions
opposite = flip f
f 1,2,3 # (1 + 2)*3 = 9
opposite 1,2,3 # (3 + 2)*1 = 5
# curried by default
flip f,1,2,3 # (3 + 2)*1 = 5
only-a = opposite 1 # [Function]
only-a-b = only-a 2 # [Function]
only-a-b 3 # (3 + 2)*1 = 5
```
### Why
1. Ramda [`flip`](http://ramdajs.com/docs/#flip) only flips the first two arguments.
2. flip was written to bring another API change to Ramda. Ramda [`compose`](http://ramdajs.com/docs/#compose) composes a function from right to left.
```livescript
_ = require 'ramda'
f = _.curry (x,y) -> x + y
g = _.curry (x,y) -> x*y
h = _.compose (f 1),(g 2) # h = (x) -> 2*x + 1
h 5 # 11
flip = require '@partially-applied/flip'
j = (flip _.compose) (f 1),(g 2) # j = (x) -> (1 + x)*2
j 5 # 12
```
**why would we need to shift the order of compose ?**
good question, it is to assist in using `livescript` do syntax:
```livescript
_ = require 'ramda'
flip = requre '@partially-applied/flip'
compose = flip _.compose
add = _.curry (x,y) -> x + y
multiply = _.curry (x,y) -> x*y
h = compose do
add 1
multiply 2
add 2
multiply 4
add 3
h # h = (x) -> ((1 + x)*2 + 2)*4 + 3
```
For more information about why use `compose` or `ramda` I suggest reading
[Chapter 5:Coding by Compose](https://github.com/MostlyAdequate/mostly-adequate-guide/blob/master/ch5.md)