diff --git a/src/fantasy-land.js b/src/fantasy-land.js index b319825..a4cfaad 100644 --- a/src/fantasy-land.js +++ b/src/fantasy-land.js @@ -1,11 +1,12 @@ import { isArray, isFn } from './type.js' import { last } from './list.js' +import { curryN } from './curry.js' /** * @param {PropertyKey[]} methods * @param {import('./types.js').Fn} f */ export function dispatch(methods, f) { - return function() { + return curryN(f.length, function() { if (arguments.length === 0) { return f() } @@ -24,6 +25,6 @@ export function dispatch(methods, f) { } return f.apply(this, arguments) - } + }) } diff --git a/src/function.js b/src/function.js index dce85c7..80dce70 100644 --- a/src/function.js +++ b/src/function.js @@ -17,11 +17,11 @@ export const id = x => x */ export const pipe = (...fns) => x => fns.reduce((v, f) => f(v), x) -export const concat = curry(dispatch(['fantasy-land/concat', 'concat'], +export const concat = dispatch(['fantasy-land/concat', 'concat'], (b, a) => iconcat(iter(a), iter(b)) -)) +) -export const compose = curry(dispatch(['fantasy-land/compose', 'compose'], +export const compose = dispatch(['fantasy-land/compose', 'compose'], /** * @template T, U, V * @param {Morphism} f @@ -30,7 +30,7 @@ export const compose = curry(dispatch(['fantasy-land/compose', 'compose'], * @returns V */ (f, g, x) => chain(g, chain(f, x)) -)) +) /** * @template T @@ -112,7 +112,7 @@ export const unless = curry( * @typedef {(f: F, a: A) => B} StaticMorphism */ -export const ap = curry(dispatch(['fantasy-land/ap', 'ap'], +export const ap = dispatch(['fantasy-land/ap', 'ap'], /** * @template A, B * @template {Morphism} Ap @@ -128,19 +128,19 @@ export const ap = curry(dispatch(['fantasy-land/ap', 'ap'], ), []) return [...xs] - })) + }) -export const chain = curry(dispatch(['fantasy-land/chain', 'chain'], +export const chain = dispatch(['fantasy-land/chain', 'chain'], (f, a) => f(a) -)) +) -export const map = curry(dispatch(['fantasy-land/map', 'map'], +export const map = dispatch(['fantasy-land/map', 'map'], (f, a) => chain(f, a) -)) +) -export const reduce = curry(dispatch(['fantasy-land/reduce', 'reduce'], +export const reduce = dispatch(['fantasy-land/reduce', 'reduce'], (f, acc, xs) => xs.reduce(f, acc) -)) +) export const repeat = curry((n, x) => { const results = []