4

我正在尝试制作一个只会生成值的序列,直到它找到以下条件并返回列出的结果:

案例头 =

  • 0 - 返回 {:origin [除 0 之外的所有生成] :pattern 0}
  • 1 - 返回 {:origin nil :pattern [all-generated-values] }
  • 重复值 - {: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 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))

在到达行尾之前停止的案例示例:

[9 8 4 5 6 7 4 ] - 重复“4”,因此停止。返回前面作为原点,其余作为模式。

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

[4 5 6 1] - 找到一个“1”所以停止,所以将所有内容作为模式返回

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

[3 0 ] - 找到一个 '0' 所以停止

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

:else 如果序列达到 x - 1 的长度:

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

问题

我已经成功地使用 partition-by 在找到重复值的点拆分组,但我想懒惰地这样做。有什么方法可以使用 take-while、condp 或 for 循环的 :while 子句来创建一个在发现重复时进行分区的条件?

一些尝试

(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))}))

# 更新

我真正想做的是找出一个值是否重复并在不使用索引的情况下对 seq 进行分区。那可能吗?像这样的东西-

{

(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})))

}

4

2 回答 2

1

我真正想做的是找出一个值是否重复并在不使用索引的情况下对 seq 进行分区。那可能吗?

我直接实施了您的更新要求。在这种情况下,split-with将优于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})))

例子:

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]})
于 2012-10-28T05:55:57.033 回答
1

懒惰是不可能的:你可以懒惰地消耗新元素,但你必须挂在所有旧元素上才能将它们用作模式,所以像(iterate inc 2)你这样的序列必须消耗所有可用内存。此外,for只允许您一次查看单个元素,因此它不适合此任务。但是,将其编写为循环/递归虽然有点乏味,但并不困难。如果序列在重复、1 或 0 之前结束,您没有指定要返回的内容,所以我只是猜测。

此外,您的第一个示例输出是错误的:它应该停在 1,而不是 4,所以我调整了您的输入。不过,除此之外,这个问题问得很好:感谢您清楚地说明问题,并描述您遇到的问题以及您尝试过的问题。

(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]})
于 2012-10-26T05:06:23.323 回答