fix dispatch

This commit is contained in:
Rowan 2025-04-17 18:58:24 -05:00
parent d343e764cd
commit 3093b8d4f8
2 changed files with 15 additions and 14 deletions

View file

@ -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)
}
})
}

View file

@ -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<U, V>} 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<A, B>} 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 = []