Skip to content

Instantly share code, notes, and snippets.

@vadim-nekrasov
vadim-nekrasov / ramda.js
Last active July 31, 2017 21:25
Pass multiple arguments to functions composition
const fn = (a, b, c) => a + b + c;
const composeMulti = (...funcs) => (...args) =>
reduce((as, currFn) =>
update(0, apply(currFn, as), as),
args, funcs)
composeMulti(fn, toUpper, fn)('a', 'b', 'c', 'd', 'e') //=> ["ABCbc", "b", "c", "d", "e"]
@vadim-nekrasov
vadim-nekrasov / partial.js
Created July 29, 2017 19:16
currying for unary function via "partial"
const fn = compose(partial(inc), of);
const futureCall = fn(5);
futureCall() //=> 6
@vadim-nekrasov
vadim-nekrasov / pluck.js
Created July 29, 2017 18:11
work of pluck with function
pluck('a', () => ({a: 'some', b: 'other'}))() //=> "some"
@vadim-nekrasov
vadim-nekrasov / o.js
Last active July 28, 2017 17:03
Method o
// Replace all values of array with one
const replacer = o(map, always);
replacer(3)([1,2,3]); //=> [3, 3, 3]
@vadim-nekrasov
vadim-nekrasov / modifying_linked_fields.js
Last active July 27, 2017 17:09
modifying an object's field, basing on values of it's other fields.
const payload = {state: false, dataLength: 2};
// Assigning "true" to the "state" field if "dataLength" > 1
chain(assoc('state'), propSatisfies(lt(1), 'dataLength'))(payload)
//=> {"dataLength": 2, "state": true}
converge(assoc('state'), [propSatisfies(lt(1), 'dataLength'), identity])(payload)
//=> {"dataLength": 2, "state": true}
const getTimeFromStart = startTime => Date.now() - startTime;
const makeFuture = startTime => ms => Future((reject, resolve) => {
console.log('start: ' + ms, 'timeFromStart: ' + getTimeFromStart(startTime));
return setTimeout(() => {
console.log('finish: ' + ms, 'timeFromStart: ' + getTimeFromStart(startTime));
resolve(ms);
}, ms)
});
const futureOfMs = makeFuture(Date.now());
compose(map(toString), map(inc), Maybe.of)(5) //=> Maybe.Just("6")
Maybe.of(5).map(compose(toString, inc)) //=> Maybe.Just("6")
//////////////////////////////////////////////////////////////////
map(concat, identity)('a')('b') //=> "ab"
map(concat('b'), identity)('a') //=> "ba"
map(concat, concat)('a')('b')('c') //=> "abc"
into({}, map(a => ({['key' + a]: a})) , [1, 5, 12]); //=> {"key1": 1, "key12": 12, "key5": 5}
into({}, map(a => ['key' + a, a]) , [1, 5, 12]); //=> {"key1": 1, "key12": 12, "key5": 5}
@vadim-nekrasov
vadim-nekrasov / composeK.js
Last active July 26, 2017 16:18
composeK
composeK(s => Maybe.Just(s + 1), s => Maybe.Just(s + 1), s => Maybe.Just(s))(5) //=> Maybe.Just(7)
//////////////////////////////////////////////////////////////////////////////
composeK(concat, concat, concat, sym => () => sym)('a')('b') //=> "abbb"
composeK(concat, concat, concat, s => Maybe.Just(s))('a')('b') //=> "abbb"
//////////////////////////////////////////////////////////////////////////////
either(Maybe.Just(false), Maybe.Just(55)) // => Maybe.Just(55)
either([false, false, 'a'], [11]) // => [11, 11, "a"]