erlang/n2o at knpmeetup 2015
TRANSCRIPT
Disclaimer
Marketing
FP makes irreversible changes in your brain, and makes developer better.
HOTCODE13 MAXIM SOKHATSKIY
Hey, did you heard about FP?
My Books
Instant Enlightenment
X = 123
Immutable
No shared memory ParadiseShared memory JavaShared memory and state HELL
Immutable
Message passingWe need MQ
No shared memory
Examples of Apps
Examples of Apps
Examples of Apps
Examples of Apps
Examples of AppsRabbitMQRiak, CouchDB, Hibari, KAI, LeoFS, MnesiaejabberdcowboyWings 3D
Soft realtime
qsort([]) -> [];qsort([x|Xs]) -> qsort([Y || Y<-Xs, Y <= X]) ++ [x] ++ qsort([Y || Y<-Xs, Y > X]).
QuickSort
Theory
BasisInteger 42 Float 4.2 aka doubleAtom okBinary <<"Erlang-powa">>
Reference #Ref<0.0.0.29>Pid <0.0.42>Port #Port<0.42>Fun #Fun<erl_eval.6.82930912>
Basis 2List [<<42,1,0,90>>, 1, ok]
Tuple {<0.0.16>, 107, 42, ["madness", true]}
we can force lists type and typify turplesnamed tuples =:= records
Pattern MatchingX = 1.
[H | T] = [1, 2, 3, 4].
Y = 2.Y = 3. %% err there
{A, B, C} = {ok, X, [“hello”, “knp”, “symfony”]}
Practice
Small stuff on Erlangfib(0) -> 0;fib(1) -> 1;fib(N) -> fib(N - 1) + fib(N - 2).
Small stuff on Erlangtail_fib(N)-> tail_fib_acc(N, 0, 0, 0).
tail_fib_acc(N, Counter, Prev, PrevPrev) -> case Counter of N -> Prev + PrevPrev; 0 -> tail_fib(N, Counter + 1, 0, 0); 1 -> tail_fib(N, Counter + 1, 1, 0); _ -> tail_fib(N, Counter + 1, Prev + PrevPrev, Prev) end.
N2O
DEMO TIME
N2O, Idiomatic chat
-module ( chat ).event (chat) -> wf:send (chat, wf:q (message) ),
body () -> wf:async ( fun() -> loop() end, chat ), [ #panel { id=history }, #textbox { id=message }, #button { postback=chat } ].
loop () -> receive Message -> wf:insert_bottom ( history, #span { text=Message } ), wf:flush () end, loop ().
Linkshttps://github.com/5HT/n2o
http://slides.com/maximsokhatsky/n2o
https://www.youtube.com/watch?v=FKPJ3C-UZJw
https://github.com/synrc/kvs
https://synrc.com/