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

there and back #520

Open
wants to merge 3 commits 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
96 changes: 77 additions & 19 deletions src/one_function_to_rule_them_all.clj
Original file line number Diff line number Diff line change
@@ -1,43 +1,101 @@
(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 [str1 str2] (str str1 " " str2))
a-seq)))

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

(defn my-count [a-seq]
:-)
(reduce (fn [cnt elem] (inc cnt))
0
a-seq))

(defn my-reverse [a-seq]
[:-])
(reduce (fn [acc e] (cons e acc))
()
a-seq))

(defn min-max-element [a-seq]
[:-])
(let [min-max (fn [min-max-vec elem]
[(min elem (first min-max-vec))
(max elem (second min-max-vec))])
fst (first a-seq)]
(reduce min-max [fst fst] a-seq)))

(defn insert [sorted-seq n]
[:-])
(loop [init (take-while #(< % n) sorted-seq)
tail (drop-while #(< % n) sorted-seq)]
(concat init
(cons n tail))))

(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] (- 0 x))
([x y] (- x y)))

(defn count-params
([& more] (count more)))

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

(defn count-params [x]
:-)
(defn pred-and
([] (fn [x] true))
([pred] pred)
([p1 p2] (fn [x] (and (p1 x) (p2 x))))
([p1 p2 & more] (fn [x] (and ((pred-and p1 p2) x)
(every? true? (map #(% x) more))))))

(defn my-* [x]
:-)
(defn head-taker [xss]
(cond (empty? xss) ()
(some empty? xss) ()
:else (cons (first (first xss))
(head-taker (rest xss)))))

(defn pred-and [x]
(fn [x] :-))
(defn removed-heads [xss]
(cond (empty? xss) ()
(some empty? xss) ()
:else (cons (rest (first xss))
(removed-heads (rest xss)))))

(defn my-map [f a-seq]
[:-])
(defn my-map
([f xs] (loop [ys xs
acc []]
(if (empty? ys)
acc
(recur (rest ys)
(conj acc (f (first ys)))))))
([f xs & more] (loop [ys xs
tail more
acc []]
(if (empty? ys)
acc
(recur (rest ys)
(removed-heads tail)
(conj acc
(apply f (head-taker (cons ys tail)))))))))