Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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