Created
November 18, 2018 22:10
-
-
Save sangheestyle/c9c2acdc633a9ca1d0b9b6322d2e4a17 to your computer and use it in GitHub Desktop.
Practice Ramda.js
This file contains hidden or 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
// Practice Ramda | |
import * as R from 'ramda' | |
const guys: Array<string> = ["Tom", "Jim"] | |
const ladies: Array<string> = ["Marry", "Jane"] | |
// any | |
R.any(x => x == "Tom")(guys); // true | |
R.any(x => x == "Tony")(guys); // false | |
// all | |
R.all(x => x == "Tom")(guys); // false | |
R.all(x => x != "Tony")(guys); // true | |
// append | |
R.append(3, [1, 2]); // [1, 2, 3] | |
// concat | |
R.concat('ABC', 'DEF'); // 'ABCDEF' | |
R.concat([1, 2, 3], [4, 5, 6]); // [1, 2, 3, 4, 5, 6] | |
R.concat([], []); // [] | |
// difference | |
R.difference([1, 2], [3, 2]); // [1] | |
// drop | |
R.drop(2, ['foo', 'bar', 'baz']); // [ 'baz' ] | |
// dropLast | |
R.dropLast(2, ['foo', 'bar', 'baz']); // [ 'foo' ] | |
// dropWhile | |
R.dropWhile(x => x <= 2, [1, 2, 3, 4, 3, 2, 1]); // [3, 4, 3, 2, 1] | |
// dropRepeats | |
R.dropRepeats([1, 1, 1, 2, 3, 4, 4, 2, 2]); //=> [1, 2, 3, 4, 2] | |
// endsWith | |
R.endsWith('c', 'abc') // true | |
R.endsWith(['c'], ['a', 'b', 'c']) // true | |
// equals | |
R.equals([1, 2, 3], [1, 2, 3]); // true | |
// flatten | |
R.flatten([1, 2, [3, 4], 5, [6, [7, 8, [9, [10, 11], 12]]]]); | |
// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] | |
// fromPairs | |
R.fromPairs([['a', 1], ['b', 2], ['c', 3]]); // {a: 1, b: 2, c: 3} | |
// groupBy | |
const byGrade = R.groupBy(function(student) { | |
const score = student.score; | |
return score < 65 ? 'F' : | |
score < 70 ? 'D' : | |
score < 80 ? 'C' : | |
score < 90 ? 'B' : 'A'; | |
}); | |
const students = [{name: 'Abby', score: 84}, | |
{name: 'Eddy', score: 58}, | |
// ... | |
{name: 'Jack', score: 69}]; | |
byGrade(students); | |
// { | |
// 'A': [{name: 'Dianne', score: 99}], | |
// 'B': [{name: 'Abby', score: 84}] | |
// // ..., | |
// 'F': [{name: 'Eddy', score: 58}] | |
// } | |
// has | |
const hasName = R.has('name'); | |
hasName({name: 'alice'}); // true | |
// hasPath | |
R.hasPath(['a', 'b'], {a: {b: 2}}); // true | |
// includes | |
R.includes({ name: 'Fred' }, [{ name: 'Fred' }]); // true | |
R.includes([42], [[42]]); // true | |
// insert | |
R.insert(2, 'x', [1,2,3,4]); // [1,2,'x',3,4] | |
// intersection | |
R.intersection([1,2,3,4], [7,6,5,4,3]); // [4, 3] | |
// isEmpty | |
R.isEmpty([1, 2, 3]); //=> false | |
R.isEmpty([]); //=> true | |
R.isEmpty(''); //=> true | |
R.isEmpty(null); //=> false | |
R.isEmpty({}); //=> true | |
R.isEmpty({length: 0}); //=> false | |
// isNil | |
R.isNil(null); //=> true | |
R.isNil(undefined); //=> true | |
R.isNil(0); //=> false | |
R.isNil([]); //=> false | |
// join | |
const spacer = R.join(' '); | |
spacer(['a', 2, 3.4]); // 'a 2 3.4' | |
R.join('|', [1, 2, 3]); // '1|2|3' | |
// last | |
R.last(['fi', 'fo', 'fum']); // 'fum' | |
// max | |
R.max(789, 123); // 789 | |
R.max('a', 'b'); // 'b' | |
// mean | |
R.mean([2, 7, 9]); // 6 | |
R.mean([]); // NaN | |
// median | |
R.median([2, 9, 7]); // 7 | |
R.median([7, 2, 10, 9]); // 8 | |
R.median([]); // NaN | |
// mergeDeepLeft | |
R.mergeDeepLeft({ name: 'fred', age: 10, contact: { email: '[email protected]' }}, | |
{ age: 40, contact: { email: '[email protected]' }}); | |
// { name: 'fred', age: 10, contact: { email: '[email protected]' }} | |
// mergeDeepRight | |
R.mergeDeepRight({ name: 'fred', age: 10, contact: { email: '[email protected]' }}, | |
{ age: 40, contact: { email: '[email protected]' }}); | |
//=> { name: 'fred', age: 40, contact: { email: '[email protected]' }} | |
// mergeDeepWith | |
R.mergeDeepWith(R.concat, | |
{ a: true, c: { values: [10, 20] }}, | |
{ b: true, c: { values: [15, 35] }}); | |
// { a: true, b: true, c: { values: [10, 20, 15, 35] }} | |
// min | |
R.min(789, 123); // 123 | |
R.min('a', 'b'); // 'a' | |
// negate | |
R.negate(42); // -42 | |
// not | |
R.not(true); // false | |
R.not(false); // true | |
R.not(0); // true | |
R.not(1); // false | |
// path | |
R.path(['a', 'b'], {a: {b: 2}}); // 2 | |
R.path(['a', 'b'], {c: {b: 2}}); // undefined | |
// pick | |
R.pick(['a', 'd'], {a: 1, b: 2, c: 3, d: 4}); // {a: 1, d: 4} | |
R.pick(['a', 'e', 'f'], {a: 1, b: 2, c: 3, d: 4}); // {a: 1} | |
// pipe | |
const f = R.pipe(Math.pow, R.negate, R.inc); | |
f(3, 4); // -(3^4) + 1 | |
// product | |
R.product([2,4,6,8,100,1]); // 38400 | |
// range | |
R.range(1, 5); // [1, 2, 3, 4] | |
R.range(50, 53); // [50, 51, 52] | |
// reduce | |
R.reduce(R.subtract, 0, [1, 2, 3, 4]) // ((((0 - 1) - 2) - 3) - 4) = -10 | |
// repeat | |
R.repeat('hi', 5); // ['hi', 'hi', 'hi', 'hi', 'hi'] | |
// reverse | |
R.reverse([1, 2, 3]); // [3, 2, 1] | |
R.reverse('abc'); // 'cba' | |
// sort | |
const diff = function(a, b) { return a - b; }; | |
R.sort(diff, [4,2,7,5]); // [2, 4, 5, 7] | |
// split | |
R.split('.', 'a.b.c.xyz.d'); // ['a', 'b', 'c', 'xyz', 'd'] | |
// startsWith | |
R.startsWith('a', 'abc') // true | |
R.startsWith(['a'], ['a', 'b', 'c']) // true | |
// take | |
R.take(2, ['foo', 'bar', 'baz']); // ['foo', 'bar'] | |
R.take(4, ['foo', 'bar', 'baz']); // ['foo', 'bar', 'baz'] | |
R.take(3, 'ramda'); // 'ram' | |
const personnel = [ | |
'Dave Brubeck', | |
'Paul Desmond', | |
'Eugene Wright', | |
'Joe Morello', | |
'Gerry Mulligan', | |
'Bob Bates', | |
'Joe Dodge', | |
'Ron Crotty' | |
]; | |
const takeFive = R.take(5); | |
takeFive(personnel); | |
// ['Dave Brubeck', 'Paul Desmond', 'Eugene Wright', 'Joe Morello', 'Gerry Mulligan'] | |
// takeWhile | |
const isNotFour = x => x !== 4; | |
R.takeWhile(isNotFour, [1, 2, 3, 4, 3, 2, 1]); // [1, 2, 3] | |
R.takeWhile(x => x !== 'd' , 'Ramda'); // 'Ram' | |
// union | |
R.union([1, 2, 3], [2, 3, 4]); // [1, 2, 3, 4] | |
// uniq | |
R.uniq([1, 1, 2, 1]); // [1, 2] | |
// zip | |
for (let pair of R.zip(guys, ladies)) { | |
console.log(pair); // ['Tom', 'Marry'] ... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment