Intro To Erlang

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    4 Favorites

    Intro To Erlang - Presentation Transcript

    1. Introduction to Erlang Erlang is a functional, concurrency-oriented, distributive, fault-tolerant programming language. 1
    2. Philosophy of Erlang “Erlang was designed for writing concurrent programs that \"run forever\".” - Joe Armstrong Let it fail. Have another process deal with it. Fault tolerance requires at least *two* computers. 2
    3. Erlang Mindset Lots of little tasks Tons of processes Each process does a task very well Ant Colony Ants are processes sending messages to each other 3
    4. Why Erlang? 4
    5. Erlang makes hard things easier. 5
    6. Concurrency 6
    7. 7
    8. Spawning Processes 8
    9. F = fun() -> io:format(“hi”) end, spawn(F). >> hi<0.34.0> 9
    10. spawn(io, format, [“hi”]). 10
    11. Message Passing 11
    12. 12
    13. Pid ! Message Pid ! {do_task, run_home} Pid ! 2 13
    14. receive ... end receive Pattern1 [when Guard1] -> Expressions1; Pattern2 [when Guard2] -> Expressions2 end. 14
    15. Messages are contained in a process’s inbox. receive _ -> ok end. 15
    16. Fault Tolerance 16
    17. 17
    18. 18
    19. which is why erlang was designed to be.... 19
    20. Distributive 20
    21. 21
    22. Pid ! Message where Pid exists on another node. 22
    23. Erlang Nodes Can only talk if the erlang ‘cookie’ is the same Can exist on the same machine or different machines Does have overhead, isn’t free Can have ‘C’ nodes (+ other node types that speak erlang) Communicate via Global Name Registry, Specific Pids, or by Node name 23
    24. The Basics 24
    25. Erlang is Functional 25
    26. Periods, Commas, Semicolons Oh My! People think Erlang’s syntax is ugly. It’s actually very logical. 26
    27. Periods ( . ) end everything except when Semicolons ( ; ) end clauses and Commas ( , ) separate expressions. 27
    28. calculate_area({square, Size}) -> Size * Size; calculate_area({circle, Radius}) -> Pi = 3.14, Pi * Radius * Radius; calculate_area(_Unknown) -> ok. 28
    29. case Expression of {do_task, run_home} -> Home = 123, spawn(run, home, [Home]); _ -> ok end. 29
    30. Data Types 30
    31. Integer - however big you can imagine Floats Atoms - similar to a ruby symbol, they are used to represent non-numerical constants (true, false, ok, error) Tuple - Fixed length collection of items ({one, two, three} or {one, two} etc) Lists - Stores a variable number of things ([1, 2, {one, two}, $a]) 31
    32. Missing Links... 32
    33. Strings - A string is just a list of integers in erlang. Booleans - Use atoms (true, false) Hash - Property List ( [tuple()] ) shown later 33
    34. Pattern Matching Once you pattern match, variable assigning feels dirty. 34
    35. 1 = 1 ( ok ) 1 = 2 ( error thrown bad_match ) Value = {2, 4}, {Width, Height} = {2, 4} ( ok ) [Head|Tail] = [1,2,3,4] ( ok ) 35
    36. Variable Binding or restricting infinite possibilities. 36
    37. An unbound variable can be anything it needs to be (infinite possibilities) A. 37
    38. Pattern matching an unbound variable is like restricting what it can be. A = 1. A = 2. (error bad_match) 38
    39. Tuples Single entity that holds a collection of items. 39
    40. {1, 2, 3}. {one, two}. {user_id, 2, user_name, “asceth”}. 40
    41. Lists Variable sized container of items. 41
    42. List = [1, 2, 3], NewList = List ++ [4, 5, 6]. >> [1, 2, 3, 4, 5, 6] [Head|Tail] = NewList. >> Head -> 1 >> Tail -> [2, 3, 4, 5, 6] 42
    43. [tuple()] Property Lists or semi-hashes. 43
    44. PList = [{key, value}, {key1, value1}]. case lists:keysearch(key, 1, PList) of false -> error; {value, {Key, Value}} -> Value end. 44
    45. keydelete - deletes tuple with specified key in list of tuples and returns new list keymap - returns list of tuples where Nth element is replaced be specified fun keymember - tests for existence of key keymerge - combines two tuples with tuple1 taking priority ukeymerge, ukeysort - like keymerge but removes duplicates in tuple2 45
    46. Functions Pattern matching, Function Arities & more 46
    47. Functions with the same name can exist Functions with the same name and different arity can exist Erlang tells you if a function cannot be called due to order of definement Function order is very important 47
    48. contains(Key, []) -> false; contains(Key, List) -> lists:any(fun(X) -> Key == X end, List). contains(1, [1, 2, 3]). true contains({home, 123}, [{apartment, 333}, {house, 000}]). false 48
    49. Anonymous 49
    50. contains(Key, []) -> false; contains(Key, List) -> lists:any(fun(X) -> Key == X end, List). F = fun(X, Y) -> X1 = X * 2, Y1 = Y * X, X1 + Y1 end, F(2, 2). >> 12 50
    51. Looping Tail recursion makes looping good again. 51
    52. print([]) -> ok; print([H|T]) -> io:format(“~p~n”, [H]), print(T). 52
    53. Conditionals If’s are okay, cases are cooler. 53
    54. if A == 5 -> do_something(); true -> do_something_else() end. 54
    55. case A of 0 -> first(); 2 -> second(); _ -> something() end. 55
    56. Resources http://www.erlang.org Technorati 56
    57. 57
    58. Questions? 58

    + ascethasceth, 12 months ago

    custom

    1485 views, 4 favs, 2 embeds more stats

    Introduction to Erlang presentation for the Columbi more

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 1485
      • 1441 on SlideShare
      • 44 from embeds
    • Comments 0
    • Favorites 4
    • Downloads 51
    Most viewed embeds
    • 34 views on http://colaruby.org
    • 10 views on http://colaruby.com

    more

    All embeds
    • 34 views on http://colaruby.org
    • 10 views on http://colaruby.com

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories