Skip to content

Instantly share code, notes, and snippets.

View krzysztofczernek's full-sized avatar

Krzysztof Czernek krzysztofczernek

View GitHub Profile
// 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
}
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]
['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
const getUserDisplayName = user => `${user.firstName} ${user.lastName}`
const users = [
{firstName: 'Jane', lastName: 'Doe'},
{firstName: 'John', lastName: 'Doe'}
]
users.map(getUserDisplayName) // ["Jane Doe", "John Doe"]
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"]
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"
// 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),
// Traditional way, no composition
const calculate1 = x => (x + 1) * 2
// Composition
const calculate2 = compose(multiplyBy(2), add(1))
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)
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)