Tom*_*oli 2 functional-programming clojure
我正在尝试设置一个系统,根据提供的项目数量和有多少行来打印一定数量的页面.
我基本上有一个包含几个字段的地图列表,包括一个名字字段,该字段可能很长并且跨越打印机上的多行.我有一个功能,可以辨别它将占用多少行,所以这没有问题.
主要的问题是,当它达到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"}]
Run Code Online (Sandbox Code Playgroud)
进入这个
[[{: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"}]]
Run Code Online (Sandbox Code Playgroud)
产品名称的最大行长度为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)
Run Code Online (Sandbox Code Playgroud)
您需要计算产品说明中的行数,而不是计算字符串中的字符数.您计算每个产品的行数的功能char-count与我上面的代码相同.我想你应该能够从这里弄明白.
注意:此解决方案具有延迟的额外好处.这意味着如果你最终只想使用第一个分区,它就不会打扰整个字符串集.在您的情况下,如果用户决定仅查看前几页,它将转换为不对整个库存进行分区.
| 归档时间: |
|
| 查看次数: |
243 次 |
| 最近记录: |