This file contains 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
;; | |
;; NS CHEATSHEET | |
;; | |
;; * :require makes functions available with a namespace prefix | |
;; and optionally can refer functions to the current ns. | |
;; | |
;; * :import refers Java classes to the current namespace. | |
;; | |
;; * :refer-clojure affects availability of built-in (clojure.core) | |
;; functions. |
This file contains 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
;; ariarule's solution to Split a sequence | |
;; https://4clojure.com/problem/49 | |
(fn [n xv] [(subvec xv 0 n) (subvec xv n)]) |
This file contains 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
;; ariarule's solution to Group a Sequence | |
;; https://4clojure.com/problem/63 | |
(fn mgb [f s] | |
(let [p (set (map f s))] | |
(apply hash-map (interleave p (map (fn [z] (vec (filter #(not (nil? %)) (map #(if (= z (f %)) %) s)))) p))))) |
This file contains 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
;; ariarule's solution to To Tree, or not to Tree | |
;; https://4clojure.com/problem/95 | |
(fn valid [n] | |
(if (nil? n) | |
true | |
(if (coll? n) | |
(if (= (count n) 3) | |
(and (valid (nth n 1)) (valid (nth n 2))) | |
false) |
This file contains 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
;; ariarule's solution to Infix Calculator | |
;; https://4clojure.com/problem/135 | |
(fn [& calc] | |
(reduce (fn [work current] | |
(if (number? work) | |
(list work current) | |
((second work) (first work) current))) calc)) |
This file contains 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
;; ariarule's solution to Reverse Interleave | |
;; https://4clojure.com/problem/43 | |
(fn [c n] | |
(reduce #(map (fn [x y] (concat x y)) %1 %2) (map #(partition 1 %) (partition n c)))) |
This file contains 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
;; ariarule's solution to Split by Type | |
;; https://4clojure.com/problem/50 | |
(fn [s] | |
(partition-by type (sort #(compare (hash (type %1)) (hash (type %2))) s))) |
This file contains 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
;; ariarule's solution to Find Distinct Items | |
;; https://4clojure.com/problem/56 | |
(fn my-dist [g] | |
(letfn [(getpos [i] (some (fn [x] (if (= i (second x)) (first x) nil)) (apply list (apply hash-map (interleave (range (count g)) g)))))] | |
(sort #(compare (getpos %1) (getpos %2)) (apply list (set g))))) |
This file contains 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
;; ariarule's solution to Function Composition | |
;; https://4clojure.com/problem/58 | |
(fn new-comp [& fs] | |
(letfn [(restdo [f margs] | |
(if (= '() f) | |
(first margs) | |
(restdo (rest f) [(apply (first f) margs)]))) | |
(firstdo [& margs] | |
(restdo (reverse fs) margs))] |
This file contains 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
;; ariarule's solution to Juxtaposition | |
;; https://4clojure.com/problem/59 | |
(fn new-juxt [& fs] | |
(fn [& args] | |
(map #(apply % args) fs))) |
OlderNewer