Seq of Numbers from 0 to N and Back

25 views
Skip to first unread message

HiHeelHottie

unread,
Aug 24, 2011, 6:49:07 PM8/24/11
to Clojure

How can you generate a sequence of numbers from 0 to n and back? Here
is my try.

user> (require '(clojure.contrib [math :as math]))
nil
user> (take 15 (let [n 3] (drop n (map #(math/abs (- (mod % (* n 2))
n)) (range)))))
(0 1 2 3 2 1 0 1 2 3 2 1 0 1 2)

Aaron Cohen

unread,
Aug 24, 2011, 7:05:15 PM8/24/11
to clo...@googlegroups.com
One way: 

user=>(concat (range 5) (range 5 0 -1))
(0 1 2 3 4 5 4 3 2 1)

user=>(take 15 (cycl­e (conc­at (rang­e 5) (rang­e 5 0 -1)))­))

(0 1 2 3 4 5 4 3 2 1 0 1 2 3 4)

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@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+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

HiHeelHottie

unread,
Aug 24, 2011, 11:23:37 PM8/24/11
to Clojure

Thanks Aaron. Very neat!

On Aug 24, 7:05 pm, Aaron Cohen <aa...@assonance.org> wrote:
> One way:
>
> user=>(concat (range 5) (range 5 0 -1))
> (0 1 2 3 4 5 4 3 2 1)
>
> user=>(take 15 (cycl­e (conc­at (rang­e 5) (rang­e 5 0 -1)))­))
> (0 1 2 3 4 5 4 3 2 1 0 1 2 3 4)
>
Reply all
Reply to author
Forward
0 new messages