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上記のコードと同等です。ここから解けばいいと思います。

注:このソリューションには、 lazyであるという追加の利点があります。つまり、最初のパーティションのみを使用したい場合でも、文字列のセット全体を分割する必要はありません。あなたの場合、ユーザーが最初の数ページのみを表示することにした場合、インベントリ全体を分割しないことに変換されます。

于 2012-10-26T02:03:17.803 に答える