Re: Repeating a vector n times
Damn, Meikel's solution is better, I was thinking: (apply concat (repeat n xs)) On Wed, Jul 13, 2011 at 10:54 AM, Bhinderwala, Shoeb sabhinderw...@wellington.com wrote: ** I have to write a function that will take a vector as input, repeat the elements multiple times and return back a single vector of the repeated items. I came up with the following but am wondering if there is a better or simpler way to write this: (def xs [a b c]) (defn repeat-vec-n [xs n] (vec (reduce concat [] (take n (repeat xs) OUTPUT: user= xs [a b c] user= (repeat-vec-n xs 3) [a b c a b c a b c] ***-- Shoeb* -- 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 -- 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: Repeating a vector n times
Thanks Tamreen. Your solution will have to be wrapped in another vec call. I will use Miekel's: (reduce into [] (repeat n xs)). From: clojure@googlegroups.com [mailto:clojure@googlegroups.com] On Behalf Of Tamreen Khan Sent: Wednesday, July 13, 2011 11:02 AM To: clojure@googlegroups.com Subject: Re: Repeating a vector n times Damn, Meikel's solution is better, I was thinking: (apply concat (repeat n xs)) On Wed, Jul 13, 2011 at 10:54 AM, Bhinderwala, Shoeb sabhinderw...@wellington.com wrote: I have to write a function that will take a vector as input, repeat the elements multiple times and return back a single vector of the repeated items. I came up with the following but am wondering if there is a better or simpler way to write this: (def xs [a b c]) (defn repeat-vec-n [xs n] (vec (reduce concat [] (take n (repeat xs) OUTPUT: user= xs [a b c] user= (repeat-vec-n xs 3) [a b c a b c a b c] -- Shoeb -- 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 mailto:clojure%2bunsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -- 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 -- 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: Repeating a vector n times
On Wed, Jul 13, 2011 at 11:21 AM, Bhinderwala, Shoeb sabhinderw...@wellington.com wrote: Thanks Tamreen. Your solution will have to be wrapped in another vec call. I will use Miekel’s: (reduce into [] (repeat n xs)). What about (vec (take n (cycle xs)))? -- Protege: What is this seething mass of parentheses?! Master: Your father's Lisp REPL. This is the language of a true hacker. Not as clumsy or random as C++; a language for a more civilized age. -- 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: Repeating a vector n times
Ken, that'll result in the original vector back out again. (vec (take 3 (cycle [1 2 3]))) = [1 2 3]. I think you mean: (vec (take (* n (count xs)) (cycle xs On Wed, Jul 13, 2011 at 5:13 PM, Ken Wesson kwess...@gmail.com wrote: On Wed, Jul 13, 2011 at 11:21 AM, Bhinderwala, Shoeb sabhinderw...@wellington.com wrote: Thanks Tamreen. Your solution will have to be wrapped in another vec call. I will use Miekel’s: (reduce into [] (repeat n xs)). What about (vec (take n (cycle xs)))? -- Protege: What is this seething mass of parentheses?! Master: Your father's Lisp REPL. This is the language of a true hacker. Not as clumsy or random as C++; a language for a more civilized age. -- 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 -- 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: Repeating a vector n times
On Wed, Jul 13, 2011 at 5:17 PM, Sam Ritchie sritchi...@gmail.com wrote: Ken, that'll result in the original vector back out again. (vec (take 3 (cycle [1 2 3]))) = [1 2 3]. I think you mean: (vec (take (* n (count xs)) (cycle xs I wasn't using n to mean the number of repetitions of the xs but the total number of desired elements. One advantage of this method is if you want the first 20 elements from repeating a vector of seven items it won't set your hair on fire. :) -- Protege: What is this seething mass of parentheses?! Master: Your father's Lisp REPL. This is the language of a true hacker. Not as clumsy or random as C++; a language for a more civilized age. -- 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