user945754
user945754

Reputation:

stop and split generated sequence at repeats - clojure

I am trying to make a sequence that will only generate values until it finds the following conditions and return the listed results:

case head =

{

; 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 x n]
  (trim-head (* head n) x))

(defn row [x n]
   (iterate #(next-head % x n) n))

; Generates a whole row - 
; Rows are a max of x - 1.
(take (- x 1) (row 11 3))

Examples of cases to stop before reaching end of row:

[9 8 4 5 6 7 4] - '4' is repeated so STOP. Return preceding as origin and rest as pattern.

{:origin [9 8] :pattern [4 5 6 7]}

[4 5 6 1] - found a '1' so STOP, so return everything as pattern

{:origin nil :pattern [4 5 6 1]}

[3 0] - found a '0' so STOP

{:origin [3] :pattern [0]}

:else if the sequences reaches a length of x - 1:

{:origin [all values generated] :pattern nil}

The Problem

I have used partition-by with some success to split the groups at the point where a repeated value is found, but would like to do this lazily. Is there some way I can use take-while, or condp, or the :while clause of the for loop to make a condition that partitions when it finds repeats?

Some Attempts

(take 2 (partition-by #(= 1 %) (row 11 4)))

(for [p (partition-by #(stop-match? %) head) (iterate #(next-head % x n) 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 be able to do is find out if a value has repeated and partition the seq without using the index. Is that possible? Something like this -

{

(defn row [x n]
  (loop [hist [n]
         head (gen-next-head (first hist) x n)
         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 x n) (inc steps)))))
      {:origin hist :pattern nil})))

}

Upvotes: 4

Views: 384

Answers (2)

tnoda
tnoda

Reputation: 1524

What I really want to be able to do is find out if a value has repeated and partition the seq without using the index. Is that possible?

I implemented your updated requirement straight forwardly. In this case, split-with would be preferable to partition-by.

;;; find out if a value has repeated, but considering zero and one.
(defn- generate
  "Returns a vector of [duplicate-value values-until-duplicate].
   duplicate-value might be zero or one."
  [s]
  (->> [s [] #{0 1}]
       (iterate (fn [[[head & more] generated idx]]
                  [more (conj generated head) (conj idx head)]))
       (take-while (comp seq first))
       (drop-while (fn [[[head & _] _ idx]]
                     (nil? (idx head))))
       first
       ((juxt ffirst second))))

;;; partition the seq without using the index.
(defn partition-by-duplicate
  [s]
  (let [[pivot generated-values] (generate s)]
    (cond (= 0 pivot) {:origin generated-values, :pattern [0]}
          (= 1 pivot) {:origin nil, :pattern (conj generated-values 1)}
          pivot (->> generated-values
                     (split-with (partial not= pivot))
                     (interleave [:pattern :origin])
                     (apply hash-map))
          :else {:origin s, :pattern nil})))

Example:

user> (map generate [[9 8 2 4 5 6 7 4] [4 5 6 1] [3 0]])
([4 [9 8 2 4 5 6 7]]
 [1 [4 5 6]]
 [0 [3]])

user> (map partition-by-duplicate [[9 8 2 4 5 6 7 4] [4 5 6 1] [3 0]])
({:pattern (9 8 2), :origin (4 5 6 7)}
 {:origin nil, :pattern [4 5 6 1]}
 {:origin [3], :pattern [0]})

Upvotes: 1

amalloy
amalloy

Reputation: 91857

Not much laziness is possible: you can lazily consume new elements, but you must hang onto all old elements to use them as the pattern, so on a sequence like (iterate inc 2) you must consume all available memory. Additionally, for only ever lets you look at a single element at once, so it is ill-suited to this task. However, writing it as a loop/recur is, while a bit tedious, not difficult. You didn't specify what to return if the sequence ends before a repeat, 1, or 0, so I just guessed.

Also, your first example output is wrong: it should stop at the 1, not at the 4, so I adjusted your input. Aside from that, though, the question is well-asked: thanks for specifying the problem clearly, and describing what you're having trouble with as well as what you've tried.

(defn trim-head [coll]                                                      
  (loop [so-far [], indexes {}, index 0, coll (seq coll)]                   
    (if-not coll                                                            
      {:origin nil, :pattern so-far} ;; ?? not specified in question        
      (let [x (first coll), xs (rest coll)]                                 
        (if (contains? indexes x)                                           
          {:origin (subvec so-far 0 (indexes x))                            
           :pattern (subvec so-far (indexes x))}                            
          (case x                                                           
            0 {:origin so-far, :pattern [x]}                                
            1 {:origin nil, :pattern (conj so-far x)}                       
            (recur (conj so-far x) (assoc indexes x index) (inc index) (seq xs))))))))

user> (map trim-head [[9 8 2 4 5 6 7 4] [4 5 6 1] [3 0]])                       
({:origin [9 8 2], :pattern [4 5 6 7]}
 {:origin nil, :pattern [4 5 6 1]} 
 {:origin [3], :pattern [0]})

Upvotes: 1

Related Questions