Last active
August 29, 2015 14:03
-
-
Save cgrand/6ef20e02eb3c0c88c5cf to your computer and use it in GitHub Desktop.
seqexp with custom registers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; (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