gpt4 book ai didi

clojure - 根据 clojure 中的 map 值拆分 map 集合

转载 作者:行者123 更新时间:2023-12-02 05:26:03 24 4
gpt4 key购买 nike

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

我基本上有一个包含几个字段的 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 中的 map 值拆分 map 集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13079475/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com