##55app
(= (__ [1 1 2 3 2 1 1]) {1 4, 2 2, 3 1}) (= (__ [:b :a :b :a :b]) {:a 2, :b 3}) #(into {} (map (fn [[akey aval]] [akey (count aval)]) (group-by identity %))) ;;others (defn map-frequencies "Map occurrences of numbers. Should not use frequencies function." [xs] (reduce (fn [m i] (assoc m i (inc (m i 0)))) {} xs))
##56ide
(= (__ [1 2 1 3 1 2 4]) [1 2 3 4]) (= (__ [:a :a :b :b :c :c]) [:a :b :c]) #(reduce (fn [xs item] (if (some (partial = item) xs) xs (conj xs item) )) [] %)
##58oop
(= "HELLO" ((__ #(.toUpperCase %) #(apply str %) take) 5 "hello world")) (= [3 2 1] ((__ rest reverse) [1 2 3 4])) #(fn [& xs] (loop [[fnitem & fns] (reverse %&) rs xs] (if fnitem (recur fns (list (apply fnitem rs))) (first rs)))) ;;other #(fn [& args] (first (reduce (fn [args fnitem] (list (apply fnitem args))) args (reverse %&))))
##59 Take a set of functions and return a new function that takes a variable number of arguments and returns a sequence containing the result of applying each function left-to-right to the argument listrest
(= ["HELLO" 5] ((__ #(.toUpperCase %) count) "hello")) (= [2 6 4] ((__ :a :c :b) {:a 2, :b 4, :c 6, :d 8 :e 10})) (fn ([] []) ([a b] (fn [& args] [(apply a args) (apply b args)])) ([a b c] (fn [& args] [(apply a args) (apply b args) (apply c args)])) ) ;;or (fn [& fns] #(map (fn [fnitem] (apply fnitem %&)) fns))
##60 reductionscode
;;Write a function which behaves like reduce, but returns each intermediate value of the reduction. Your function must accept either two or three arguments, and the return sequence must be lazy. (= (take 5 (__ + (range))) [0 1 3 6 10]) (= (__ conj [1] [2 3 4]) [[1] [1 2] [1 2 3] [1 2 3 4]]) (defn my-reductions ([f xs] (my-reductions f (first xs) (rest xs))) ([f init xs] (cons init (lazy-seq (when-not (empty? xs) (my-reductions f (apply f (list init (first xs))) (rest xs)))))))
##66最大公約數three
(fn greatest-common-divisor [a b] (loop [gcd (min a b)] (if (= 0 (rem a gcd) (rem b gcd)) gcd (recur (dec gcd)))))
##61 zipmapip
(= (__ [:a :b :c] [1 2 3]) {:a 1, :b 2, :c 3}) (fn [xs1 xs2] (into {} (map #(identity [% %2]) xs1 xs2))) ;;其它實現 (fn [xs1 xs2] (apply hash-map (interleave xs1 xs2)))
##62 implement iteraterci
(fn my-iterate [f init] (lazy-seq (cons init (my-iterate f (f init)))))
##63 implement group-byrem
(fn [f col] (reduce (fn [m x] (assoc m (f x) (conj (m (f x) []) x))) {} col))