Last active
August 5, 2016 14:56
-
-
Save JakeCoxon/5429d23f7bb15cb01c3cadcd4d9066eb to your computer and use it in GitHub Desktop.
Wrap ramda to work nice with the bind operator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ramda from 'ramda' | |
import Immutable from 'immutable' | |
const applyWildcard = (array, val) => { | |
const wildIndex = ramda.findIndex(x => x === __, array); | |
return wildIndex > -1 ? ramda.update(wildIndex, val, array) : array.concat([val]); | |
} | |
const toThisFunction = (func) => { | |
return _.isFunction(func) ? function(...args) { | |
const newArgs = this === undefined ? args : applyWildcard(args, this); | |
return toThisFunction(func(...newArgs)); | |
} : func | |
} | |
const R = ramda.mapObjIndexed((val, key, obj) => toThisFunction(val), ramda); | |
const { __, merge, map, filter, gt, add, any, tap, assoc, compose, take } = R; | |
const log = tap(console.log.bind(console)); | |
[1,2,3] | |
::map(add(1)) | |
::filter(gt(__, 2)) | |
::log(); // [3, 4] | |
({ name: "Jake", score: 84 }) | |
::merge(__, { score: 100 }) | |
::log() // { name: "Jake", score: 100 } | |
const doSomeStuff = compose( | |
map(add(10)), | |
take(2) | |
) | |
new Immutable.List([1,2,3]) | |
::doSomeStuff() | |
::log(); // List(11, 12) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment