From 57456fabcf9bdf5328a38ac9a411f51c4b0f4b90 Mon Sep 17 00:00:00 2001 From: rkelkka Date: Sun, 2 Dec 2018 16:06:32 +0200 Subject: [PATCH 1/3] Fix travis build --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 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: From 49bca10d90da4ababed5fd3abd4a276283852f17 Mon Sep 17 00:00:00 2001 From: rkelkka Date: Sat, 15 Dec 2018 22:14:45 +0200 Subject: [PATCH 2/3] All but encore done --- src/one_function_to_rule_them_all.clj | 92 +++++++++++++++++++++------ 1 file changed, 74 insertions(+), 18 deletions(-) diff --git a/src/one_function_to_rule_them_all.clj b/src/one_function_to_rule_them_all.clj index 6844d99..5a7856b 100644 --- a/src/one_function_to_rule_them_all.clj +++ b/src/one_function_to_rule_them_all.clj @@ -1,43 +1,99 @@ (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) + () + (rest (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] - [:-]) + (let [min-max-helper (fn [acc elem] + (if (empty? acc) + (vector elem, elem) + (vector (min (first acc) elem) (max (second acc) elem))))] + (reduce min-max-helper [] a-seq))) ;[] is empty vector (defn insert [sorted-seq n] - [:-]) + (loop [s sorted-seq + acc [n]] + (if (= 0 (count s)) + acc + (let [new-acc + (if (< n (first s)) + (conj acc (first s)) + (conj (vec (butlast acc)) (first s) n))] ;vec drops nil, vector to make conj append instead of prepend + (recur (rest s) new-acc))))) + (defn insertion-sort [a-seq] - [:-]) + (reduce insert [] a-seq)) + +(defn my-toggle [a-seq elem] + (if (contains? a-seq elem) + (disj a-seq elem) + (conj a-seq elem))) (defn parity [a-seq] - [:-]) + (reduce my-toggle #{} a-seq));#{} apparently empty set -(defn minus [x] - :-) -(defn count-params [x] - :-) +(defn minus + ([x] (* -1 x)) + ([x y] (- x y))) -(defn my-* [x] - :-) -(defn pred-and [x] - (fn [x] :-)) +(defn count-params + ([] 0) + ([x] 1) + ([x y] 2) + ([x y & more] + (+ 2 (count more)))) +;this works too +(defn count-params-alternative + ([& more] (count more))) + +(defn my-* + ([] 1) + ([x] x) + ([x y] (* x y)) + ([x y & more] (reduce * (* x y) more))) + + +;from predicates ex +;(defn pred-and [pred1 pred2] +; (fn [x] (and (pred1 x) (pred2 x)))) + + +; If no parameters are given, return a predicate that always returns true. +; If only one predicate p is given, return p. +; If two predicates are given, return a predicate that returns true if both of them return true and false otherwise. +; If more than two predicates are given, return a predicate that returns true only if all of the predicates return true and false otherwise. +(defn pred-and + ([] (fn [x] true)) + ([p] (fn [x] (p x))) + ([p1 p2] (fn [x] (and (p1 x) (p2 x)))) + ([p1 p2 & more] + (reduce pred-and (fn [x] (and (p1 x) (p2 x))) more))) + + +;(defn my-map [f seq-1 seq-2] +; (if (and (empty? seq-2) (empty? seq-2)) +; () +; (cons (f (first seq-1) (first seq-2)) (my-map f (rest seq-1) (rest seq-2))))) +; EXTRA (defn my-map [f a-seq] - [:-]) \ No newline at end of file + [:-]) From 80989607b3020bdd2f9e11a4601bfe6ea61d752a Mon Sep 17 00:00:00 2001 From: rkelkka Date: Sun, 16 Dec 2018 13:05:38 +0200 Subject: [PATCH 3/3] Encore my-map --- src/one_function_to_rule_them_all.clj | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/one_function_to_rule_them_all.clj b/src/one_function_to_rule_them_all.clj index 5a7856b..5e4e35a 100644 --- a/src/one_function_to_rule_them_all.clj +++ b/src/one_function_to_rule_them_all.clj @@ -90,10 +90,14 @@ (reduce pred-and (fn [x] (and (p1 x) (p2 x))) more))) -;(defn my-map [f seq-1 seq-2] -; (if (and (empty? seq-2) (empty? seq-2)) -; () -; (cons (f (first seq-1) (first seq-2)) (my-map f (rest seq-1) (rest seq-2))))) -; EXTRA -(defn my-map [f a-seq] - [:-]) +; Encore +(defn my-map + ([f a-seq] + (reduce (fn [acc elem] (conj acc (f elem))) [] a-seq)) ;not again it is important to use vector as opposed to list as it affects whether conj prepends or appends + ([f a-seq & more-seqs] + (let [all-seqs (cons a-seq more-seqs)] + ;prepare the input so that result is first seq contains the first item in each coll, then the second etc. + ;partition then chunks the outcome back to as many sequences there were in the input + ; ([1 2 3] [1 2 3] [1 2 3]) => ((1 1 1) (2 2 2) (3 3 3)) + (let [interleaved-seqs (partition (count all-seqs) (apply interleave all-seqs))] + (reduce (fn [acc s] (conj acc (apply f s))) [] interleaved-seqs)))))