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
// Example from https://github.com/loop-recur/FPJS-Class/blob/master/part1_exercises/exercises/compose/compose_exercises.js | |
// We can use ramda to avoid writing all helpers ourselves | |
const R = require('ramda') | |
const fastestCar = cars => { | |
const sorted = R.sortBy(car => car.horsepower, cars) | |
const fastest = R.last(sorted) | |
return fastest.name | |
} |
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
const users = [ | |
{firstName: 'Jane', lastName: 'Doe'}, | |
{firstName: 'John', lastName: 'Doe'} | |
] | |
users.map(user => user.firstName) // ["Jane", "John"] | |
// Let's make this point-free. First: | |
const prop = propName => obj => obj[propName] |
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
['1', '12', '123'].map(num => parseInt(num)) // [1, 12, 123] | |
// This works nicely, however making it point-free is not as simple as: | |
['1', '12', '123'].map(parseInt) // [1, NaN, 1] | |
// This breaks because `map` passes more than just one argument to `parseInt` – it passes `currentValue`, `index`, and `array` | |
// `parseInt` then accepts the second argument as `radix` | |
// More information: | |
// * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map |
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
const getUserDisplayName = user => `${user.firstName} ${user.lastName}` | |
const users = [ | |
{firstName: 'Jane', lastName: 'Doe'}, | |
{firstName: 'John', lastName: 'Doe'} | |
] | |
users.map(getUserDisplayName) // ["Jane Doe", "John Doe"] |
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
const getUserDisplayName = user => `${user.firstName} ${user.lastName}` | |
const users = [ | |
{firstName: 'Jane', lastName: 'Doe'}, | |
{firstName: 'John', lastName: 'Doe'} | |
] | |
users.map(user => getUserDisplayName(user)) // ["Jane Doe", "John Doe"] |
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
const map = fn => collection => collection.map(fn) | |
// Alternatively, import map from ramda: | |
// import { map } from 'ramda' | |
const doSmth = number => { | |
return String(number * 2) | |
} | |
console.log(doSmth(10)) // "20" |
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
// Traditional way, no composition | |
const activeProjectsDetails1 = projects => { | |
const active = projects.filter(project => project.isActive) | |
const detailsOnly = projects.map(project => pickProjectDetails(project)) | |
return orderBy(detailsOnly, ['updatedAt']) | |
} | |
// Composition | |
const activeProjectsDetails2 = projects => pipe( | |
filter(isActive), |
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
// Traditional way, no composition | |
const calculate1 = x => (x + 1) * 2 | |
// Composition | |
const calculate2 = compose(multiplyBy(2), add(1)) |
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
const double = x => x * 2 | |
const getLength = string => string.length | |
const map = fn => collection => collection.map(fn) | |
const messages = ['Hello', 'Bye'] | |
const pipe = (...fns) => input => fns.reduce((mem, fn) => fn(mem), input) | |
const doubleLengths = pipe(map(getLength), map(double))(messages) |
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
const double = x => x * 2 | |
const getLength = string => string.length | |
const map = (collection, fn) => collection.map(fn) | |
const messages = ['Hello', 'Bye'] | |
const mapToLengths = collection => map(collection, getLength) | |
const mapToDoubles = collection => map(collection, double) | |
const pipe = (...fns) => input => fns.reduce((mem, fn) => fn(mem), input) |