Skip to content

Instantly share code, notes, and snippets.

;; ariarule's solution to Split a sequence
;; https://4clojure.com/problem/49
(fn [n xv] [(subvec xv 0 n) (subvec xv n)])
;; 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)))))
;; 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)
;; 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))
;; 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))))
;; 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)))
;; 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)))))
;; 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))]
;; ariarule's solution to Juxtaposition
;; https://4clojure.com/problem/59
(fn new-juxt [& fs]
(fn [& args]
(map #(apply % args) fs)))
;; ariarule's solution to Word Sorting
;; https://4clojure.com/problem/70
(fn sortwords [s]
(letfn [(wordcomp [w x]
(apply compare (map #(.toLowerCase %) [w x])))]
(sort wordcomp (.split s "[^a-zA-Z]"))))