49 lines
1.7 KiB
JavaScript
49 lines
1.7 KiB
JavaScript
import { Result } from './result.js'
|
|
|
|
export function curry(func) {
|
|
return function curried(...args) {
|
|
if (args.length >= func.length) {
|
|
return func.apply(this, args)
|
|
} else {
|
|
return function(...args2) {
|
|
return curried.apply(this, args.concat(args2))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// predicates
|
|
export const and = (...booleans) => booleans.every(identity)
|
|
export const or = (...booleans) => booleans.some(identity)
|
|
export const isOk = r => !is(Result, r) || r.isOk()
|
|
|
|
export const construct = Type => args => new Type(...args)
|
|
|
|
export const of = value => Array.isArray(value) ? value : [value]
|
|
export const head = value => value[0]
|
|
export const tail = value => value.slice(1)
|
|
export const prop = curry((p, v) => v[p])
|
|
export const apply = curry((v, f) => f(v))
|
|
export const reduce = curry((fn, init, v) => v.reduce(fn, init))
|
|
export const map = curry((fn, v) => v.map(fn))
|
|
export const filter = curry((fn, v) => v.filter(fn))
|
|
export const find = curry((fn, v) => v.find(fn))
|
|
export const join = curry((sep, v) => v.join(sep))
|
|
export const rev = v => v.slice().reverse()
|
|
export const pipe = (...f) => v => f.reduce(apply, v)
|
|
export const prepend = curry((value, iter) => [value, ...iter])
|
|
export const append = curry((value, iter) => [...iter, value])
|
|
|
|
export const identity = x => x
|
|
|
|
export const ifElse = curry((p, ft, ff, v) => p(v) ? ft(v) : ff(v))
|
|
export const when = curry((p, f, v) => ifElse(p, f, identity, v))
|
|
export const unless = curry((p, f, v) => ifElse(p, identity, f, v))
|
|
|
|
export const isNil = v => !v
|
|
|
|
export const take = curry((n, value) => value.slice(0, n))
|
|
export const drop = curry((n, value) => value.slice(n))
|
|
|
|
export const is = curry((type, value) => value instanceof type)
|
|
|