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

Fix #527

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Fix #527

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
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: clojure
lein: lein2
script: lein2 midje :config .midje-grading-config.clj
lein: lein
script: lein midje :config .midje-grading-config.clj
jdk:
- openjdk7
notifications:
Expand Down
81 changes: 62 additions & 19 deletions src/one_function_to_rule_them_all.clj
Original file line number Diff line number Diff line change
@@ -1,43 +1,86 @@
(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 [acc elem] (str acc " " elem)) a-seq)))

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

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

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

(defn min-max-element [a-seq]
[:-])
(reduce (fn [[mn mx] elem] [(min mn elem) (max mx elem)])
[(first a-seq) (first a-seq)]
a-seq))

(defn insert [sorted-seq n]
[:-])
(loop [acc []
remaining sorted-seq]
(cond
(empty? remaining)
(conj acc n)
(< n (first remaining))
(concat acc (list n) remaining)
:else
(recur (conj acc (first remaining)) (rest remaining)))))

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

(defn parity [a-seq]
[:-])
(reduce (fn [counts elem]
(if (contains? counts elem)
(disj counts elem)
(conj counts elem)))
#{}
a-seq))

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

(defn count-params [x]
:-)
(defn count-params
[& args] (count args))

(defn my-* [x]
:-)
(defn my-*
([] 1)
([x] x)
([x y] (* x y))
([x y & zs] (reduce * (concat [x y] zs))))

(defn pred-and [x]
(fn [x] :-))
(defn pred-and
([] (fn [_] true))
([p] p)
([p1 p2] (fn [x] (and (p1 x) (p2 x))))
([p1 p2 & pn]
(fn [x]
(reduce
(fn [bool p] (and bool (p x)))
(concat [p1 p2] pn)))))

(defn my-map [f a-seq]
[:-])
(defn my-map
([f a-seq]
(reduce (fn [processed elem]
(conj processed (f elem)))
[]
a-seq))
([f first-seq & other-seqs]
(let [seqs (concat [first-seq] other-seqs)]
(reduce (fn [processed index]
(conj processed (apply f (my-map (fn [x] (nth x index)) seqs))))
[]
(range (apply min (my-map count seqs)))))))