Last active
December 8, 2015 08:53
-
-
Save elierotenberg/4020c966ce2b08a8a69b to your computer and use it in GitHub Desktop.
Match with in JS DSL
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
import match, { $, $$$ } from 'match-with'; | |
// l is a list | |
const length = match(l).with( | |
// $() is a single-item named placeholder | |
// $$$() is a rest-collecting named placeholder | |
// .when(predicate) adds a guard predicate | |
clause([$('head'), $$$('tail')]).when(({ tail }) => tail.length > 0)).then(({ head, tail }) => 1 + tail.length), | |
// $ is a single-item anonymous placeholder | |
// $$$ is a rest-collecting anonymous placeholder | |
clause([$]).then(() => 1), | |
clause([]).then(() => 0), | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment