From f294c4be83edd05686901555a457d80627873249 Mon Sep 17 00:00:00 2001 From: James Raikes Date: Sun, 1 Oct 2017 13:04:34 +0100 Subject: [PATCH 1/2] All exercises completed --- src/one_function_to_rule_them_all.clj | 59 ++++++++++++++++++--------- 1 file changed, 40 insertions(+), 19 deletions(-) diff --git a/src/one_function_to_rule_them_all.clj b/src/one_function_to_rule_them_all.clj index 6844d99..0d2aa4a 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 #(str %1 " " %2) a-seq))) (defn my-interpose [x a-seq] - [:-]) + (if (empty? a-seq) + '() + (rest (reduce #(conj % x %2) [] a-seq)))) (defn my-count [a-seq] - :-) + (reduce (fn [cnt _] (inc cnt)) 0 a-seq)) (defn my-reverse [a-seq] - [:-]) + (reduce conj () a-seq)) (defn min-max-element [a-seq] - [:-]) + (let [min-max (fn [[mn mx] elem] + (list (min mn elem) + (max mx elem))) + f (first a-seq)] + (reduce min-max [f f] a-seq))) (defn insert [sorted-seq n] - [:-]) + (sort (conj sorted-seq n))) (defn insertion-sort [a-seq] - [:-]) + (reduce insert [] a-seq)) (defn parity [a-seq] - [:-]) + (let [toggle (fn [a-set elem] (if (contains? a-set elem) + (disj a-set elem) + (conj a-set elem)))] + (reduce toggle #{} a-seq))) -(defn minus [x] - :-) +(defn minus + ([x] (- x)) + ([x y] (- x y))) -(defn count-params [x] - :-) -(defn my-* [x] - :-) +(defn count-params [& more] + (my-count more)) -(defn pred-and [x] - (fn [x] :-)) +(defn my-* [& more] + (reduce * 1 more)) -(defn my-map [f a-seq] - [:-]) \ No newline at end of file +(defn pred-and + ([] (fn [_] true)) + ([p?] p?) + ([p1? p2?] #(and (p1? %) (p2? %))) + ([p1? p2? & more] (reduce pred-and (pred-and p1? p2?) more))) + +(defn my-map + ([f a-seq] (seq (reduce #(conj % (f %2)) [] a-seq))) + ([f a-seq & more] (loop [res [] + l-seq (cons a-seq more)] + (if (some empty? l-seq) + (seq res) + (recur (conj res (apply f (my-map first l-seq))) (my-map rest l-seq)))))) From ca0c690fec1c73549d5fe94ea0f75a06e20524af Mon Sep 17 00:00:00 2001 From: James Raikes Date: Sun, 1 Oct 2017 13:04:34 +0100 Subject: [PATCH 2/2] All exercises completed --- .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..0d2aa4a 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 #(str %1 " " %2) a-seq))) (defn my-interpose [x a-seq] - [:-]) + (if (empty? a-seq) + '() + (rest (reduce #(conj % x %2) [] a-seq)))) (defn my-count [a-seq] - :-) + (reduce (fn [cnt _] (inc cnt)) 0 a-seq)) (defn my-reverse [a-seq] - [:-]) + (reduce conj () a-seq)) (defn min-max-element [a-seq] - [:-]) + (let [min-max (fn [[mn mx] elem] + (list (min mn elem) + (max mx elem))) + f (first a-seq)] + (reduce min-max [f f] a-seq))) (defn insert [sorted-seq n] - [:-]) + (sort (conj sorted-seq n))) (defn insertion-sort [a-seq] - [:-]) + (reduce insert [] a-seq)) (defn parity [a-seq] - [:-]) + (let [toggle (fn [a-set elem] (if (contains? a-set elem) + (disj a-set elem) + (conj a-set elem)))] + (reduce toggle #{} a-seq))) -(defn minus [x] - :-) +(defn minus + ([x] (- x)) + ([x y] (- x y))) -(defn count-params [x] - :-) -(defn my-* [x] - :-) +(defn count-params [& more] + (my-count more)) -(defn pred-and [x] - (fn [x] :-)) +(defn my-* [& more] + (reduce * 1 more)) -(defn my-map [f a-seq] - [:-]) \ No newline at end of file +(defn pred-and + ([] (fn [_] true)) + ([p?] p?) + ([p1? p2?] #(and (p1? %) (p2? %))) + ([p1? p2? & more] (reduce pred-and (pred-and p1? p2?) more))) + +(defn my-map + ([f a-seq] (seq (reduce #(conj % (f %2)) [] a-seq))) + ([f a-seq & more] (loop [res [] + l-seq (cons a-seq more)] + (if (some empty? l-seq) + (seq res) + (recur (conj res (apply f (my-map first l-seq))) (my-map rest l-seq))))))