I am trying to set up a system that will print a certain amount of pages depending on the amount of items supplied and how many lines that have.
I basically have a list of maps with a couple of fields, including a name field that could be quite long and span over a number of lines on the printer. I have a function that can discern how many lines it will take up, so that’s no issue.
The main problem is that I want to split (well, partition, if you use clojure’s terminology) the collection of products when it gets to 30 lines, so I can start another page. Is there a way to run over the collection working out the total lines up to 30 (less if there is a multi-line product that would otherwise go over 30 lines) and then split it?
I’d like to turn this
[{: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"}]
into this
[[{: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"}]]
The max line-length for the product name is 25 characters
Thanks heaps in advance!
I’m going to give you an answer to a similar problem, and you should be able to extrapolate from there to get the answer to your problem.
Question:
How do I group a sequence of strings into subgroups of adjacent strings with a total of at most n characters in each subgroup?
Solution:
Instead of counting characters in a string you want to count lines in a product description. Your function for counting the number of lines per product is the equivalent of
char-countin my code above. I think you should be able to figure it out from here.Note: This solution has the added benefit of being lazy. That means it doesn’t bother partitioning the entire set of strings if you end up only wanting to use the first partition. In your case, it would translating into not partitioning the entire inventory if the user decides to only view the first couple pages.