Re: Problem adjusting the implementation of 'partition'
Glad I was able to help :) On Nov 23, 12:53 am, Stefan Rohlfing stefan.rohlf...@gmail.com wrote: Fantastic! Thanks again! On Nov 23, 3:50 pm, Benny Tsai benny.t...@gmail.com wrote: Indeed there is :) http://clojuredocs.org/clojure_core/clojure.core/cycle On Nov 23, 12:37 am, Stefan Rohlfing stefan.rohlf...@gmail.com wrote: Hi Benny, Your solution is much more elegant and flexible as my hacking of the core function! How would you implement 'pad-padding' (I like this name) if every item of the padding should be repeated in order, not only the last one? (defn pad-padding [padding] (concat padding (repeat padding))) does not work because of the additional parentheses around 'padding'. Is there a function that circles through the elements of a coll such as: (take 10 (circle [1 2 3])) ;; (1 2 3 1 2 3 1 2 3 1) Stefan On Nov 23, 3:17 pm, 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
Problem adjusting the implementation of 'partition'
Dear Clojure Group, I am trying to adjust the implementation of the 'partition' function so that the last element of a provided padding such as [a b] will be repeated if necessary to ensure the length of the last list returned by 'padding' is the same as the other lists. This is the original implementation of 'padding': (defn partition [n coll] (partition n n coll)) ([n step coll] (lazy-seq (when-let [s (seq coll)] (let [p (take n s)] (when (= n (count p)) (cons p (partition n step (drop step s ([n step pad coll] (lazy-seq (when-let [s (seq coll)] (let [p (take n s)] (if (= n (count p)) (cons p (partition n step pad (drop step s))) (list (take n (concat p pad) And here is my implementation of 'my-partition': (defn my-partition ([n coll] (partition n n coll)) ([n step coll] (lazy-seq (when-let [s (seq coll)] (let [p (take n s)] (when (= n (count p)) ;; 1) (cons p (partition n step (drop step s ([n step pad coll] (lazy-seq (when-let [s (seq coll)] (let [p (take n s)] (if (= n (count p)) (cons p (partition n step pad (drop step s))) ;; Changed from here on: (let [padding (take n (concat p pad))] (if (= n (count padding)) (list padding) (list (take n (concat padding (repeat (last padding) Unfortunately this doesn't work as 'partition' and 'my-partition' both return the same result when 'my-partition' should add additional padding instead: (partition 3 3 [a] [1 2 3 4 5 6 7]) ;; ((1 2 3) (4 5 6) (7 a)) (my-partition 3 3 [a] [1 2 3 4 5 6 7]) ;; ((1 2 3) (4 5 6) (7 a)) ;; Should be: ((1 2 3) (4 5 6) (7 a a a)) I believe that the last step is OK as it returns the expected result: (def pad [a b]) (take 10 (concat [1 2 a] (repeat (last pad ;; (1 2 a b b b b b b b) Does anybody know where I made a mistake? Any suggestions are highly appreciated! Stefan -- 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
Re: Problem adjusting the implementation of 'partition'
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
Re: Problem adjusting the implementation of 'partition'
Hi Meikel, What I want to accomplish is to have the function 'fill the gap' if not enough padding is supplied. With the standard 'partition' function this does not work, as it only adds as much padding as provided: (partition 3 3 [a] [1 2 3 4 5 6 7 8 9 10]) ;; ((1 2 3) (4 5 6) (7 8 9) (10 a)) I thought my implementation would solve this problem, but unfortunately there is some bug in the code I haven't been able to find. Stefan On Nov 23, 3:05 pm, 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
Re: Problem adjusting the implementation of 'partition'
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
Re: Problem adjusting the implementation of 'partition'
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
Re: Problem adjusting the implementation of 'partition'
Hi Benny, Your solution is much more elegant and flexible as my hacking of the core function! How would you implement 'pad-padding' (I like this name) if every item of the padding should be repeated in order, not only the last one? (defn pad-padding [padding] (concat padding (repeat padding))) does not work because of the additional parentheses around 'padding'. Is there a function that circles through the elements of a coll such as: (take 10 (circle [1 2 3])) ;; (1 2 3 1 2 3 1 2 3 1) Stefan On Nov 23, 3:17 pm, 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
Re: Problem adjusting the implementation of 'partition'
Hi Benny, This implementation looks great! If I could only find out what went wrong with my (not so elegant) solution... Stefan On Nov 23, 3:30 pm, Benny Tsai benny.t...@gmail.com wrote: 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
Re: Problem adjusting the implementation of 'partition'
Indeed there is :) http://clojuredocs.org/clojure_core/clojure.core/cycle On Nov 23, 12:37 am, Stefan Rohlfing stefan.rohlf...@gmail.com wrote: Hi Benny, Your solution is much more elegant and flexible as my hacking of the core function! How would you implement 'pad-padding' (I like this name) if every item of the padding should be repeated in order, not only the last one? (defn pad-padding [padding] (concat padding (repeat padding))) does not work because of the additional parentheses around 'padding'. Is there a function that circles through the elements of a coll such as: (take 10 (circle [1 2 3])) ;; (1 2 3 1 2 3 1 2 3 1) Stefan On Nov 23, 3:17 pm, 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
Re: Problem adjusting the implementation of 'partition'
Fantastic! Thanks again! On Nov 23, 3:50 pm, Benny Tsai benny.t...@gmail.com wrote: Indeed there is :) http://clojuredocs.org/clojure_core/clojure.core/cycle On Nov 23, 12:37 am, Stefan Rohlfing stefan.rohlf...@gmail.com wrote: Hi Benny, Your solution is much more elegant and flexible as my hacking of the core function! How would you implement 'pad-padding' (I like this name) if every item of the padding should be repeated in order, not only the last one? (defn pad-padding [padding] (concat padding (repeat padding))) does not work because of the additional parentheses around 'padding'. Is there a function that circles through the elements of a coll such as: (take 10 (circle [1 2 3])) ;; (1 2 3 1 2 3 1 2 3 1) Stefan On Nov 23, 3:17 pm, 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