Created
May 22, 2011 16:01
-
-
Save trotter/985624 to your computer and use it in GitHub Desktop.
Philly Lambda Erlang/OTP Talk
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(basic). | |
-export([say_hello/0, | |
make_hello/0, | |
match_me/1 | |
]). | |
say_hello() -> | |
io:fwrite("Ohai!\n"). | |
make_hello() -> | |
fun () -> io:fwrite("Ohai!\n") end. | |
%% Hello = make_hello(). | |
%% Hello(). | |
match_me(1) -> | |
loneliest; | |
match_me(2) -> | |
next_to_loneliest; | |
match_me(friend) -> | |
all_ok; | |
match_me(_) -> | |
what_is_this. | |
destructuring_bind() -> | |
[{Name, Title}, _] = [{"Trotter", "Speaker"}, | |
{"Aaron", "Organizer"}], | |
io:fwrite("~p\n", [Name]), %% => Trotter | |
io:fwrite("~p\n", [Title]). %% => Speaker |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(example_sup). | |
-behaviour(supervisor). | |
-export([ | |
start_link/0 | |
]). | |
-export([init/1]). | |
%%-------------------------------------------------------------------- | |
%% API functions | |
%%-------------------------------------------------------------------- | |
start_link() -> | |
supervisor:start_link(?MODULE, []). | |
%%-------------------------------------------------------------------- | |
%% Supervisor callbacks | |
%%-------------------------------------------------------------------- | |
init([]) -> | |
Worker = {gen_server_examples, {gen_server_examples, start_link, []}, | |
permanent, 5000, worker, [gen_server_examples]}, | |
RestartStrategy = {one_for_one, 5, 30}, | |
{ok, {RestartStrategy, [Worker]}}. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(gen_server_examples). | |
-behaviour(gen_server). | |
-export([ | |
say_hello_sync/1, | |
say_hello_async/0, | |
start/0, | |
start_link/0 | |
]). | |
%% gen_server callbacks | |
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, | |
terminate/2, code_change/3]). | |
-define(SERVER, ?MODULE). | |
-record(state, {}). | |
%%-------------------------------------------------------------------- | |
%% API | |
%%-------------------------------------------------------------------- | |
start() -> | |
gen_server:start({local, ?SERVER}, ?MODULE, [], []). | |
start_link() -> | |
gen_server:start_link({local, ?SERVER}, ?MODULE, [], []). | |
say_hello_sync(Name) -> | |
gen_server:call(?SERVER, {say_hello, Name}). | |
say_hello_async() -> | |
gen_server:cast(?SERVER, say_hello). | |
%%-------------------------------------------------------------------- | |
%% gen_server callbacks | |
%%-------------------------------------------------------------------- | |
init([]) -> | |
{ok, #state{}}. | |
handle_call({say_hello, Name}, _From, State) -> | |
Response = "Hello, " ++ Name, | |
{reply, {ok, Response}, State}. | |
handle_cast(say_hello, State) -> | |
io:fwrite("Hello world!\n"), | |
{noreply, State}. | |
handle_info(_Reason, State) -> | |
{noreply, State}. | |
terminate(_Info, _State) -> | |
ok. | |
code_change(_OldVsn, State, _Extra) -> | |
{ok, State}. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% Gonna die | |
1> Hello = 3. | |
3 | |
2> Hello = 2. | |
** exception error: no match of right hand side value 2 | |
%% Forget all variables (repl only) | |
3> f(). | |
ok | |
4> Hello = 2. | |
2 | |
5> process:concurrent_stuffs(). | |
Super fast | |
<0.39.0> | |
So slow |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def match_me(val) | |
case val | |
when 1 | |
:loneliest | |
when 2 | |
:next_to_loneliest | |
when :friend | |
:all_ok | |
else | |
:what_is_this | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% Start two nodes. One using `erl -name a`, the other using `erl -name b`. | |
%% On node a, run `node_connections:listen().` | |
%% On node b, run `node_connections:show_me().` | |
-module(node_connections). | |
-export([ | |
listen/0, | |
show_me/0 | |
]). | |
listen() -> | |
register(listener, self()), | |
listen_loop(). | |
listen_loop() -> | |
receive | |
{From, pizza} -> From ! "Nom noms", listen_loop(); | |
{From, _} -> From ! "Boo!", listen_loop(); | |
_ -> io:fwrite("Listen is dying!") | |
end. | |
show_me() -> | |
AHost = list_to_atom("a@" ++ net_adm:localhost()), | |
io:fwrite("Current nodes: ~p~n", [nodes()]), | |
io:fwrite("Ping? ~p~n", [net_adm:ping(AHost)]), | |
io:fwrite("Current nodes: ~p~n", [nodes()]), | |
{listener, AHost} ! {self(), pizza}, | |
receive | |
PizzaResp -> PizzaResp | |
end, | |
io:fwrite("Send pizza: ~p~n", [PizzaResp]), | |
{listener, AHost} ! {self(), faces}, | |
receive | |
FacesResp -> FacesResp | |
end, | |
io:fwrite("Send faces: ~p~n", [FacesResp]), | |
{listener, AHost} ! die. | |
atom_for_slides() -> | |
'[email protected]'. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
([email protected])1> node_connections:show_me(). | |
Current nodes: [] | |
Ping? pong | |
Current nodes: ['[email protected]'] | |
Send pizza: "Nom noms" | |
Send faces: "Boo!" | |
die |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(process). | |
-compile(export_all). | |
make_process() -> | |
spawn(?MODULE, food_loop, []). | |
food_loop() -> | |
receive | |
pizza -> io:fwrite("let's eat\n"), | |
food_loop(); | |
death -> io:fwrite("all done here\n"); | |
_ -> io:fwrite("Nothing to do\n"), | |
food_loop() | |
end. | |
pizza_death() -> | |
Proc = make_process(), | |
Proc ! pizza, | |
Proc ! hi, | |
Proc ! death, | |
%% Goes into the void | |
Proc ! huh. | |
concurrent_stuffs() -> | |
spawn(fun () -> timer:sleep(1000), | |
io:fwrite("So slow\n") end), | |
spawn(fun () -> io:fwrite("Super fast\n") end). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment