Skip to content

Instantly share code, notes, and snippets.

View mApply
type Null = undefined | null
type MVal<T> = T | Null
type Nullify<T> = {
[P in keyof T]: MVal<T[P]>;
};
type MParams<T extends (...args: any) => any> = T extends (...args: infer P) => any ? Nullify<P> : never;
declare function apply<F extends (...args: any) => any>(f:F, ...args: Parameters<F>): ReturnType<F>
declare function mApply<F extends (...args: any) => any>(f:F, ...args: MParams<F>): MVal<ReturnType<F>>
@GheorgheP
GheorgheP / TS, mFn
Last active Apr 17, 2020
Transform a strict function in an function with nullified parameters.
View TS, mFn
type Null = undefined | null
type MVal<T> = T | Null
declare function mFn<T, R>(f:(a:T) => R): {
(a: T): R,
(a: Null): Null
}
declare function mFn<T, T2, R>(f:(a:T, b:T2) => R): {
(a: T, b:T2): R,
View composeAsync.js
const composeAsync = (...fs) => {
if (fs.length === 0) {
throw new Error('compose requires at least one argument')
}
return async (...as) => {
const call = async fs => {
const f = fs[0]
const tail = fs.slice(1, Infinity)
return await tail.length === 0 ? f(...as) : f(await call(tail))
@GheorgheP
GheorgheP / gist:f593e5579ca945687b2005d460c9ec77
Created Apr 23, 2017
ES6 curry, with normal function behavior
View gist:f593e5579ca945687b2005d460c9ec77
const curry = f => {
const _curry = (args = []) => args.length < f.length ? (...a) => _curry([...args, ...a]) : f(...args)
return _curry()
}
You can’t perform that action at this time.