Skip to content

Instantly share code, notes, and snippets.

View WaldoJeffers's full-sized avatar
🤓
Building a new standard JS library: conductor

WaldoJeffers

🤓
Building a new standard JS library: conductor
View GitHub Profile
@WaldoJeffers
WaldoJeffers / compose.js
Last active January 3, 2024 16:47
JavaScript one-line compose (ES6)
const compose = (...fns) => fns.reduce((f, g) => (...args) => f(g(...args)))
// Usage : compose functions right to left
// compose(minus8, add10, multiply10)(4) === 42
//
// The resulting function can accept as many arguments as the first function does
// compose(add2, multiply)(4, 10) === 42
@WaldoJeffers
WaldoJeffers / curry.js
Last active October 23, 2020 01:37
JavaScript one-line curry (ES6)
const curry = (fn, arity = fn.length) => (...args) => args.length >= arity ? fn(...args) : (...moreArgs) => curry(fn)(...args, ...moreArgs)
// tests
const _add = (a, b, c) => a + b + c
describe('curry', () => {
it('should currify the input function', () => {
const add = curry(_add)
expect(add(1, 2, 3)).toBe(6)
expect(add(1)(2)(3)).toBe(6)
@WaldoJeffers
WaldoJeffers / index.js
Created May 15, 2018 14:08
expectToThrow
import { identity, map, compose, forEach } from 'ramda'
const withCatchAll = fn => (...args) => fn(...args).catch(identity)
const unsafeInsert = witchCatchAll(insert)
await Promise.all(map(compose(unsafeInsert, stripOneMandatoryParam)), MANDATORY_PARAMS).then(forEach(expectToThrow))
const numbers = [3, 1, 4]
const isEven = x => Promise.resolve(x => x % 2 === 0)
const result = await Promise
.all(Object.values(obj).map(isEven))
.then(results => Object.keys(obj).reduce((acc, key, index) => {
if (results[index]){
acc[key] = obj[key]
}
return acc
}, {}) // { books: 10 }
const obj = { books: 10, ruler: 1, pencils: 5 }
const add = (a, b) => a + b
const total = Object.values(obj).reduce(add, 0) // 16
@WaldoJeffers
WaldoJeffers / reduce_object_with_conductor.js
Last active May 26, 2018 18:16
Reduce an object with conductor
import { reduce } from 'conductor'
const obj = { books: 10, ruler: 1, pencils: 5 }
const add = (a, b) => a + b
const total = reduce(add, 0, obj) // 16
const homeworld_stats = { Alderaan: 1, Naboo: 1, Stewjon: 1, Tatooine: 7 }
{
Alderaan: {
brown: 1
},
Naboo: {
red: 1
},
Stewjon: {
'blue-gray': 1,
Tatooine: {