Question

I have a vector that contains numbers. The structure of the vector is the following:

  1. items of arbitrary length
  2. a separator [0 0 0 0 0] (0 repeated 5 times) between the items

I want to write a function split that retrieves the list of the items (without the separator): It is similar to string/split.

For instance:

(split [123 0 1 0 0 0 0 0 1 1 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 1 199])
; => [[123 0 1] [1 1] [1 0 1] [0 0 1 199]]

Remark: the code has to be effective as the length of the vector is around 1 million.

Thanks for your help.

Was it helpful?

Solution

Here:

(defn the-split [coll] 
  (let [part   (partition-by identity coll)
        ppart  (partition-by #(= [0 0 0 0 0] %) part)
        almost (map #(apply concat %) ppart)]
    (filter (partial not= [0 0 0 0 0]) almost)))


(the-split [123 0 1 0 0 0 0 0 1 1 0 0 0 0 0 1 0 1 0 0 0 0 0 1 199])
=> ((123 0 1) (1 1) (1 0 1) (1 199))

OTHER TIPS

Here's one approach -- with the [0 0 0 0 0] separator hardwired, but straightforward to generalize:

(defn split5z [xs]
  (let [delim [0 0 0 0 0]
        step (fn step [xs seg]
               (lazy-seq
                (if-let [xs (seq xs)]
                  (let [window (take 5 xs)]
                    (if (= window delim)
                      (cons seg (step (drop 5 xs) []))
                      (step (rest xs) (conj seg (first xs)))))
                  (list seg))))]
    (step xs [])))

Applying it to your sample input:

(split5z [123 0 1 0 0 0 0 0 1 1 0 0 0 0 0 1 0 1 0 0 0 0 0 1 199])
;= ([123 0 1] [1 1] [1 0 1] [1 199])

Wrap in vec if you'd like the output to be a vector rather than a seq of vectors.

Another approach -- this time eagerly consuming the input with loop/recur:

(defn split5z [sep xs]
  (let [scnt (count sep)]
    (loop [xs  (seq xs)
           out []
           seg []]
      (if xs
        (if (= (take scnt xs) sep)
          (recur (nthnext xs scnt)
                 (conj out seg)
                 [])
          (recur (next xs)
                 out
                 (conj seg (first xs))))
        (if (seq seg)
          (conj out seg)
          seg)))))

At the REPL:

(split5z [0 0 0 0 0]
         [123 0 1 0 0 0 0 0 1 1 0 0 0 0 0 1 0 1 0 0 0 0 0 1 199])
;= [[123 0 1] [1 1] [1 0 1] [1 199]]

A lazy solution:

(defn split [v]
  (let [delim (repeat 5 0)
        i (->> v (partition 5 1) (take-while #(not= delim %)) count)]
    (if (zero? i) [v] (lazy-seq (cons (subvec v 0 i)
                                      (split (subvec v (+ i 5))))))))

For example

(split [123 0 1 0 0 0 0 0 1 1 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 1 199])
; ([123 0 1] [1 1] [1 0 1] [0 0 1 199])
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top