Created
July 7, 2014 20:20
-
-
Save palladin/def871a13f9d26c6c388 to your computer and use it in GitHub Desktop.
Clojure meetup final
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; Anything you type in here will be executed | |
;; immediately with the results shown on the | |
;; right. | |
(def data '({:name "nick" :age 20} {:name "george", :age 30} | |
{:name "giannis", :age 40} {:name "dimitris", :age 50})) | |
(defn map' [f source] | |
(fn [k] | |
(source (fn [x acc] (k (f x) acc))))) | |
(defn filter' [pred source] | |
(fn [k] | |
(source (fn [x acc] | |
(if (pred x) | |
(k x acc) | |
acc))))) | |
(defn reduce' [f acc source] | |
((source f) acc)) | |
(defn toList [source] | |
(reduce' (fn [x acc] (cons x acc)) | |
'() source)) | |
(defn build [source] | |
(fn [k] | |
(fn [acc] | |
(defn reduce'' [source] | |
(if (empty? source) | |
acc | |
(k (first source) | |
(reduce'' (rest source))))) | |
(reduce'' source)))) | |
(defn transform-name-upper [source] | |
(toList (map'(fn [first'] (.toUpperCase (:name first'))) | |
(build source)))) | |
(defn transform-inc-age [source] | |
(map'(fn [first'] (inc (:age first'))) | |
source)) | |
(transform-name-upper data) | |
(transform-inc-age data) | |
(defn find-by-age [source age] | |
(filter' (fn [first'] (= (:age first') age)) | |
source)) | |
(defn find-by-name [source name] | |
(filter' (fn [first'] (= (:name first') name)) | |
source)) | |
(find-by-age data 30) | |
(find-by-name data "nick") | |
;;(f (f ..... acc)) | |
;; (f.... (f (f acc) | |
(defn length [source] | |
(reduce' (fn [x acc] (+ 1 acc)) 0 source)) | |
(defn sum-age [source] | |
(reduce' (fn [x acc] (+ (:age x) acc)) 0 source)) | |
(length data) | |
(sum-age data) | |
(defn sum-inc-age [source] | |
(reduce' + 0 | |
(map' (fn [x] (inc (:age x))) source))) | |
(sum-inc-age data) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment