Skip to content

Instantly share code, notes, and snippets.

View GheorgheP's full-sized avatar

Gheorghe GheorgheP

  • Moldova
  • 06:42 (UTC +02:00)
View GitHub Profile
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 April 17, 2020 12:59
Transform a strict function in an function with nullified parameters.
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,
@GheorgheP
GheorgheP / composeAsync.js
Last active July 2, 2017 07:16
composeAsync
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 April 23, 2017 06:51
ES6 curry, with normal function behavior
const curry = f => {
const _curry = (args = []) => args.length < f.length ? (...a) => _curry([...args, ...a]) : f(...args)
return _curry()
}