Last active
August 29, 2015 13:59
-
-
Save zaneli/10588354 to your computer and use it in GitHub Desktop.
「ClojureでNinety-Nine Lisp Problems(P16~20)」ブログ用
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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))) | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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) | |
) | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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) | |
) | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn my-drop | |
"Drop every N'th element from a list." | |
[xs n] | |
(keep-indexed #(when-not (zero? (mod (inc %1) n)) %2) xs) | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn my-drop | |
"Drop every N'th element from a list." | |
[xs n] | |
(mapcat butlast (partition-all n (concat xs [nil]))) | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)) | |
) | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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) | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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) | |
) | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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