Skip to content

Instantly share code, notes, and snippets.

@luislee818
Last active October 3, 2022 23:39
Show Gist options
  • Save luislee818/a02aebbccb744ed38303adcc9860d390 to your computer and use it in GitHub Desktop.
Save luislee818/a02aebbccb744ed38303adcc9860d390 to your computer and use it in GitHub Desktop.
Ramda samples
// using v0.24.1
const R = require('ramda');
// adjust
// [a -> a] -> Number -> [a] -> [a]
const arr = [1, 2, 3];
let result = R.adjust(R.add(1), 1, arr);
console.log('adjust: ', result); // [1, 3, 3]
// update
// Number -> a -> [a] -> [a]
result = R.update(1, 9, arr);
console.log('update: ', result); // [1, 9, 3]
// apply
// (*... -> a) -> [*] -> a
result = R.apply(Math.max, arr);
console.log('apply: ', result); // 3
// unapply
// ([*] -> a) -> (*... -> a)
result = R.unapply(R.join(','))(...arr);
console.log('unapply: ', result); // 1,2,3
// applySpec
// { k: ((a, b, ...) -> v) } -> ((a, b, ...) -> { k: v })
const transform = R.applySpec({
foo: R.add,
bar: { baz: R.multiply }
});
result = transform(1, 2);
console.log('applySpec: ', result); // { foo: 3, bar: { baz: 2 } }
// chain
// when arg[1] is list, (a -> b) -> [a] -> [b]
result = R.chain(x => [[x], x * 10])(arr);
console.log('chain(1): ', result); // [[1], 10, [2], 20, [3], 30]
// when arg[1] is function, ((b, a) -> c) -> (a -> b) -> (a -> c)
result = R.chain(R.concat, R.toUpper)('ramda');
console.log('chain(2): ', result); // RAMDAramda
const foos = [{ foo: 1 }, { foo: 2 }, { foo: 3 }];
result = R.map(R.chain(R.merge, x => ({ bar: x.foo + 1 })))(foos);
console.log('chain(2): ', result); // [{ foo: 1, bar: 2 }, { foo: 2, bar: 3 }, { foo: 3, bar: 4 }]
// ap
// when arg[0] and arg[1] are both functions, (a -> b -> c) -> (a -> b) -> a -> c
// it's like the args in opposite order or chain (when arg[1] is function)
result = R.ap(R.concat, R.toUpper)('ramda');
console.log('ap(1): ', result); // ramdaRAMDA
// when arg[0] is a list of function, and arg[1] is mappable
// it's a reduce of the functions on the list, map the function on the list and concat
result = R.ap([R.add(1), R.multiply(10)], [1, 2]);
console.log('ap(2): ', result); // [2, 3, 10, 20]
result = R.ap([R.toUpper, R.concat('z')], 'abc');
console.log('ap(2): ', result); // ['A', 'B', 'C', 'za', 'zb', 'zc']
// converge
// (x1 -> x2 -> ... -> z) -> [(a -> b -> ... -> x1), (a -> b -> ... -> x2), ...] -> (a -> b -> ... -> z)
result = R.converge(Math.max, [R.prop('foo'), R.prop('bar'), R.prop('baz')])({
foo: 1,
bar: 2,
baz: 3
});
console.log('converge: ', result); // 3
// equivalent with lift when final arg is just one
result = R.liftN(3, Math.max)(R.prop('foo'), R.prop('bar'), R.prop('baz'))({
foo: 1,
bar: 2,
baz: 3
});
console.log('liftN equivalent: ', result); // 3
// lift
// when the 1+ args are functions
// ((x1, x2, ...) -> y) -> ((a, b, ..., m) -> x1, (a, b, ..., m) -> x2, ...) -> (a, b, ..., m) -> y
const sum = (a, b, c) => a + b + c;
result = R.lift(sum)(R.add(1), R.add(2), R.add(3))(1);
console.log('lift(1): ', result); // 9
// when the 1+ args are arrays
// ((a, b, ...) -> y) -> ([a], [b], ...) -> [y]
result = R.lift(sum)([100, 200], [20], [3]);
console.log('lift(2): ', result); // [123, 223]
// liftN
result = R.liftN(3, R.unapply(R.sum))(R.add(1), R.add(2), R.add(3))(1);
console.log('liftN(1): ', result); // 9
// useWith: your friend for being point-free
// (x1 -> x2 -> ... -> z) -> [(a -> x1), (b -> x2), ...] -> (a -> b -> ... -> z)
result = R.useWith(Math.max, [R.prop('foo'), R.prop('bar'), R.prop('baz')])
({ foo: 1 })({ bar: 2 })({ baz: 3 });
console.log('useWith: ', result); // 3
// juxt
// [(a -> b -> ... -> z)] -> (a -> b -> ... -> [z])
result = R.juxt([Math.max, Math.min, R.unapply(R.nth(1))])(...arr);
console.log('juxt: ', result); // [3, 1, 2]
// nth
// Number -> [a] -> a
result = R.nth(2, arr);
console.log('nth: ', result); // 3
// nthArg
// Number -> *... -> *
R.nthArg(2)(...arr);
console.log('nthArg: ', result); // 3
// of
// a -> [a]
result = R.of(123);
console.log('of: ', result); // [123]
// objOf
// String -> v -> { String: v }
result = R.compose(R.objOf('foo'), R.map(R.objOf('bar')))(arr);
console.log('objOf: ', result); // { foo: [{ bar: 1 }, { bar: 2 }, { bar: 3 }] }
// partition
// (a -> Boolean) -> [a] -> [[a], [a]]
result = R.partition(R.gt(2))(arr);
console.log('partition: ', result); // [[1], [2, 3]]
// props
// [k] -> { k: v } -> [v]
result = R.compose(R.join(' '), R.props(['first', 'last']))({ first: 'John', last: 'Smith' });
console.log('props: ', result);
result = R.converge(R.unapply(R.join(' ')), [R.prop('first'), R.prop('last')])({ first: 'John', last: 'Smith' });
console.log('props equivalent: ', result);
// reduced
result = R.reduce((acc, x) => x === 2 ? R.reduced('999') : acc + x, 0)(arr);
console.log('reduced: ', result); // 999
// times
// (Number -> a) -> Number -> [a]
result = R.times(R.multiply(3), 5);
console.log('times: ', result); // [0, 3, 6, 9, 12]
// zip
// [a] -> [b] -> [[a, b]]
result = R.zip([1, 2, 3, 4], ['a', 'b', 'c']);
console.log('zip: ', result); // [[1, 'a'], [2, 'b'], [3, 'c']]
// transpose
// [[a]] -> [[a]]
result = R.transpose([[1, 2, 3, 4], ['a', 'b', 'c'], ['x', 'y', 'z']]);
console.log('transpose: ', result); // [[1, 'a', 'x'], [2, 'b', 'y'], [3, 'c', 'z'], [4]]
// zipObj
// [k] -> [v] -> [{ k: v }]
result = R.zipObj(['a', 'b', 'c'], [1, 2, 3]);
console.log('zipObj: ', result); // { a: 1, b: 2, c: 3 }
// zipWith
// (a, b -> c) -> [a] -> [b] -> [c]
result = R.zipWith(R.pair, [1, 2, 3], ['a', 'b', 'c']);
console.log('zipWith: ', result); // [[1, 'a'], [2, 'b'], [3, 'c']]
// unfold
// (a -> [(b, a)]) -> a -> [b]
result = R.unfold(R.ifElse(R.lt(50), R.F, x => [-x, x + 10]))(10);
console.log('unfold: ', result); // [-10, -20, -30, -40, -50]
// where
// { k: (v -> Boolean) } -> { k: v } -> Boolean
const pred = R.where({
foo: R.equals(123),
bar: R.has('baz'),
baz: R.contains(999)
});
result = pred({ foo: 123, bar: { baz: 'baz' }, baz: [123, 999] });
console.log('where: ', result); // true
// intersperse
// a -> [a] -> [a]
result = R.intersperse(0, arr);
console.log('intersperse: ', result); // [1, 0, 2, 0, 3]
// project
// [k] -> [{ k: v }] -> [{ k: v }]
const p1 = { name: 'John', gender: 'M', age: 18 };
const p2 = { name: 'Jane', gender: 'F', age: 20 };
result = R.project(['name', 'age'], [p1, p2]);
console.log('project: ', result); // [{ name: 'John', age: 18 }, { name: 'Jane', age: 20 }]
result = R.map(R.pick(['name', 'age']))([p1, p2]);
console.log('project equivalent: ', result); // [{ name: 'John', age: 18 }, { name: 'Jane', age: 20 }]
// similar to how R.project is implemented, useWith!
const myProject = R.useWith(R.map, [R.pick, R.identity]);
result = myProject(['name', 'age'], [p1, p2]);
console.log('project equivalent 2: ', result); // [{ name: 'John', age: 18 }, { name: 'Jane', age: 20 }]
// transduce
const objs = [p1, p2];
// note the function order is reversed in compose/pipe when using as transducer
const transducer = R.compose(R.filter(R.propEq('gender', 'F')), R.map(R.prop('name')));
result = R.transduce(transducer, R.flip(R.append), [], objs);
console.log('transduce: ', result); // ['Jane']
result = R.into([], transducer, objs);
console.log('into: ', result); // ['Jane']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment