Or, to put it all together into a modified partition:

(defn my-partition
  ([n coll]
     (partition n n coll))
  ([n step coll]
     (partition n step coll))
  ([n step pad coll]
     (let [expanded-pad (concat pad (repeat (last pad)))]
       (partition n step expanded-pad coll))))

user=> (my-partition 4 4 ["a" "b"] [1 2 3 4 5 6 7 8 9])
((1 2 3 4) (5 6 7 8) (9 "a" "b" "b"))

On Nov 23, 12:25 am, Benny Tsai <benny.t...@gmail.com> wrote:
> Sorry, pad-padding is a terrible name choice on my part :)  Maybe
> something like "repeat-last" would be at least a little bit better.
>
> On Nov 23, 12:17 am, Benny Tsai <benny.t...@gmail.com> wrote:
>
>
>
>
>
>
>
> > Since (repeat) returns a lazy sequence, and partition will only take
> > as many as is needed, I think you don't even have to specify how many
> > times to repeat:
>
> > user=> (partition 3 3 (repeat "a") [1 2 3 4 5 6 7])
> > ((1 2 3) (4 5 6) (7 "a" "a"))
>
> > And if the desired behavior is to repeat the last element of provided
> > padding as many times as necessary, you could do it with a little
> > helper function:
>
> > (defn pad-padding [padding]
> >   (concat padding (repeat (last padding))))
>
> > user=> (partition 4 4 (pad-padding ["a" "b"]) [1 2 3 4 5 6 7 8 9])
> > ((1 2 3 4) (5 6 7 8) (9 "a" "b" "b"))
>
> > On Nov 23, 12:05 am, Meikel Brandmeyer <m...@kotka.de> wrote:
>
> > > Hi,
>
> > > while not having looked at your code, partition does what you want:
>
> > > user=> (partition 3 3 (repeat 3 "a") [1 2 3 4 5 6 7])
> > > ((1 2 3) (4 5 6) (7 "a" "a"))
> > > user=> (partition 3 3 (repeat 3 "a") [1 2 3 4 5 6 7 8])
> > > ((1 2 3) (4 5 6) (7 8 "a"))
> > > user=> (partition 3 3 (repeat 3 "a") [1 2 3 4 5 6 7 8 9])
> > > ((1 2 3) (4 5 6) (7 8 9))
> > > user=> (partition 3 3 (repeat 3 "a") [1 2 3 4 5 6 7 8 9 10])
> > > ((1 2 3) (4 5 6) (7 8 9) (10 "a" "a"))
>
> > > (Note: the 3 in repeat is not strictily necessary. 2 would be
> > > sufficient)
>
> > > Sincerely
> > > Meikel

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to