erm.js - The Esoteric Reducing Machine
erm.js creates composable machines for pattern matching.
let monkeyKeystrokes = // raw (data + location) with closures worksOfShakespeare worksOfShakespeare haltAndCatchFire _ // values with closures haltAndCatchFire _ // values to arrays haltAndCatchFire _
If like me you've got data, you've tried pattern matching with rxjs, you've tried finding arrays in arrays with the Knuth-Morris-Pratt algorithm, but it's all too much and not quite what you need, then maybe erm is the javascript pattern matching library you're looking for!
note: this project is (pre) alpha right now and discussed APIs are subject to change
Introduction
erm.js will work with arrays of anything, including strings.
Principle of Operation
- A match$machine has one iterable input and has many make$machines.
- A match$machine sends its iterable input in slices of n(1,∞) items to a make$machine accepting n arguments.
- When a make$machine predicate returns false, the match$machine restarts the slicing cycle sending input to the next make$machine in the chain.
- A match$machine will terminate when all input is accepted by the make$machines.
- A
_
machine is a make$machine that accepts any input and always advances match$machine 1 position.
The static Match API
match(...input)(make(...predicate:unary|...value)[.until(haltpredicate:unary|value)](output, [error])[,...])
match accepts ...input and returns a match$machine - a callable object that accepts one or more make$machines
make accepts ...predicate|...value and return a make$machine- a callable object that accepts an output callback and optionally and error callback.
Fixed Size Patterns
A make$machine will be activated with the same number of items as predicate has parameters -or- the same number of ...values provided; which is to say the make$machine has the same arity; so a predicate p => ...
will produce a machine that activates with 1 parameter, in this instance p
; and the values ...['4', '2']
will produce a machine that activates with 2 parameters. 4
and 2
. Because of this, predicates with a ...rest
parameter are not compatible-. Variable length patterns can be matched using make$machine.until - à la Kleene star...
Variable Size Patterns
The make$machine also exposes an optional until method which causes the machine to run again until the haltpredicate signals true. To illustrate this with an albeit contrived example, compare it to the Regex [^] and * operator:
// regexlet username = /[^@]*/ // erm with predicates _ // erm with literals _
Predicates, Values and Callbacks
predicate and haltpredicate are as you would expect, p => true|false.
If a value value is supplied to make it is automatically converted to p => p == value i.e. make(3.14) == make(p => p == 3.14)
output is your supplied callback function that is invoked with a single object { value, signal, location: { start, length } }
error is your optional callback function that is invoked with a single object { error, location: { start, length } }
Utility Functions and Constants
Match.not() will invert a predicate while preserving arity e.g. let TRUE = make(p => true); let FALSE = make(not(TRUE))
Match._ is the 'unit' value symbol and acts as a wildcard when used in place of a make$machine. Like the default:
label in a switch
statement, _
catches anything that your make$machines don't. Unlike the default:
label in a switch statement, a match$machine without a _
will not be able to read unmatched data and may not terminate.
Quick Start
Install
npm install erm-js
Usage
The Match class exposes the basic building blocks { match, make, not, _ }
for composing machines:
const match make not _ = Match console _
Examples
const match make not _ = Match // simple predicateslet p == 'h'let p == 'e'let p == 'l'let p == 'o'let p == 'z' // still simple but more usefullet && && && && // tada! console console console _ // kapow! console _ // zorb! console _ // implementing a take-while functionlet { let items = items _ return items} // and using itlet taken = p != "111111" // implementing a partition by functionlet { let left = let right = left right _ return left right} // and using itlet lefthandside righthandside = p