Skip to content

Instantly share code, notes, and snippets.

@frodo821
Created April 18, 2020 06:22
Show Gist options
  • Select an option

  • Save frodo821/3c7a12acf1d2fdf3b9a8c0205a522aa8 to your computer and use it in GitHub Desktop.

Select an option

Save frodo821/3c7a12acf1d2fdf3b9a8c0205a522aa8 to your computer and use it in GitHub Desktop.
interface Conditional<T> {
then: (expr: () => T) => Conditional<T>;
fail: (expr: () => T) => Conditional<T>;
result: () => T | undefined;
}
export default function cond<T>(condition: boolean): Conditional<T> {
let res: T | undefined;
return {
then(expr) {
if (condition) {
res = expr();
}
return this;
},
fail(expr) {
if (!condition) {
res = expr();
}
return this;
},
result: () => res,
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment