From 4d3a75da80a054999fddea12dbad7cf44f6aa61f Mon Sep 17 00:00:00 2001 From: ttiira Date: Wed, 26 Jul 2017 17:04:51 +0300 Subject: [PATCH 1/3] work --- src/one_function_to_rule_them_all.clj | 68 ++++++++++++++++++++------- 1 file changed, 50 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..623c14c 100644 --- a/src/one_function_to_rule_them_all.clj +++ b/src/one_function_to_rule_them_all.clj @@ -1,43 +1,75 @@ (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 [str1 str2] (str str1 " " str2)) + a-seq))) (defn my-interpose [x a-seq] - [:-]) + (if (empty? (rest a-seq)) + a-seq + (rest (reduce (fn [acc elem] (conj (conj acc x) + elem)) + [] + a-seq)))) (defn my-count [a-seq] - :-) + (reduce (fn [cnt elem] (inc cnt)) + 0 + a-seq)) (defn my-reverse [a-seq] - [:-]) + (reduce (fn [acc e] (cons e acc)) + () + a-seq)) (defn min-max-element [a-seq] - [:-]) + (let [min-max (fn [min-max-vec elem] + [(min elem (first min-max-vec)) + (max elem (second min-max-vec))]) + fst (first a-seq)] + (reduce min-max [fst fst] a-seq))) (defn insert [sorted-seq n] - [:-]) + (loop [init (take-while #(< % n) sorted-seq) + tail (drop-while #(< % n) sorted-seq)] + (concat init + (cons n tail)))) (defn insertion-sort [a-seq] - [:-]) + (reduce insert [] a-seq)) + +(defn toggle [a-set elem] + (if (contains? a-set elem) + (disj a-set elem) + (conj a-set elem))) (defn parity [a-seq] - [:-]) + (reduce toggle #{} a-seq)) -(defn minus [x] - :-) +(defn minus + ([x] (- 0 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] (* x y)) + ([x y & more] (reduce * (* x y) more))) -(defn pred-and [x] - (fn [x] :-)) +(defn pred-and + ([] (fn [x] true)) + ([pred] pred) + ([p1 p2] (fn [x] (and (p1 x) (p2 x)))) + ([p1 p2 & more] (fn [x] (and (pred-and p1 p2) + (every? true? (map #(% x) more)))))) (defn my-map [f a-seq] - [:-]) \ No newline at end of file + [:-]) From 97980929625d1e90d0379cc6bce2bf8152cf4a44 Mon Sep 17 00:00:00 2001 From: ttiira Date: Mon, 31 Jul 2017 18:17:37 +0300 Subject: [PATCH 2/3] work --- src/one_function_to_rule_them_all.clj | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/one_function_to_rule_them_all.clj b/src/one_function_to_rule_them_all.clj index 623c14c..684730f 100644 --- a/src/one_function_to_rule_them_all.clj +++ b/src/one_function_to_rule_them_all.clj @@ -68,8 +68,28 @@ ([] (fn [x] true)) ([pred] pred) ([p1 p2] (fn [x] (and (p1 x) (p2 x)))) - ([p1 p2 & more] (fn [x] (and (pred-and p1 p2) + ([p1 p2 & more] (fn [x] (and ((pred-and p1 p2) x) (every? true? (map #(% x) more)))))) -(defn my-map [f a-seq] - [:-]) +(defn head-taker [xss] + (cond (empty? xss) () + (some empty? xss) () + :else (cons (first (first xss)) + (head-taker (rest xss))))) + +(defn removed-heads [xss] + (cond (empty? xss) () + (some empty? xss) () + :else (cons (rest (first xss)) + (removed-heads (rest xss))))) + +(defn my-map + ([f xs] (if (empty? xs) + xs + (cons (f (first xs)) + (my-map f (rest xs))))) + ([f xs & more] (let [all (cons xs more)] + (if (some empty? all) + () + (cons (apply f (head-taker all)) + (my-map f (removed-heads all))))))) From a1c92d5fd77a75c9f9d626174d07f724a81756c2 Mon Sep 17 00:00:00 2001 From: ttiira Date: Wed, 2 Aug 2017 17:47:44 +0300 Subject: [PATCH 3/3] done --- src/one_function_to_rule_them_all.clj | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/one_function_to_rule_them_all.clj b/src/one_function_to_rule_them_all.clj index 684730f..8488bc5 100644 --- a/src/one_function_to_rule_them_all.clj +++ b/src/one_function_to_rule_them_all.clj @@ -84,12 +84,18 @@ (removed-heads (rest xss))))) (defn my-map - ([f xs] (if (empty? xs) - xs - (cons (f (first xs)) - (my-map f (rest xs))))) - ([f xs & more] (let [all (cons xs more)] - (if (some empty? all) - () - (cons (apply f (head-taker all)) - (my-map f (removed-heads all))))))) + ([f xs] (loop [ys xs + acc []] + (if (empty? ys) + acc + (recur (rest ys) + (conj acc (f (first ys))))))) + ([f xs & more] (loop [ys xs + tail more + acc []] + (if (empty? ys) + acc + (recur (rest ys) + (removed-heads tail) + (conj acc + (apply f (head-taker (cons ys tail)))))))))