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/
-;
-;
-
 
 
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.