erm.js creates composable machines for pattern matching.
let monkeyKeystrokes =
infiniteTypewriters(infiniteMonkeys).readToEnd()
// raw (data + location) with closures
match(...monkeyKeystrokes)(
make(macbeth)(book => worksOfShakespeare.push(book)),
make(twogentlemenofverona)(book => worksOfShakespeare.push(book)),
make(ayorkshiretragedy)(haltAndCatchFire),
_
)
// values with closures
match(...monkeyKeystrokes)(
make(macbeth).stream(book => worksOfShakespeare.push(book)),
make(twogentlemenofverona).stream(book => worksOfShakespeare.push(book)),
make(ayorkshiretragedy)(haltAndCatchFire),
_
)
// values to arrays
match(...monkeyKeystrokes)(
make(macbeth).push(worksOfShakespeare),
make(twogentlemenofverona).push(worksOfShakespeare),
make(ayorkshiretragedy)(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
erm.js will work with arrays of anything, including strings.
- 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.
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.
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...
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:
// regex
let username = /[^@]*/.match(emailaddress)
saveUsername(username)
// erm with predicates
match(emailaddress)(
make(c => true).until(make(c => c == '@'))(output => saveUsername(output)),
_
)
// erm with literals
match(emailaddress)(
make(_).until('@')(output => saveUsername(output)),
_
)
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 } }
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.
npm install erm-js
The Match class exposes the basic building blocks { match, make, not, _ }
for composing machines:
const { match, make, not, _ } = require('erm-js').Match
match(..."my input data") (
make((i, n) => i == "i" && n == "n")(_in => console.log(_in)),
_
)
const { match, make, not, _ } = require('erm-js').Match
// simple predicates
let h = p => p == 'h'
let e = p => p == 'e'
let l = p => p == 'l'
let o = p => p == 'o'
let z = p => p == 'z'
// still simple but more useful
let hello = (ph,pe,pl,pL,po) => h(ph) && e(pe) && l(pl) && l(pL) && o(po)
// tada!
match(...'hello world')(
make(h)(x => console.log('h found:', x)),
make(e)(x => console.log('e found:', x)),
make(l)(x => console.log('l found:', x)),
_
)
// kapow!
match(...'hello world')(
make(hello)(x => console.log('hello found:', x)),
_
)
// zorb!
match(...'hello world')(
make(l).until(not(o))(x => console.log('ll found:', x)),
_
)
// implementing a take-while function
let takewhile = input => predicate => {
let items = []
match(...input)(
make(predicate).until(p => !predicate(p))(result => items.push(...result.value)).break(),
_
)
return items
}
// and using it
let taken = takewhile(dumbpasswords)(p => p != "111111")
// implementing a partition by function
let partition = input => predicate => {
let left = []
let right = []
match(...input)(
make(predicate)(q => left.push(q.value)),
make(p => !predicate(p))(q => right.push(q.value)),
_
)
return [left, right]
}
// and using it
let [lefthandside, righthandside] = partition(dumbpasswords)(p => p.match(/^\d*$/))