Erlang for Python programmers

Daniel Arbuckle

SoCal Piggies

What is Erlang?

[any material that should appear in print but not on the slide]

Digression: Functional Programming

[any material that should appear in print but not on the slide]

Why would we want that?

[any material that should appear in print but not on the slide]

Digression complete

[any material that should appear in print but not on the slide]

Loops in Erlang and Python

Where a Python programmer would type: an Erlang programmer would type:
def foo(i):
    for x in range(i, 11):
        do_something(x)
foo(10) ->
    do_something(0).

foo(I) when I < 10 ->
    do_something(I),
    foo(I + 1).
[any material that should appear in print but not on the slide]

Branching in Erlang and Python

Where a Python programmer would type: an Erlang programmer would type: or:
def foo(i):
    if i == 10:
        do_something(i)
    elif i < 10:
        do_something(i)
        foo(i + 1)
foo(10) ->
    do_something(0).

foo(I) when I < 10 ->
    do_something(I),
    foo(I + 1).
foo(I) ->
    if
        I == 10 ->
            do_something(I);
        I < 10 ->
            do_something(I),
            foo(I + 1)
    end.
[any material that should appear in print but not on the slide]

Message passing in Erlang and Python

Where a Python programmer would type: an Erlang programmer would type:
sock.sendall(pickle.dumps(('hello', 19, x)))
f = sock.makefile('r')
word, number, x = pickle.load(f)
f.close()
if word == 'welcome':
    process(word, number, x)
else:
    error(word, number x)
Proc_id ! {hello, 19, X}.
receive
    {welcome, Number, Y} ->
        process(welcome, Number, Y);
    {Word, Number, Y} ->
        error(Word, Number, Y)
end.
[any material that should appear in print but not on the slide]

Error handling in Erlang and Python

Where a Python programmer would type: an Erlang programmer would type:
try:
    something()
except ByTheGodsNooo:
    recover()
process_flag(trap_exit, true),
Risky = spawn(thismodule, something, []),
link(Risky),
receive
    {'EXIT', Risky, byTheGodsNoo} ->
        recover();
    {'EXIT', Risky, Reason} ->
        exit(Reason)
end.

[any material that should appear in print but not on the slide]

Other tidbits

[any material that should appear in print but not on the slide]