Vaguery / klapaucius

A clean, tested, maintainable Push interpreter written in Clojure.
MIT License
31 stars 2 forks source link

:exec-cartesianblock and :code-cartesianblock #125

Open Vaguery opened 7 years ago

Vaguery commented 7 years ago

Take a (small) scalar and consume that many items from :exec or :code, respectively.

For each item in the collection, in order, execute the cartesian product. So if 10 items are consumed, produce the 10x10 pairs, in turn.

The continuation form may require a :generator.

Vaguery commented 7 years ago

One way to do this as a continuation:

Suppose there is an exec-each-pair instruction, which takes a single item and a collection, and produces

'(item (first list) :exec-each-pair item (rest list))

as a continuation form. That is, it will cycle through each pair of the item and the items of the list, exhausting the list. And disappear when done.

OK, so an :exec-all-pairs would take two collections (seq1 and seq2), and would build this continuation:

'(:exec-each-pair (first seq1) (seq2) :exec-all-pairs (rest seq1) seq2)

That is, it launches an :exec-each-pair for the first item of seq1, and every item of seq2, and re-runs itself for the remainder, so all pairs are eventually matched up.

Now :exec-cartesian-pairs produces this continuation form, given a single seq of items:

'(:exec-all-pairs seq seq)

which develops into

'(:exec-each-pair (first seq) (seq) :exec-all-pairs (rest seq) seq)

and so on