fix dispatch
This commit is contained in:
parent
d343e764cd
commit
3093b8d4f8
2 changed files with 15 additions and 14 deletions
|
@ -1,11 +1,12 @@
|
||||||
import { isArray, isFn } from './type.js'
|
import { isArray, isFn } from './type.js'
|
||||||
import { last } from './list.js'
|
import { last } from './list.js'
|
||||||
|
import { curryN } from './curry.js'
|
||||||
/**
|
/**
|
||||||
* @param {PropertyKey[]} methods
|
* @param {PropertyKey[]} methods
|
||||||
* @param {import('./types.js').Fn} f
|
* @param {import('./types.js').Fn} f
|
||||||
*/
|
*/
|
||||||
export function dispatch(methods, f) {
|
export function dispatch(methods, f) {
|
||||||
return function() {
|
return curryN(f.length, function() {
|
||||||
if (arguments.length === 0) {
|
if (arguments.length === 0) {
|
||||||
return f()
|
return f()
|
||||||
}
|
}
|
||||||
|
@ -24,6 +25,6 @@ export function dispatch(methods, f) {
|
||||||
}
|
}
|
||||||
|
|
||||||
return f.apply(this, arguments)
|
return f.apply(this, arguments)
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,11 +17,11 @@ export const id = x => x
|
||||||
*/
|
*/
|
||||||
export const pipe = (...fns) => x => fns.reduce((v, f) => f(v), 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))
|
(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
|
* @template T, U, V
|
||||||
* @param {Morphism<U, V>} f
|
* @param {Morphism<U, V>} f
|
||||||
|
@ -30,7 +30,7 @@ export const compose = curry(dispatch(['fantasy-land/compose', 'compose'],
|
||||||
* @returns V
|
* @returns V
|
||||||
*/
|
*/
|
||||||
(f, g, x) => chain(g, chain(f, x))
|
(f, g, x) => chain(g, chain(f, x))
|
||||||
))
|
)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @template T
|
* @template T
|
||||||
|
@ -112,7 +112,7 @@ export const unless = curry(
|
||||||
* @typedef {(f: F, a: A) => B} StaticMorphism
|
* @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 A, B
|
||||||
* @template {Morphism<A, B>} Ap
|
* @template {Morphism<A, B>} Ap
|
||||||
|
@ -128,19 +128,19 @@ export const ap = curry(dispatch(['fantasy-land/ap', 'ap'],
|
||||||
), [])
|
), [])
|
||||||
|
|
||||||
return [...xs]
|
return [...xs]
|
||||||
}))
|
})
|
||||||
|
|
||||||
export const chain = curry(dispatch(['fantasy-land/chain', 'chain'],
|
export const chain = dispatch(['fantasy-land/chain', 'chain'],
|
||||||
(f, a) => f(a)
|
(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)
|
(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)
|
(f, acc, xs) => xs.reduce(f, acc)
|
||||||
))
|
)
|
||||||
|
|
||||||
export const repeat = curry((n, x) => {
|
export const repeat = curry((n, x) => {
|
||||||
const results = []
|
const results = []
|
||||||
|
|
Loading…
Add table
Reference in a new issue