Created
January 9, 2019 16:52
-
-
Save busypeoples/ef2a74739426639ecfdffca9a79fbf6c to your computer and use it in GitHub Desktop.
Pattern Matching Functionality
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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