I am trying to make a sequence that will only generate values ββuntil it finds the following conditions and returns the listed results:
case head =
- 0 - return {: origin [all generated except 0]: pattern 0}
- 1 - return {: origin nil: pattern [all-generated-values]}
- repeat-value - {: origin [values-before-repeat]: pattern [values-after-repeat]
{
; n = int ; x = int ; hist - all generated values ; Keeps the head below x (defn trim-head [head x] (loop [head head] (if (> head x) (recur (- head x)) head))) ; Generates the next head (defn next-head [head xn] (trim-head (* head n) x)) (defn row [xn] (iterate
Examples of stopped cases before reaching the end of the line:
[9 8 4 5 6 7 4] - '4' is repeated so STOP. Go back as a beginning and rest as a pattern.
{:origin [9 8] :pattern [4 5 6 7]}
[4 5 6 1] - found "1", therefore STOP, so return everything as a template
{:origin nil :pattern [4 5 6 1]}
[3 0] - found "0", so STOP
{:origin [3] :pattern [0]}
: else if sequences reach length x - 1:
{:origin [all values generated] :pattern nil}
Problem
I used some success with splitting to split the groups at the point where the duplicate value was found, but I would like to do it lazily. Is there a way I can use either condp or: while in a for loop to create a condition that separates when it finds duplicates?
Some attempts
(take 2 (partition-by #(= 1 %) (row 11 4))) (for [p (partition-by #(stop-match? %) head) (iterate #(next-head % xn) n) :while (or (not= (last p) (or 1 0 n) (nil? (rest p))] {:origin (first p) :pattern (concat (second p) (last p))}))
# Updates
What I really want to do is find out if the value was repeated and split seq without using an index. Is it possible? Something like that -
{
(defn row [xn] (loop [hist [n] head (gen-next-head (first hist) xn) steps 1] (if (>= (- x 1) steps) (case head 0 {:origin [hist] :pattern [0]} 1 {:origin nil :pattern (conj hist head)} ; Speculative from here on out (let [p (partition-by #(apply distinct? %) (conj hist head))] (if-not (nil? (next p)) ; One partition if no repeats. {:origin (first p) :pattern (concat (second p) (nth 3 p))} (recur (conj hist head) (gen-next-head head xn) (inc steps))))) {:origin hist :pattern nil})))
}