Skip to content

Instantly share code, notes, and snippets.

@cgrand
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cgrand/6ef20e02eb3c0c88c5cf to your computer and use it in GitHub Desktop.
Save cgrand/6ef20e02eb3c0c88c5cf to your computer and use it in GitHub Desktop.
seqexp with custom registers
; (as of https://github.com/cgrand/seqexp/commit/0defb92c1f28fdb5ae37fd7d7325b29fc7518d0f)
; (spelling of 'occurrence' is fixed by the next commit...)
; plain old last subgroup behaviour
=> (exec (*' (+? odd?) (as :threes (+ 3)))
[3 3 3 7 7 3 3 2])
{:threes (3 3), :match (3 3 3 7 7 3 3), :rest (2)}
; but now we can ask for all the occurrences for a given group
=> (exec (*' (+? odd?) (as :threes (+ 3)))
[3 3 3 7 7 3 3 2]
:groups {:threes all-occurences})
{:match (3 3 3 7 7 3 3), :rest (2), :threes [(3 3 3) (3 3)]}
; in truth this is accomplished by reducing over occurrences
=> (source all-occurences)
(def all-occurences (reduce-occurences conj []))
nil
; so, for example, you may choose to count occurences instead!
=> (exec (*' (+? odd?) (as :threes (+ 3)))
[3 3 3 7 7 3 3 2]
:groups {:threes (reduce-occurences (fn [n _] (inc n)) 0)})
{:match (3 3 3 7 7 3 3), :rest (2), :threes 2}
; being able to see all subgroups allows to see more directly the consequence of greediness/reluctancy:
=> (exec (*' (as :sep (+? odd?)) (as :threes (+ 3)))
[3 3 3 7 7 3 3 2]
:groups {:threes all-occurences
:sep all-occurences})
{:match (3 3 3 7 7 3 3), :rest (2), :threes [(3 3 3) (3 3)], :sep [(7 7)]}
=> (exec (*' (as :sep (+ odd?)) (as :threes (+ 3)))
[3 3 3 7 7 3 3 2]
:groups {:threes all-occurences
:sep all-occurences})
{:match (3 3 3 7 7 3 3), :rest (2), :threes [(3 3 3) (3)], :sep [(7 7 3)]}
=> (exec (*' (as :sep (+? odd?)) (as :threes (+ 3)))
[3 3 3 7 7 3 3 2]
:groups {:threes all-occurences
:sep all-occurences})
{:match (3 3 3 7 7 3 3), :rest (2), :threes [(3 3 3) (3 3)], :sep [(7 7)]}
; in this case you even get three :threes groups:
=> (exec (*' (as :sep (+? odd?)) (as :threes (+? 3)))
[3 3 3 7 7 3 3 2]
:groups {:threes all-occurences
:sep all-occurences})
{:match (3 3 3 7 7 3 3), :rest (2), :threes [(3) (3) (3 3)], :sep [(3) (7 7)]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment