clojure - 根据 clojure 中的映射值拆分映射集合

标签 clojure functional-programming

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

我基本上有一个包含几个字段的 map 列表,其中包括一个可能很长并且跨越打印机上的多行的名称字段。我有一个函数可以识别它将占用多少行,所以这不是问题。

主要问题是我想在产品集合达到 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 个字符

提前致谢!

最佳答案

我将为您提供类似问题的答案,您应该能够从那里推断出您问题的答案。


问题:

如何将字符串序列分组为相邻字符串的子组,每个子组中总共最多有 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。我想你应该能够从这里弄清楚。

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

关于clojure - 根据 clojure 中的映射值拆分映射集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13079475/

相关文章:

java - VAVR 撰写尝试和列表

functional-programming - 有条件地选择调用哪个函数

clojure - 了解 Clojure 中的斐波那契函数

unit-testing - Clojure - 测试 Pedestal 路线

haskell - 如何使仿函数的函数类型构造函数实例化?重复实例

javascript - 如何更改模板字符串推断的值

functional-programming - 具有所有中间值的列表的总和

json - 如何在 Clojure 中获取以字符串形式存储在数据库中的日期?

matlab - 将多维元胞数组的生成从 Matlab 转换为 Clojure 的最佳方法是什么

clojure - 避免覆盖变量名