Skip to content

Instantly share code, notes, and snippets.

@busypeoples
Created January 9, 2019 16:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save busypeoples/ef2a74739426639ecfdffca9a79fbf6c to your computer and use it in GitHub Desktop.
Save busypeoples/ef2a74739426639ecfdffca9a79fbf6c to your computer and use it in GitHub Desktop.
Pattern Matching Functionality
// Modelled after Ramda's Cond function
// https://ramdajs.com/docs/#cond
const match = <T, U>(
conditions: [(i: T) => boolean, (i: T) => U][],
data: T
): U | undefined => {
return conditions.reduce((result: U | undefined, [predicate, transformer]) => {
if (result) {
return result;
}
if (predicate(data)) {
return transformer(data);
}
return undefined;
}, undefined);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment