Skip to content

Instantly share code, notes, and snippets.

@elierotenberg
Last active December 8, 2015 08:53
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 elierotenberg/4020c966ce2b08a8a69b to your computer and use it in GitHub Desktop.
Save elierotenberg/4020c966ce2b08a8a69b to your computer and use it in GitHub Desktop.
Match with in JS DSL
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