From a3773540ed16d57dc9ceabdbc7544a4607f1c45a Mon Sep 17 00:00:00 2001 From: Stephanie Clay Date: Sun, 30 Dec 2018 12:54:25 -0600 Subject: [PATCH] submission --- .travis.yml | 4 +- src/one_function_to_rule_them_all.clj | 59 ++++++++++++++++++--------- 2 files changed, 42 insertions(+), 21 deletions(-) diff --git a/.travis.yml b/.travis.yml index 455f3c0..45c29f6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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: diff --git a/src/one_function_to_rule_them_all.clj b/src/one_function_to_rule_them_all.clj index 6844d99..bf41709 100644 --- a/src/one_function_to_rule_them_all.clj +++ b/src/one_function_to_rule_them_all.clj @@ -1,43 +1,64 @@ (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 y] (str x " " y)) a-seq))) (defn my-interpose [x a-seq] - [:-]) + (let [interposer (fn [coll elem] (if (empty? coll) [elem] (conj (conj coll x) elem)))] + (reduce interposer () a-seq))) (defn my-count [a-seq] - :-) + (let [counter (fn [acc e] (inc acc))] + (reduce counter 0 a-seq))) (defn my-reverse [a-seq] - [:-]) + (let [reverser (fn [acc e] (cons e acc))] + (reduce reverser [] a-seq))) (defn min-max-element [a-seq] - [:-]) + (let [minmax (fn [[mn mx] elem] [(min elem mn) (max elem mx)]) + f (first a-seq)] + (reduce minmax [f f] a-seq))) (defn insert [sorted-seq n] - [:-]) + (cond + (empty? sorted-seq) [n] + (< n (first sorted-seq)) (cons n sorted-seq) + :else (cons (first sorted-seq) (insert (rest sorted-seq) n)))) (defn insertion-sort [a-seq] - [:-]) + (reduce insert [] a-seq)) (defn parity [a-seq] - [:-]) + (let [toggle (fn [acc elem] (if (contains? acc elem) (disj acc elem) (conj acc elem)))] + (reduce toggle #{} a-seq))) -(defn minus [x] - :-) +(defn minus + ([x] (- x)) + ([x y] (- x y))) -(defn count-params [x] - :-) +(defn count-params + ([& more] (count more))) -(defn my-* [x] - :-) +(defn my-* + ([] 1) + ([x] x) + ([x y & more] (reduce * (* x y) more))) -(defn pred-and [x] - (fn [x] :-)) +(defn pred-and + ([] (fn [x] true)) + ([p] p) + ([p q & more] (let [reducer (fn [p q] (fn [x] (and (p x) (q x))))] + (reduce reducer (reducer p q) more)))) + +(defn my-map [f & seqs] + (if (empty? (first seqs)) + [] + (let [f-on-seqs (fn [f seqs] (reduce (fn [acc elem] (conj acc (f elem))) [] seqs))] + (cons (apply f (f-on-seqs first seqs)) (apply my-map f (f-on-seqs rest seqs)))))) -(defn my-map [f a-seq] - [:-]) \ No newline at end of file