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

everything done #512

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .nrepl-port
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
50494
104 changes: 85 additions & 19 deletions src/one_function_to_rule_them_all.clj
Original file line number Diff line number Diff line change
@@ -1,43 +1,109 @@
(ns one-function-to-rule-them-all)

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

(concat-elements []) ;=> ()
(concat-elements [[1 2]]) ;=> (1 2)
(concat-elements [[1 2] [3 4]]) ;=> (1 2 3 4)

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

(str-cat ["I" "am" "Legend"]) ;=> "I am Legend"
(str-cat ["I" "am" "back"]) ;=> "I am back"
(str-cat ["more" " " "space"]) ;=> "more space"
(str-cat []) ;=> ""


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

(my-interpose 0 [1 2 3]) ;=> (1 0 2 0 3)
(my-interpose "," ["I" "me" "myself"]) ;=> ("I" "," "me" "," "myself")

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

(my-count [1 2 3])

(defn my-reverse [a-seq]
[:-])
(let [combine (fn [acc curr] (cons curr acc))]
(reduce combine () a-seq)))

(my-reverse [1 2 3]) ;=> (3 2 1)
(my-reverse [1 2]) ;=> (2 1)
(my-reverse []) ;=> ()

(defn min-max-element [a-seq]
[:-])
(vector (reduce min a-seq) (reduce max a-seq)))

(min-max-element [2 7 3 15 4]) ;=> [2 15]
(min-max-element [1 2 3 4]) ;=> [1 4]
(min-max-element [1]) ;=> [1 1]


(defn insert [sorted-seq n]
[:-])
(loop [s sorted-seq a []]
(cond
(empty? s) (conj a n)
(< (first s) n) (recur (rest s) (conj a (first s)))
:else (apply conj a n s))))

(defn insertion-sort [a-seq]
[:-])
(let [srt (fn [acc new] (insert acc new))]
(reduce srt [] a-seq)))

(insertion-sort [8 1 6 4 2 3])

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

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

(parity [1 2 31 3 2])

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

(minus 2 1)

(defn count-params
([& more]
(reduce (fn [acc x] (inc acc)) 0 more)))

(count-params :a)
(count-params)

(defn minus [x]
:-)
(defn my-*
([] 1)
([& more]
(reduce (fn [acc y] (* acc y)) 1 (seq more))))

(defn count-params [x]
:-)
(my-* 2 3)

(defn my-* [x]
:-)
(defn pred-and
([] (fn [x] true))
([pred1?] pred1?)
([pred1? pred2?] (fn [x] (and (pred1? x) (pred2? x))))
([pred1? pred2? & more] (reduce pred-and (pred-and pred1? pred2?) more)))

(defn pred-and [x]
(fn [x] :-))
(pred-and (filter (pred-and) [1 0 -2]))

(defn my-map [f a-seq]
[:-])
(defn my-map
([f & more]
(loop [result []
left more]
(if (some empty? left)
(seq result)
(recur (conj result (apply f (map first left))) (map rest left))))))