meows
Version:
A kittybin of tools.
132 lines (130 loc) • 4.6 kB
TypeScript
export interface cond extends Function {
(x: any): boolean;
}
/**
* Takes a sequence of functions and composes them from right to left, where
* given an array of functions such as `[λa, λb, λc]`, return a new function
* `(...args) => λa(λb(λc(...args)))`.
*
* @example
* let add2 = x => x + 2
* let mul3 = x => x * 3
* let sub1 = x => x - 1
* let λa = compose(add2, mul3) // add2(mul3(...a))
* let λb = compose(mul3, add2) // mul3(add2(...a))
* let λc = compose(add2, mul3, sub1) // add2(mul3(sub1(...a)))
* λa(0) // => 2
* λb(0) // => 6
* λc(0) // => -1
*/
export declare const compose: (...fns: Function[]) => Function;
/**
* Takes a sequence of functions and composes them from left to right, where
* given an array of functions such as `[λa, λb, λc]`, return a new function
* `(...args) => λc(λb(λa(...args)))`.
*
* @example
* let add2 = x => x + 2
* let mul3 = x => x * 3
* let sub1 = x => x - 1
* let λa = pipe(add2, mul3) // mul3(add2(...a))
* let λb = pipe(mul3, add2) // add2(mul3(...a))
* let λc = pipe(add2, mul3, sub1) // sub1(mul3(add2(...a)))
* λa(0) // => 6
* λb(0) // => 2
* λc(0) // => 5
*/
export declare const pipe: (...fns: Function[]) => Function;
/**
* Given a lambda `λ` and two arrays of non-zero length, `arr1` and `arr2`,
* creates new list where each new value indexed at position `i` corresponds to
* the result of `λ(arr1[i], arr2[i])`.
*
* @example
* const triangles = [0, 1, 3, 6, 10]
* const squares = [0, 1, 4, 9, 16]
* const naturals = [0, 1, 2, 3]
* const minus = (a, b) => a - b
*
* zipWith(minus, squares, triangles) // => [0, 0, 1, 3, 6]
* zipWith(minus, triangles, naturals) // => [0, 0, 1, 3]
* zipWith(minus, [], triangles) // => []
*/
export declare function zipWith(λ: Function, arr1: any[], arr2: any[]): any[];
/**
* @example
* [true, true, false].filter(identity) // => [true, true]
*/
export declare const identity: (x: any) => any;
/**
* Given a lambda and a value `x`, run the lambda with optional arguments as a
* side effect, and then return the original given value `x`.
*
* @example
* function sayHi(msg = 'hi') { console.log(msg) }
* effectId(sayHi, 42) // => logs 'hi' and returns 42
* effectId(sayHi, 42, 'yo') // => logs 'yo' and returns 42
*/
export declare function effectId(λ: Function, x: any, ...arg: any[]): any;
/**
* Count the number of array elements which pass a boolean function.
* @example
* const v1 = [2, 4, 6, 8]
* const v2 = [2, 4, 6, 9]
* const even = x => x % 2 === 0
*
* count(even, v1) // => 4
* count(even, v2) // => 1
* count(even, []) // => 0
*/
export declare function count(λ: cond, arr: any[]): number;
/**
* Given λ → returns `(compose not λ)`.
* @example
* const notArray = not(Array.isArray)
* notArray([]) // => false
* notArray({}) // => true
*/
export declare const not: (λ: Function) => (...arg: any[]) => boolean;
/**
* Checks if an array is a **non-empty array** of items that would all pass your
* boolean function. This is unlike the normal convention to pass on empty
* arrays.
*
* @example
* always(even, [0, 2, 4]) // => true
* always(even, [0, 1, 2]) // => false
* always(even, []) // => false, fails on empty array!
*/
export declare function always(λ: cond, arr: any[]): boolean;
/**
* Coerces values to `true`. If input is a function, `truthy` will run the
* function with optional arguments for side effects before returning `true`.
* @example
* truthy(console.log('hi')) && 42 // logs 'hi' and returns 42
*
* const say = (msg='hi') => console.log(msg)
* truthy(say) // logs 'hi' and returns true
* truthy(say, 'yo') // logs 'yo' and returns true
*/
export declare function truthy(λ: Function, ...arg: any[]): boolean;
/**
* Coerces values to `false`. If input is a function, `falsy` will run the
* function with optional arguments for side effects before returning `false`
*
* @example
* falsy(console.log('hi')) || 42 // logs 'hi' and returns 42
*
* function say(msg='hi') { console.log(msg) }
* falsy(say) // logs 'hi' and returns false
* falsy(say, 'yo') // logs 'yo' and returns false
*/
export declare function falsy(λ: Function, ...arg: any[]): boolean;
/**
* Checks if a non-empty array of functions all pass another non-empty array of
* values. Returns `false` on any empty array.
*
* @example
* allPassAll([isEven, isPositive, isSafe], [2, 4, 6, 8]) // => true
*/
export declare function allPassAll(tests: cond[], values: any[], throwOnEmpty?: boolean): boolean;