根据clojure中的地图值拆分地图集合

时间:2012-10-26 01:05:06

标签: clojure functional-programming

我正在尝试设置一个系统,根据提供的项目数量和有多少行来打印一定数量的页面。

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

主要的问题是我希望在产品达到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个字符

提前感谢大家!

1 个答案:

答案 0 :(得分: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。我想你应该能够从这里弄明白。

注意:此解决方案还有 lazy 的额外好处。这意味着如果你最终只想使用第一个分区,它就不会打扰整个字符串集。在您的情况下,如果用户决定仅查看前几页,则会转换为不对整个广告资源进行分区。