Clojure字符串分区列表,累积结果

时间:2018-02-19 21:30:55

标签: clojure

我很抱歉标题缺乏精确性,但这可能说明我缺乏clojure经验。

我试图获取一个大的字符串列表,并将该列表转换为另一个字符串列表,连续进行,直到累加器的长度小于某个长度。

例如,如果我有

[ "a" "bc" "def" "ghij" ]

并且我的最大字符串长度是4,我会沿着列表走,累积concat,直到我的积累len> 4,然后从头开始累加器。我的结果如下:

[ "abc" "def" "ghij" ]

我似乎无法为partition-by提出适当的咒语,这让我有点疯狂。我一直试图让我的累加器成为atom(但似乎无法找出reset!的位置),但除此之外,我无法看到/如何跟踪我累积的字符串。

提前感谢任何怜悯我的人。

4 个答案:

答案 0 :(得分:2)

这是我对此的看法:

(defn collapse [maxlen xs]
  (let [concats (take-while #(< (count %) maxlen) (reductions str xs))]
    (cons (last concats) (drop (count concats) xs))))
(collapse 4 ["a" "bc" "def" "ghij"])
;; => ("abc" "def" "ghij")

答案 1 :(得分:2)

(defn catsize [limit strs]
  (reduce (fn [res s]
              (let [base (peek res)]
                (if (> (+ (.length ^String base) (.length ^String s)) limit)
                  (conj res s)
                  (conj (pop res) (str base s)))))
          (if (seq strs) [(first strs)] [])
          (rest strs)))

答案 2 :(得分:1)

这非常接近。我不确定为什么你在最后一个字符串的末尾有j。

(sequence
 (comp
  (mapcat seq)
  (partition-all 3)
  (map clojure.string/join))
 ["a" "bc" "def" "ghij"]) => ("abc" "def" "ghi" "j")

答案 3 :(得分:-4)

我将如何做到这一点:

(ns tst.demo.core
  (:use demo.core tupelo.core tupelo.test))

(def bound 4)

(defn catter [strings-in]
  (loop [merged-strs    []
         curr-merge     (first strings-in)
         remaining-strs (rest strings-in)]
   ;(newline) (spyx [merged-strs curr-merge remaining-strs])
    (if (empty? remaining-strs)
      (conj merged-strs curr-merge)
      (let          ; try using 'let-spy' instead
        [new-str   (first remaining-strs)
         new-merge (str curr-merge new-str)]
        (if (< (count new-merge) bound)
          (recur merged-strs new-merge (rest remaining-strs))
          (recur (conj merged-strs curr-merge) new-str (rest remaining-strs)))))))

(dotest
  (is=  ["abc" "def" "ghij"]     (catter ["a" "bc" "def" "ghij"]) )
  (is=  ["abc" "def" "ghij"]     (catter ["a" "b" "c" "def" "ghij"]) )
  (is=  ["abc" "def" "ghij"]     (catter ["a" "b" "c" "d" "ef" "ghij"]) )
  (is=  ["abc" "def" "ghij"]     (catter ["a" "bc" "d" "ef" "ghij"]) )
  (is=  ["abc" "def" "ghij"]     (catter ["a" "bc" "d" "e" "f" "ghij"]) )

  (is=  ["abc" "def" "gh" "ij"]  (catter ["abc" "d" "e" "f" "gh" "ij"]) )
  (is=  ["abc" "def" "ghi" "j"]  (catter ["abc" "d" "e" "f" "ghi" "j"]) )

  (is=  ["abcdef" "ghi" "j"]     (catter ["abcdef" "ghi" "j"]) )
  (is=  ["abcdef" "ghi" "j"]     (catter ["abcdef" "g" "h" "i" "j"]) )
)

您需要将[tupelo "0.9.71"]添加到项目依赖项中。

更新

如果您使用spylet-spy,则可以看到算法用于获得结果的过程。例如:

(catter ["a" "b" "c" "d" "ef" "ghij"]) )   =>     ["abc" "def" "ghij"]     

-----------------------------------------------------------------------------
strings-in => ["a" "b" "c" "d" "ef" "ghij"]

[merged-strs curr-merge remaining-strs] => [[] "a" ("b" "c" "d" "ef" "ghij")]
new-str => "b"
new-merge => "ab"

[merged-strs curr-merge remaining-strs] => [[] "ab" ("c" "d" "ef" "ghij")]
new-str => "c"
new-merge => "abc"

[merged-strs curr-merge remaining-strs] => [[] "abc" ("d" "ef" "ghij")]
new-str => "d"
new-merge => "abcd"

[merged-strs curr-merge remaining-strs] => [["abc"] "d" ("ef" "ghij")]
new-str => "ef"
new-merge => "def"

[merged-strs curr-merge remaining-strs] => [["abc"] "def" ("ghij")]
new-str => "ghij"
new-merge => "defghij"

[merged-strs curr-merge remaining-strs] => [["abc" "def"] "ghij" ()]

Ran 2 tests containing 10 assertions.
0 failures, 0 errors.