Intro To Erlang

6,343 views

Published on

Introduction to Erlang presentation for the Columbia Ruby Group.

Published in: Technology, News & Politics

Intro To Erlang

  1. 1. Introduction to Erlang Erlang is a functional, concurrency-oriented, distributive, fault-tolerant programming language. 1
  2. 2. Philosophy of Erlang “Erlang was designed for writing concurrent programs that quot;run foreverquot;.” - Joe Armstrong Let it fail. Have another process deal with it. Fault tolerance requires at least *two* computers. 2
  3. 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. 4. Why Erlang? 4
  5. 5. Erlang makes hard things easier. 5
  6. 6. Concurrency 6
  7. 7. 7
  8. 8. Spawning Processes 8
  9. 9. F = fun() -> io:format(“hi”) end, spawn(F). >> hi<0.34.0> 9
  10. 10. spawn(io, format, [“hi”]). 10
  11. 11. Message Passing 11
  12. 12. 12
  13. 13. Pid ! Message Pid ! {do_task, run_home} Pid ! 2 13
  14. 14. receive ... end receive Pattern1 [when Guard1] -> Expressions1; Pattern2 [when Guard2] -> Expressions2 end. 14
  15. 15. Messages are contained in a process’s inbox. receive _ -> ok end. 15
  16. 16. Fault Tolerance 16
  17. 17. 17
  18. 18. 18
  19. 19. which is why erlang was designed to be.... 19
  20. 20. Distributive 20
  21. 21. 21
  22. 22. Pid ! Message where Pid exists on another node. 22
  23. 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. 24. The Basics 24
  25. 25. Erlang is Functional 25
  26. 26. Periods, Commas, Semicolons Oh My! People think Erlang’s syntax is ugly. It’s actually very logical. 26
  27. 27. Periods ( . ) end everything except when Semicolons ( ; ) end clauses and Commas ( , ) separate expressions. 27
  28. 28. calculate_area({square, Size}) -> Size * Size; calculate_area({circle, Radius}) -> Pi = 3.14, Pi * Radius * Radius; calculate_area(_Unknown) -> ok. 28
  29. 29. case Expression of {do_task, run_home} -> Home = 123, spawn(run, home, [Home]); _ -> ok end. 29
  30. 30. Data Types 30
  31. 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. 32. Missing Links... 32
  33. 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. 34. Pattern Matching Once you pattern match, variable assigning feels dirty. 34
  35. 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. 36. Variable Binding or restricting infinite possibilities. 36
  37. 37. An unbound variable can be anything it needs to be (infinite possibilities) A. 37
  38. 38. Pattern matching an unbound variable is like restricting what it can be. A = 1. A = 2. (error bad_match) 38
  39. 39. Tuples Single entity that holds a collection of items. 39
  40. 40. {1, 2, 3}. {one, two}. {user_id, 2, user_name, “asceth”}. 40
  41. 41. Lists Variable sized container of items. 41
  42. 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. 43. [tuple()] Property Lists or semi-hashes. 43
  44. 44. PList = [{key, value}, {key1, value1}]. case lists:keysearch(key, 1, PList) of false -> error; {value, {Key, Value}} -> Value end. 44
  45. 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. 46. Functions Pattern matching, Function Arities & more 46
  47. 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. 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. 49. Anonymous 49
  50. 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. 51. Looping Tail recursion makes looping good again. 51
  52. 52. print([]) -> ok; print([H|T]) -> io:format(“~p~n”, [H]), print(T). 52
  53. 53. Conditionals If’s are okay, cases are cooler. 53
  54. 54. if A == 5 -> do_something(); true -> do_something_else() end. 54
  55. 55. case A of 0 -> first(); 2 -> second(); _ -> something() end. 55
  56. 56. Resources http://www.erlang.org Technorati 56
  57. 57. 57
  58. 58. Questions? 58

×