Skip to content

Instantly share code, notes, and snippets.

@rlorca
Created October 12, 2010 12:07
Show Gist options
  • Save rlorca/622075 to your computer and use it in GitHub Desktop.
Save rlorca/622075 to your computer and use it in GitHub Desktop.
;idea from the book onlisp, pag 30
(defn rev [nums]
(let [f (fn [coll res]
(if (empty? coll)
res
(recur (drop-last coll) (conj res (last coll)))))]
(f nums [])))
(defn rotate [arr op]
(loop [elem arr
left (mod op (count arr))]
(if (> left 0)
(recur
(conj (drop-last elem) (last elem))
(dec left))
elem)))
;different approach, cycling sequence
(defn rotate [arr times]
(let [size (count arr)
op (mod times size)]
(take
size
(drop op
(cycle arr)))))
@rlorca
Copy link
Author

rlorca commented Oct 12, 2010

DONE: find a better way to append an element to a collection. The place where conj will add the element is implementation dependent.
(elements are added at the beginning, so function starts from last element)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment