Dmitry Groshev @lambdadmitry
JS:
Foo.Bar = (function() {...
baz = _.map(..., function(){ ... });
})(...);
CLJS:
(map #(...) ...)
Структурное редактирование
rainbow delimeters!
Структурное выделение
Структурное удаление
LISP как JAVA: многократно приятнее с правильным редактором
скобочки вызывают зависимость
not your mom's Elm
CLJS гарантирован рост из-за CLJ
(+ 1 2)
:foo
(list 1 2 3)
[1 2 3]
{:a 1, :b 2}
#{1 2 3}
#js [1 2 3]
#js {:a 1 :b 2}
~1-3x immutability overhead
(def pony {:name "Rainbow Dash"
:additional {:color "rainbow"
:friends ...}})
(pony :name) ; => "Rainbow Dash"
(:name pony) ; => "Rainbow Dash"
(map :name ponies) ; => (Name1 Name2 ...)
(get-in pony [:additional :friends]) ; => ...
(map inc (for [q elements
p other-elements]
(+ q p)))
(loop [x 1]
(when (< x 10)
(prn x)
(recur (inc x))))
(defn fizzbuzz [x]
(cond (zero? (mod x 15)) "FizzBuzz"
(zero? (mod x 5)) "Buzz"
(zero? (mod x 3)) "Fizz"
:else x))
(doseq [x (range 1 101)]
(prn (fizzbuzz x)))
Прелестно!
function fd(a) {
return (0 === (a % 15 + 15) % 15 ? "FizzBuzz" :
0 === (a % 5 + 5) % 5 ? "Buzz" :
0 === (a % 3 + 3) % 3 ? "Fizz" : t ? a : null);
}
for (var gd = G(Yc.a(1, 101)), hd = null, id = 0, jd = 0;;) {
if (jd < id) {
var kd = hd.G(null, jd);
ed.j(N([fd(kd)], 0));
jd += 1;
} else {
...
}
};
15k gzipped
"Chambered" by David Nolen
~250LOC CLJS, 200–400LOC JS
(defrecord Puppies [...])
(defrecord Ukraine [...])
(defprotocol Likeable
(like [thing])
(hug [thing times]))
(extend-type Puppies
Likeable
(like [this] ...)
(hug [this times] ...))
(extend-protocol Likeable
Ukraine
(like [this] ...)
(hug [this times] ...))
Look ma, in my browser!
(->> (fetch "some_url")
(filter valid?)
(map #(assoc % :received (current-time)))
(into #{}))
(sel1 :body) ; => document.body (15x jQuery)
(sel1 :#my-id) ; => document.getElementById("my-id") (7x jQuery)
(sel ".c1, .c2") ; => «обычный» диспатч (~jQuery)
Compile-time компиляция шаблонов:
(em/deftemplate template :compiled "/templates/template1.html" [some-data]
["#heading1"] (ef/content "fruit")
["thead tr > *:last-child"] (ef/content "quantity")
["tbody"] (ef/content (some-function some-data)))
Clojure:
(ann my-weird-plus [Number -> String -> Number])
(defn my-weird-plus [a b]
(let [b-int (Integer/parseInt b)]
(+ a b)))
Опциональная runtime проверка типов! CLJS-порт в разработке
Снова FizzBuzz:
(doseq [n (range 1 101)]
(println
(match [(mod n 3) (mod n 5)]
[0 0] "FizzBuzz"
[0 _] "Fizz"
[_ 0] "Buzz"
:else n)))
Компилируется в эффективный код
{:on-message (fn [msg]
(go (>! first-chan
(decode msg))))}
...
(go
(>! final-chan
(+ (<! first-chan)
(<! second-chan))))
Quiz: что проще оптимизировать?
Слайды: si14.github.io/itlifeconf-2014-slides
Твиттер: @lambdadmitry
Почта: lambdadmitry@gmail.com