2

我正在尝试建立一个系统,该系统将根据提供的物品数量和有多少行打印一定数量的页面。

我基本上有一个带有几个字段的地图列表,包括一个可能很长并且跨越打印机上的多行的名称字段。我有一个功能可以辨别它将占用多少行,所以这没问题。

主要问题是我想在产品集合达到 30 行时拆分(好吧,分区,如果你使用 clojure 的术语),所以我可以开始另一个页面。有没有办法遍历集合,计算出最多 30 行的总行(如果有一个多行产品,否则会超过 30 行)然后拆分它?

我想转这个

[{:qty 2 :size "M" :product "Testing 1 2 3"}
 {:qty 1 :size "S" :product "Hello there world"}
 {:qty 12 :size "XS" :product "Some really long product name just to test"}
 {:qty 932 :size "L" :product "More longer names to play with"}
 {:qty 1 :size "M" :product "Another product name"}
 {:qty 1242 :size "XS" :product "This is just an obscenely long product name that I am hoping will spill over on to the next lines"}
 {:qty 2 :size "M" :product "Testing 1 2 3"}
 {:qty 1 :size "S" :product "Hello there world"}
 {:qty 12 :size "XS" :product "Some really long product name just to test"}
 {:qty 932 :size "L" :product "More longer names to play with"}
 {:qty 1 :size "M" :product "Another product name"}
 {:qty 1242 :size "XS" :product "This is just an obscenely long product name that I am hoping will spill over on to the next lines"}
 {:qty 2 :size "M" :product "Testing 1 2 3"}
 {:qty 1 :size "S" :product "Hello there world"}
 {:qty 12 :size "XS" :product "Some really long product name just to test"}
 {:qty 932 :size "L" :product "More longer names to play with"}
 {:qty 1 :size "M" :product "Another product name"}
 {:qty 1242 :size "XS" :product "This is just an obscenely long product name that I am hoping will spill over on to the next lines"}]

进入这个

[[{:qty 2 :size "M" :product "Testing 1 2 3"}
  {:qty 1 :size "S" :product "Hello there world"}
  {:qty 12 :size "XS" :product "Some really long product name just to test"}
  {:qty 932 :size "L" :product "More longer names to play with"}
  {:qty 1 :size "M" :product "Another product name"}
  {:qty 1242 :size "XS" :product "This is just an obscenely long product name that I am hoping will spill over on to the next lines"}
  {:qty 2 :size "M" :product "Testing 1 2 3"}
  {:qty 1 :size "S" :product "Hello there world"}
  {:qty 12 :size "XS" :product "Some really long product name just to test"}
  {:qty 932 :size "L" :product "More longer names to play with"}
  {:qty 1 :size "M" :product "Another product name"}]
 [{:qty 1242 :size "XS" :product "This is just an obscenely long product name that I am hoping will spill over on to the next lines"}
  {:qty 2 :size "M" :product "Testing 1 2 3"}
  {:qty 1 :size "S" :product "Hello there world"}
  {:qty 12 :size "XS" :product "Some really long product name just to test"}
  {:qty 932 :size "L" :product "More longer names to play with"}
  {:qty 1 :size "M" :product "Another product name"}
  {:qty 1242 :size "XS" :product "This is just an obscenely long product name that I am hoping will spill over on to the next lines"}]]

产品名称的最大行长为 25 个字符

提前致谢!

4

1 回答 1

5

我会给你一个类似问题的答案,你应该能够从那里推断出你的问题的答案。


问题:

如何将字符串序列分组为相邻字符串的子组,每个子组中最多n 个字符?

解决方案:

;; Data
(def input
  ["asdjasjdklasj" "dkjfsj" "dfkjsj" "kfjskd" "skdjfsjkdjdfs"
   "dfjskd" "wiuennsdw" "dskjdfsdjwed" "wiuf" "mncxnejs" "fjsjd"
   "dkjsf" "djsk" "djf" "erjfdkjcxzasd" "sjkja"])

;; Counting function
(def char-count count)

;; Partition function
(defn group-to-size [size coll]
  (lazy-seq
    (loop [[x & xs' :as xs] coll, n 0, acc []]
      (if (nil? x) (cons acc nil)
        (let [n' (+ n (char-count x))]
          (if (<= n' size)
            (recur xs' n' (conj acc x))
            (cons acc (group-to-size size xs))))))))

;; Sample grouping by 15 characters
(group-to-size 15 input)

; => (["asdjasjdklasj"]
;     ["dkjfsj" "dfkjsj"]
;     ["kfjskd"]
;     ["skdjfsjkdjdfs"]
;     ["dfjskd" "wiuennsdw"]
;     ["dskjdfsdjwed"]
;     ["wiuf" "mncxnejs"]
;     ["fjsjd" "dkjsf" "djsk"]
;     ["djf"]
;     ["erjfdkjcxzasd"]
;     ["sjkja"])

;; Resulting character counts per group for the above sample
(->> (group-to-size 15 input)
     (map (comp count (partial apply concat))))

; => (13 12 6 13 15 12 12 14 3 13 5)

您不想计算字符串中的字符数,而是要计算产品描述中的行数。您计算每个产品行数的函数相当于char-count我上面的代码。我想你应该能够从这里弄清楚。

注意:此解决方案具有懒惰的额外好处。这意味着如果您最终只想使用第一个分区,它不会打扰对整个字符串集进行分区。在您的情况下,如果用户决定只查看前几页,它将转化为不分区整个库存。

于 2012-10-26T02:03:17.803 回答