automat.core
$
($ tag)Defines a state tag, which can be correlated to a reducer function using compile.
advance
(advance fsm state input)(advance fsm state input reject-value)Advances a single position in the automaton. Takes a compiled fsm, a state which is either an initial reduce value or a CompiledAutomatonState previously returned by advance, and an input.
If a reject-value is specified, it will be returned if an invalid input is given. Otherwise an IllegalArgumentException is thrown.
(def auto (a/compile [1 2 3]))
(a/advance auto nil 1)
(a/advance auto nil 19)
(a/advance auto nil 19 :error)
(def adv (partial a/advance auto))
(-> nil (adv 1) (adv 2) (adv 3))
advance-stream
(advance-stream fsm state stream reject-value)Advances through the the stream, stopping if the stream is accepted or rejected. If the input state is accepted?, proceeds anyway. If rejected, reject-value is returned in lieu of the new state.
If an input state is already accepted and reject-value is returned, this means that inputs beyond the accepted sub-sequence have been consumed. As such, be careful when using impure functions and input sources.
compile
(compile fsm)(compile fsm {:keys [backend action-comparator], :as options})Compiles the fsm into something that can be used with find, advance-stream, greedy-find, and advance. Optionally takes an option map, which may contain:
reducers - a map or function of actions defined via $ onto reducer functions which take two arguments, the current reduction value and the input.
signal - a function that takes an input and returns the signal that will be used to advance the automaton. The input passed into reducer functions will not be affected by this.
action-comparator - an optional comparator function used to sort actions. The default is compare.
difference
(difference & args)Returns an automaton that accepts the disjoint of the first automaton and the subsequent automata.
find
(find fsm state stream)Searches for a accepted sub-sequence within the stream. If an input sequence is rejected, begins again at the automaton’s start state. If the input state is accepted?, returns immediately.
If the returned state has accepted? set to true, the matching sub-sequence is from start-index to stream-index. Since no inputs past the match are consumed, this can be safely used with impure functions and input sources.
greedy-find
(greedy-find fsm state stream)Greedily find the largest possible accepted sub-sequence. Will only return an accepted? state once subsequent inputs have been rejected. Since this always consumes more inputs than the accepted sub-sequence, be careful when using impure functions or input sources.
interpose-$
(interpose-$ tag fsm)Applies a state tag to all states within the given automaton.
matching-inputs
(matching-inputs fsm)Returns a lazy sequence of input sequences which the automaton will match.
(take 10 (matching-inputs [1 2 3]))
not
(not & args)Returns the complement of any zero or one-transition automata. Equivalent to the character negation ^ operator in regular expressions.
precompile
(precompile fsm)Takes an fsm, and returns a data structure where states are represented by numbers, and the provided keys are :accept, :state->input->state, and :state->input->actions. The start state will always be 0.
range
(range lower upper)Returns an automaton which matches any input within the inclusive range of [upper, lower].
(def r10-19 (a/compile (a/range 10 19)))
(a/advance r10-19 nil 11)
(a/advance r10-19 nil 121 :error)