Presentation is loading. Please wait.

Presentation is loading. Please wait.

OJ Reeves. Agenda  The Boring Stuff Basic syntax, expressions, funcs, records Atoms, BIFs, data types, bools, equality  The not so Boring Stuff Pattern.

Similar presentations


Presentation on theme: "OJ Reeves. Agenda  The Boring Stuff Basic syntax, expressions, funcs, records Atoms, BIFs, data types, bools, equality  The not so Boring Stuff Pattern."— Presentation transcript:

1 OJ Reeves

2 Agenda  The Boring Stuff Basic syntax, expressions, funcs, records Atoms, BIFs, data types, bools, equality  The not so Boring Stuff Pattern matching, guards, binaries  Some Interesting Stuff processes, concurrency  Some Cool Stuff Distribution, fault-tolerance  A Problem  OTP (depending on time)

3 Play Along  Load werl or erl, that’s your REPL.  Modules? Create one for mucking around, foo.erl:  -module(foo). % matches file -compile(export_all). % your stuff goes here  Compile file in REPL: c(foo). Your code is auto-loaded when compiled.

4 The Boring Stuff  IQ = 10. % “binding” IQ = 10. % ok, same value bound IQ = 11. % runtime error  Name = “OJ”.  Response = >.  Point = {1.0, 3.2}. {X, Y} = Point. Tuples can be of mixed “type” / nested  random() -> 4. % ode to XKCD  [“I am”, “a list of”, “strings”]. Lists can be of mixed “type” [1, 10.4, “fear me!”, my_atom]

5 More Boring Stuff  clean(Text) -> Funs = [fun rem_bad/1, fun add_punc/1], lists:foldl(fun(X, T) -> X(T) end, Text, Funs).  -record(person, {name, age, state=sober}).  get_name(#person{name=Name}) -> Name.  set_name(Person, Name) -> Person#person{name=Name}.  Common: ok vs {error, “Reason”}  ok = some_module:do_something().

6 The Last of the Boring Stuff  Status = dead. anything_like_this, ‘OR THIS’  BIF – “Built in Function” integer_to_list(), list_to_binary(), atom_to_list(), etc.  Data types – don’t really have any!  Boolean stuff and, or, xor, andalso, orelse  Equality =:= =/= (basically == and !=) == /= (when mixed “types” such as 1 == 1.0) =, >, etc (=< is not a typo!)

7 The Not So Boring Stuff  div(N, 0) -> infinity; % cont.. div(N, D) -> N /D.  first_item_even([H|T]) -> H rem 2 =:= 0.  get_status(Person) -> case P#person.name of ○ “OJ” -> loser; % continue _ -> legend % no, or. end. % terminated here

8 More Not So Boring Stuff  is_decrepit(#person{age=Age}) when Age > 80 -> true;  is_decrepit(_Person) -> false.  Pattern matching is binding! rock, paper, scissors (no lizard/spock) Winner = case {Move1, Move2} of ○ {paper, rock} -> player1; {rock, scissors} -> player1; {scissors, paper} -> player1; {Move, Move} -> none; % oooh! _ -> player2 end.

9 More Not So Boring Stuff  Binaries are special lists >. >. % or int data  Can be pattern matched: A = >. > = A. % C == 2  Bit syntax – pull apart binaries!

10 Last of the Not So Boring Stuff  Pulling an MP3 header apart:  valid_header( case {B, C} of ○ {1, _} -> false; % bad version ○ {_, 0} -> false; % bad layer ○ _ -> true end;  valid_header(_) -> false. Thanks Joe!

11 Last of the Not So Boring Stuff  Pulling an MP3 header apart:  valid_header( case {B, C} of ○ {1, _} -> false; % bad version ○ {_, 0} -> false; % bad layer ○ _ -> true end;  valid_header(_) -> false.

12 Fizzbuzz  For each number from 1 to 100, output: “Fizz” if divisible by 3 “Buzz” if divisible by 5 “FizzBuzz” if divisible by both 3 and 5 The number itself otherwise.

13 Some Interesting Stuff  Processes – very cheap, easy to spin up. Not “native”. spawn(Fun) -> pid() ○ Spawn Fun/0 in a new process spawn(Node, Fun) -> pid() ○ Spawn Fun/0 on the given node spawn(Mod, Fun, Args) -> pid() ○ Spawn Mod:Fun(Args) in a new process spawn(Node, Mod, Fun, Args) -> pid() ○ See if you can guess…  Send messages to pids: Pid ! Message.

14 More Interesting Stuff  Demo of echo().  Parallel map: spawn() for each “job” Wait for response from each Pid Combine results Show me teh codez.

15 Agenda  The Boring Stuff Basic syntax, expressions, funcs, records Atoms, BIFs, data types, bools, equality  The not so Boring Stuff Pattern matching, guards, binaries  Some Interesting Stuff processes, concurrency  Some Cool Stuff Distribution, fault-tolerance  Fun Problem  Building a Webserver


Download ppt "OJ Reeves. Agenda  The Boring Stuff Basic syntax, expressions, funcs, records Atoms, BIFs, data types, bools, equality  The not so Boring Stuff Pattern."

Similar presentations


Ads by Google