Skip to content

Instantly share code, notes, and snippets.

@steenhansen
Last active June 20, 2022 20:05
Show Gist options
  • Save steenhansen/3e8c320725c6196c9a259661473dec42 to your computer and use it in GitHub Desktop.
Save steenhansen/3e8c320725c6196c9a259661473dec42 to your computer and use it in GitHub Desktop.
// Ramda Code : https://cdnjs.cloudflare.com/ajax/libs/ramda/0.24.1/ramda.min.js
// Run the below examples at https://ramdajs.com/repl/
{
const the_numbers = [1, 2, 3, 4];
const addOne = R.map(R.add(1));
const timesTwo = R.map(R.multiply(2));
const appendToAccum = (acc, val) => R.append(val, acc);
const leftToRight = R.compose(
addOne, // [2,3,4,5]
timesTwo); // [4,6,8,10]
R.transduce(leftToRight, appendToAccum, [], the_numbers);
}
{
const the_numbers = [1, 2, 3, 4];
const isOdd = (x) => x % 2 !== 0;
const addOne = R.map(R.add(1));
const keepOdd = R.filter(isOdd);
const timesTwo = R.map(R.multiply(2));
const takeOne = R.take(1);
const appendToAccum = (acc, val) => R.append(val, acc);
const doTap = tap_mess => R.map(x => {console.log(tap_mess, x); return x;});
const leftToRight = R.compose(
doTap('START'), // [1,2,3,4]
addOne, doTap('ADD ONE'), // [2,3,4,5]
keepOdd, doTap('ODD'), // [3,5]
timesTwo, doTap('TIMES 2'), // [6,10]
takeOne, doTap('END')); // [6]
R.transduce(leftToRight, appendToAccum, [], the_numbers);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment