added applyTo (name stolen from ramda)

This commit is contained in:
Rowan 2025-04-17 20:04:34 -05:00
parent c545be05ea
commit bcc2d003bf

View file

@ -1,6 +1,6 @@
import { curry, curryN } from './curry.js'
import { dispatch } from './fantasy-land.js'
import { concat as iconcat, isIterable, iter, tail } from './list.js'
import { head, concat as iconcat, isIterable, iter, tail } from './list.js'
/** @import { Fn, Morphism, InferredMorphism, Predicate } from './types.js' */
@ -11,17 +11,6 @@ import { concat as iconcat, isIterable, iter, tail } from './list.js'
*/
export const id = x => x
export const tap = curry((f, x) => {
f(x)
return x
})
/**
* @param {...Fn} fns
* @returns {(x: any) => any}
*/
export const pipe = (...fns) => (...args) => tail(fns).reduce((v, f) => f(v), head(fns)(...args))
export const concat = dispatch(['fantasy-land/concat', 'concat'],
(b, a) => {
if (isIterable(a) || isIterable(b)) {
@ -121,7 +110,6 @@ export const unless = curry(
* @template {Morphism<A, B>} [F=Morphism<A, B>]
* @typedef {(f: F, a: A) => B} StaticMorphism
*/
export const ap = dispatch(['fantasy-land/ap', 'ap'],
/**
* @template A, B
@ -140,6 +128,8 @@ export const ap = dispatch(['fantasy-land/ap', 'ap'],
return [...xs]
})
export const applyTo = flip(ap)
export const chain = dispatch(['fantasy-land/chain', 'chain'],
(f, a) => f(a)
)
@ -167,3 +157,9 @@ export const tee = curry((f, x) => {
return x
})
/**
* @param {...Fn} fns
* @returns {(x: any) => any}
*/
export const pipe = (...fns) => (...args) => reduce((v, f) => f(v), head(fns)(...args), tail(fns))