Skip to content

Instantly share code, notes, and snippets.

@ENvironmentSet
Last active August 21, 2020 20:50
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ENvironmentSet/4012d3d2969d3b9ecaf7717cc32a56d5 to your computer and use it in GitHub Desktop.
Save ENvironmentSet/4012d3d2969d3b9ecaf7717cc32a56d5 to your computer and use it in GitHub Desktop.
Generating eliminator for given constraint in typescript
/** If you want to know how to encoding HKTs in typescript, check this:
https://gist.github.com/ENvironmentSet/1662a140f99381bc85fd6be51ecdcbb5
Sorry for messy names, this was only PoC. **/
export interface HKT {
param: unknown;
result: unknown;
}
export type Apply<f extends HKT, x>
= (f & { param: x })['result'];
export type ElimWithConstraint<constraint extends HKT> = <R>(f: <A>(x: Apply<constraint, A>) => R) => R;
export function makeElimWithConstraint<constraint extends HKT>(): <A>(x: Apply<constraint, A>) => ElimWithConstraint<constraint> {
return x => f => f(x);
}
import { HKT, Apply, makeElimForConstraint } from './constraints';
export interface ExcludeBottomConstraint extends HKT {
result: this['param'] extends undefined | null ?
never
: this['param'];
}
export const elimNonBottom = makeElimWithConstraint<ExcludeBottomConstraint>();
import { HKT, Apply, makeElimForConstraint } from './constraints';
interface IdentityConstraint extends HKT {
result: this['param'];
}
export const elimAny = makeElimWithConstraint<IdentityConstraint>();
import { HKT, Apply, makeElimForConstraint } from './constraints';
export interface TruthyConstraint extends HKT {
result: this['param'] extends undefined | null | 0 | '' | false | typeof NaN ?
never
: this['param'];
}
export const elimTruthy = makeElimWithConstraint<TruthyConstraint>();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment