Skip to content

Instantly share code, notes, and snippets.

@zaneli
Last active August 29, 2015 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zaneli/10588354 to your computer and use it in GitHub Desktop.
Save zaneli/10588354 to your computer and use it in GitHub Desktop.
「ClojureでNinety-Nine Lisp Problems(P16~20)」ブログ用
(defn my-drop
"Drop every N'th element from a list."
[xs n]
(map #(second %) (filter #(not= (mod (first %) n) (dec n)) (map-indexed vector xs)))
)
(defn my-drop
"Drop every N'th element from a list."
[xs n]
(let [elems (map-indexed vector xs), m (dec n)]
(for [[idx elem] elems :when (not= (mod idx n) m)] elem)
)
)
(defn my-drop
"Drop every N'th element from a list."
[xs n]
(let [elems (partition n n nil xs)]
(reduce #(concat %1 (take (dec n) %2)) nil elems)
)
)
(defn my-drop
"Drop every N'th element from a list."
[xs n]
(keep-indexed #(when-not (zero? (mod (inc %1) n)) %2) xs)
)
(defn my-drop
"Drop every N'th element from a list."
[xs n]
(mapcat butlast (partition-all n (concat xs [nil])))
)
(defn my-split
"Split a list into two parts; the length of the first part is given."
[xs n]
(let [[f s] (last (take (inc n) (iterate #(let [[xs [y & ys]] %] [(cons y xs), ys]) [nil, xs])))]
(list (filter #(not (nil? %)) (reverse f)) (if (nil? s) '() s))
)
)
(defn my-slice
"Extract a slice from a list."
[xs i k]
(for [[idx elem] (map-indexed vector xs) :when (and (< (- i 2) idx) (> k idx))] elem)
)
(defn my-rotate
"Rotate a list N places to the left."
[xs n]
(let [idx (if (pos? n) n (+ (count xs) n)), [f s] (split-at idx xs)]
(concat s f)
)
)
(defn my-remove-at
"Remove the K'th element from a list."
[xs n]
(reverse (first (reduce (fn [[elems cnt] elem]
[(if (not= cnt n) (cons elem elems) elems), (inc cnt)]
) [nil, 0] xs)))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment