clojure - A regex style matching library for generic matching on list items -
i've seen library around before forgotten called.
you can specify pattern matches elements in list, along lines of:
(def oddsandevens (pattern (n-of odd? :*) (n-of even? 2) :$)) (pattern-match oddsandevens [1 1 2 2]) => true (pattern-match oddsandevens [1 1 1 2 2]) => true (pattern-match oddsandevens [1 1 2 2 2]) => false (pattern-match oddsandevens [1 1 2]) => false if i'm totally imagining this, can shed light on how 1 might write 1 of these things?
more generally, asking expressive way parse sequence. there of course many parsing libraries out there clojure, many of them complect lexing parsing (there may reasons in terms of optimizing performance), , can used on strings. might have outside toolbox find parser allows lexing separate concern.
take, example, the parsatron (weighing 262 loc)
(require '[the.parsatron ; sampling of available combinators :refer [run token attempt many times choice never >> eof]]) (defn matches? [parser input] (run (choice (attempt (>> parser (eof) (always true))) (always false)) input)) now define pattern
(def odds-and-evens (>> (many (token odd?)) (times 2 (token even?)))) and test
(matches? odds-and-evens [1 1 2 2]) ;=> true (matches? odds-and-evens [1 1 1 2 2]) ;=> true (matches? odds-and-evens [1 1 2 2 2]) ;=> false (matches? odds-and-evens [1 1 2]) ;=> false from here can add sugar specify pattern desired.
Comments
Post a Comment