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

completed #519

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
75 changes: 56 additions & 19 deletions src/one_function_to_rule_them_all.clj
Original file line number Diff line number Diff line change
@@ -1,43 +1,80 @@
(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)
""
(let [helper (fn [m n] (str m " " n))]
(reduce helper a-seq))))

(defn my-interpose [x a-seq]
[:-])
(if (empty? a-seq)
[]
(let [helper (fn [y m] (conj (conj y x) m))]
(rest (reverse (reduce helper '() a-seq))))))

(defn my-count [a-seq]
:-)
(reduce (fn [x y] (inc x)) 0 a-seq))

(defn my-reverse [a-seq]
[:-])
(reduce conj '() a-seq))

(defn min-max-element [a-seq]
[:-])
(if (empty? a-seq)
[]
(let [minV (reduce min a-seq) maxV (reduce max a-seq)]
[minV maxV])))

(defn insert [sorted-seq n]
[:-])
(loop [rseqs '()
inputseq sorted-seq]
(if (empty? inputseq)
(reverse (conj rseqs n))
(if (< (first inputseq) n)
(recur (conj rseqs (first inputseq)) (rest inputseq))
(reverse (apply conj (conj rseqs n) inputseq))))))

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

(defn toggle [a-seq element]
(if (contains? a-seq element)
(disj a-seq element)
(conj a-seq element)))


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

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

(defn count-params [x]
:-)
(defn count-params
([] 0)
([x] 1)
([x & more]
(reduce (fn [x y] (inc x)) 1 more)))

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

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

(defn my-map [f a-seq]
[:-])
(defn my-map
([f aseq] (reverse (reduce (fn [x y] (cons (f y) x)) '() aseq)))
([f aseq & bseq]
(loop [r '()
y (cons aseq bseq)]
(if(some empty? y)
(reverse (my-map (fn [x] (apply f x)) r))
(recur (cons (my-map first y) r) (my-map rest y))))))