Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

One Function to Rule Them All, 12/13 done #516

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 46 additions & 18 deletions src/one_function_to_rule_them_all.clj
Original file line number Diff line number Diff line change
@@ -1,43 +1,71 @@
(ns one-function-to-rule-them-all)

(defn concat-elements [a-seq]
:-)
(reduce concat '() a-seq))

(defn str-cat [a-seq]
:-)
(if (empty? a-seq)
""
(reduce (fn [x k] (str x " " k)) a-seq)))

(defn my-interpose [x a-seq]
[:-])
(if (empty? a-seq)
'()
(reduce (fn [k c] (conj k x c)) [(first a-seq)] (rest a-seq))))

(defn my-count [a-seq]
:-)
(let [counter (fn [acc k] (inc acc))]
(reduce counter 0 a-seq)))

(defn my-reverse [a-seq]
[:-])
(let [reverser (fn [k c] (cons c k))]
(reduce reverser '() a-seq)))

(defn min-max-element [a-seq]
[:-])
(let [comparing-function (fn [[a b] x]
(cond
(> x b) [a x]
(< x a) [x b]
:else [a b]))]
(reduce comparing-function [(first a-seq) (first a-seq)] (rest a-seq))))

(defn insert [sorted-seq n]
[:-])
(loop [left-part '()
right-part sorted-seq]
(if (or (empty? right-part) (<= n (first right-part)))
(concat left-part (cons n '()) right-part)
(recur (concat left-part (cons (first right-part) '())) (rest right-part)))))

(defn insertion-sort [a-seq]
[:-])
(reduce insert '() a-seq))

(defn toggle [a-set elem]
(if (contains? a-set elem)
(disj a-set elem)
(conj a-set elem)))

(defn parity [a-seq]
[:-])
(reduce toggle #{} a-seq))

(defn minus [x]
:-)
(defn minus
([x] (- 0 x))
([x y] (- x y)))

(defn count-params [x]
:-)
(defn count-params [& a-seq]
(count a-seq))

(defn my-* [x]
:-)
(defn my-* [& a-seq]
(let [number (count a-seq)]
(cond
(= number 0) 1
(= number 1) (first a-seq)
:else (reduce * a-seq))))

(defn pred-and [x]
(fn [x] :-))
(defn pred-and
([] (fn [x] true))
([x] x)
([x y] (fn [c] (and (x c) (y c))))
([x y & more] (reduce pred-and (pred-and x y) more)))

(defn my-map [f a-seq]
[:-])
[:-])