Last active
November 11, 2015 15:04
-
-
Save Deraen/3cdef8b03da56cef14c6 to your computer and use it in GitHub Desktop.
Futurice training, 5.6.2014
This file contains 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
(ns working-with-data | |
(:require [working-with-data.data :refer [participants]])) | |
;; Futu training, 5.6.2014 | |
;; RECAP | |
(def a {:name "Juho"}) | |
;; assoc - return a new map | |
(assoc a :age 24) | |
;; => {:age 24, :name "Juho"} | |
a | |
;; => {:name "Juho"}) - this is unchanged | |
;; dissoc - return a new map | |
(dissoc a :sdfsdfds) | |
;; => {:name "Juho"} | |
;; map - returns a new value for every element of collection | |
(map :prog-years participants) | |
;; => (10.0 8.0 4.0 4.0 10.0 10.0 9.0 10.0 11.0 20.0 20.0 5.0 nil 10.0 10.0 15.0 16.0 10.0 15.0 10.0 14.0 15.0) | |
(filter :scala participants) | |
;; reduce | |
;; Ugly version | |
(reduce (fn [acc p] | |
(+ acc (or (:prog-years p) 0))) | |
0 | |
participants | |
) | |
;; Better | |
(->> participants | |
(map :prog-years) | |
(remove nil?) | |
(reduce +)) | |
;; Pretty | |
(->> participants | |
(keep :prog-years) | |
(reduce +)) | |
;; destructuring | |
;; - head tail second | |
;; - maps | |
;; - inner maps | |
(defn head [[f]] | |
f) | |
(head [1 2 3 4]) | |
;; => 1 | |
(defn second [[_ s]] | |
s) | |
(second [1 2 3 4]) | |
;; => 2 | |
(defn tail [[_ & rest]] | |
rest) | |
(tail [1 2 3 4]) | |
;; => (2 3 4) | |
(let [{:keys [d] :or {d 1337}} {:a 12 :b 34 :c 42}] | |
d) | |
;; => 1337 | |
(let [[[f] second] [[1 2 3] 4 5 6]] | |
{:foo f | |
:bar second}) | |
;; => {:foo 1, :bar 4} | |
(let [{{a :foo} :bar} {:bar {:foo 5}}] | |
a) | |
;; => 5 | |
(macroexpand-1 '(let [[x y] (f)])) | |
;; => (let* [vec__3942 (f) x (clojure.core/nth vec__3942 0 nil) y (clojure.core/nth vec__3942 1 nil)]) | |
;; => (let* [vec__3936 col x (clojure.core/nth vec__3936 0 nil) y (clojure.core/nth vec__3936 1 nil)]) | |
;; => (let* [vec__3933 col x (clojure.core/nth vec__3933 0 nil)]) | |
(macroexpand-1 '(let [{a :a} m])) | |
;; => (let* [map__3939 m map__3939 (if (clojure.core/seq? map__3939) (clojure.lang.PersistentHashMap/create (clojure.core/seq map__3939)) map__3939) a (clojure.core/get map__3939 :a)]) | |
;; A usecase | |
(fact "echo echos json" | |
(let [beer {:beer "olvi" :tags ["oldschool"]} | |
{:keys [headers status body]} (post-json "/echo" beer) ;; returns a ring response | |
{:strs [content-type]} headers] | |
content-type => "image/png" | |
status => 200 | |
body => beer))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment