Commits

tednaleid committed ef5b0cb

problem 63

Comments (0)

Files changed (1)

src/four_clojure/answered.clj

 (= (take 9 (my-iterate #(inc (mod % 3)) 1)) (take 9 (cycle [1 2 3])))
 
 
+; http://www.4clojure.com/problem/63
+; Group a Sequence
+; Given a function f and a sequence s, write a function which returns a map. The keys should be the values of f applied to each item in s. The value at each key should be a vector of corresponding items in the order they appear in s.
+; restrictions: group-by
+
+(defn my-group-by [f s]
+  (reduce (fn [m x]
+            (let [y (f x)]
+              (assoc m y (conj (get m y []) x)))) {} s))
+
+(= (my-group-by #(> % 5) #{1 3 6 8}) {false [1 3], true [6 8]})
+(= (my-group-by #(apply / %) [[1 2] [2 4] [4 6] [3 6]])
+   {1/2 [[1 2] [2 4] [3 6]], 2/3 [[4 6]]})
+(= (my-group-by count [[1] [1 2] [3] [1 2 3] [2 3]])
+   {1 [[1] [3]], 2 [[1 2] [2 3]], 3 [[1 2 3]]})
+
 ; http://www.4clojure.com/problem/
 ;
 ;
 ;
 ;
 
-; http://www.4clojure.com/problem/
-;
-;
-