Created
February 9, 2016 19:46
-
-
Save fgui/c5fe855840ea6d439000 to your computer and use it in GitHub Desktop.
meet clojure
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
(ns ic.user) | |
;; funciones | |
;; (function param-1 param-2 ... param-n) | |
(inc 3) | |
(str "hola" "mundo") | |
;;(1 2 3) | |
;; los operadores matemáticos son *funciones* | |
+ | |
(+ 2 3) ;; 2 + 3 | |
(- 4 3) ;; 4 - 3 | |
(+ 4 3 1) ;; 4 + 3 + 1 | |
(+ 1) | |
(+) | |
(*) | |
(< 1 2) | |
(< 1 2 3 4) ;; (1 < 2) and (2 < 3) and (3 < 4) | |
(* 4 (+ 2 3)) ;; 20 | |
(+ (* 4 2) 3) ;; 11 | |
;; en otros lenguages podria ser | |
;; 4 * 2 + 3 = 11 tiene que saber las reglas de precedencía de los operadores | |
;; (4 * 2) + 3 = 11 | |
;; 4 * (2 + 3) = 20 | |
;; evaluación | |
(+ | |
4 | |
5 | |
) | |
(+ | |
(* | |
2 | |
3 | |
) | |
5) | |
;; Tipos | |
;; strings | |
"hello world" | |
;; números | |
42 | |
42.45 | |
;; booleans | |
true | |
false | |
;; nothing, null | |
nil | |
;; keywords | |
:keyword | |
:color | |
:key1 | |
;; Colecciones | |
;; Vectors | |
[] | |
[1 2 3] | |
[1 "hello" 34] | |
;; Maps | |
{} | |
{:key1 "value1" :key2 "value2"} | |
;; Sets | |
#{} | |
#{1 2 3 4} | |
;; they can be nested | |
[1 2 [3 4] {:one 1}] | |
;; Lists | |
'() | |
'(1 2 3) | |
;; (1 2 3) | |
;; definir variables | |
(def answer-to-everything 42) | |
answer-to-everything | |
;; variables locales | |
(def pi 3.14) | |
(let [pi 3] | |
pi) | |
pi | |
;; definir funciones | |
(defn square | |
"eleva al cuadrado" | |
[x] | |
(* x x)) | |
(square 2) | |
;; las funciones son valores | |
;; se pueden guardar | |
(def math-fns [square * +]) | |
(first math-fns) | |
((first math-fns) 2) | |
;; se pueden pasar a otras funciones | |
(map square [1 2 3 4]) | |
;; podemos devolver funciones | |
(defn apply-twice [f] | |
(fn [x] (f (f x))) | |
) | |
(def elevar-a-4 (apply-twice square)) | |
(elevar-a-4 2) | |
;; A estas funciones que acabamos de ver, | |
;; que crean y/o reciben otras funciones, | |
;; se las llama *funciones de alto orden* | |
;; Las funciones de alto orden se usan | |
;; mucho en clojure, | |
;; por ejemplo | |
;; map | |
("Higher-order functions | |
frequently used in clojure | |
- map | |
[a a a] fun -> [(fun a) (fun a) (fun a)] | |
- filter | |
[a b a] a? -> [a a] | |
- reduce | |
[a b c] fun -> (fun (fun a b) c) | |
- iterate | |
fun a -> [(fun a) (fun (fun a)) ...] | |
- etc") | |
(comment | |
(map square [1 2 3 4]) | |
(filter even? [1 2 3 4]) | |
(defn factorial [n] (reduce * (range 1N n))) | |
(factorial 10000) | |
(reduce #(update %1 %2 (fnil inc 0)) {} "agatta") | |
(frequencies "agatta") | |
(defn factorial [n] (reduce * (range 1N n))) | |
;; fib-n = fib-n-2 + fib-n-1 | |
(defn next-fib [[n-2 n-1]] [n-1 (+ n-1 n-2)]) | |
(take 10 (map first (iterate next-fib [0 1]))) | |
(take 10 (->> [0 1] (iterate next-fib) (map first))) | |
(def fibonaccis (map first (iterate next-fib [0N 1N])) ) | |
(first (drop 3 fibbonaccis)) | |
) | |
(add-slide "Flow control") | |
(comment | |
(if (zero? 0) "cero" "otro") | |
) | |
(add-slide "Tell me the truth: | |
- false nil | |
- true everything else") | |
(comment | |
(if true true false) | |
(if nil true false) | |
(if (or "" () {} :key) true false)) | |
(add-slide "Higher-order functions are fine, | |
but where is my loop? | |
recur-it.") | |
(comment | |
(loop [count 0 coll '(1 2 3)] | |
(if (empty? coll) | |
count | |
(recur (inc count) (rest coll)))) | |
(defn factorial [n] | |
(if (zero? n) 1N | |
(* n (factorial (dec n))))) | |
(factorial 1000) | |
(factorial 10000) | |
;;stackoverflow | |
(defn factorial-helper [acc n] | |
(if (zero? n) acc | |
(recur (* acc n) (dec n)))) | |
;; with partial | |
(def factorial4 (partial factorial-helper 1N)) | |
(defn factorial5 [n] | |
(loop [acc 1N fact n] | |
(if (zero? fact) | |
acc | |
(recur (* acc fact) (dec fact))))) | |
) | |
(add-slide "Seq | |
- first | |
- rest | |
- empty? | |
- seq | |
- cons | |
Collections | |
- conj | |
- assoc | |
- update") | |
(comment | |
(def ex-vector [:one 2 :three]) | |
(first (range)) | |
(second (range)) | |
(take 10 (rest (range))) | |
(rest ()) | |
(rest []) | |
(cons 1 [2 3 4]) | |
(conj [1 2 3] 4) | |
) | |
(add-slide "Teaching to fish | |
- doc, find-doc, source | |
- cheatsheet, conj.io") | |
(add-slide "Lots more to learn about: | |
- namespaces | |
- destructuring | |
- metadata | |
- managing state, concurrency | |
- lazy-sequence | |
- polymorphism (multimethods) | |
- protocols, deftype, defrecord | |
- macros | |
- java/javascript interop | |
- cljc | |
- ...") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment