Skip to content

Latest commit

 

History

History
475 lines (332 loc) · 9.61 KB

File metadata and controls

475 lines (332 loc) · 9.61 KB

code-engine: sh

author: Colin MacDonald
email: colin@bluegraybox.com
title: Erlang Intro
footer:
subfooter:
{% css }
.syntaxhighlighter {
padding: 0.2em 0 0.2em 1em;
margin: 0 0 0 0 !important;
width: 100
;
}
.syntaxhighlighter table td.code .line {
padding: 0 0 0 0 !important;
}
.code {
background-color: #DDDDDD;
padding: 1px;
}
.slide h1 {
text-align: left;
color: #FF6633;
}
.codeurl {
color: grey;
font-style: italic;
}
body {
color: grey;
}
small {
color: lightgrey;
}
.author {
color: grey;
font-style: italic;
margin-top: 6em;
}
table.author td {
padding-left: 4em;
}
table.author td:first-child {
padding-left: 0;
}
li.none {
list-style-type: none;
}
a:link, a:visited {
color: grey;
text-decoration: none;
}
.credit, .credit a, .aside {
color: #888;
}
.credit, .credit a {
font-size: medium;
}
.error {
color: red;
}

.bullets.code ul li {
text-align: left;
padding: 10px 25px;
}
{% end %}

Erlang: Your New Favorite Scripting Language?

What’s cool about Erlang?

!SLIDE

Scalability

Image by joguldi

Ok, but what do I do with it?

!SLIDE

Elegance

Image by Stephen A. Wolfe

Procedural

Narrative

Image from Pratham Books

Object-Oriented

Things that do things

Image from Frédéric Bisson

!SLIDE

Relationships

Image from TheIguana

Functional

f(x)

What does that even mean?

!SLIDE

OO vs. Functional

Image from Yun Huang Yong

Why?

Procedural

  • 1 + 2 + 3 + 4 + 5 + … + n

Why?

Procedural

  • 1 + 2 + 3 + 4 + 5 + … + n

Functional

  • n(n+1)/2

Where are my curly braces?!

!SLIDE

  • % comment

!SLIDE

  • atom
  • ‘atom’
  • but not “atom”

!SLIDE

  • Variable

!SLIDE

  • [1, 2, 3] % list
  • {1, 2, 3} % tuple

!SLIDE

  • [X, Y, Z] = [1, 2, 3]

!SLIDE

  • [First, Second | Rest] = [1, 2, 3, 4]
  • [First, Second | Rest] = [1, 2]
  • [First, Second] = [1, 2, 3, 4](error)
  • [First, Second | _ ] = [1, 2, 3, 4]
  • [First, Second | _Rest ] = [1, 2, 3, 4]

!SLIDE
{% code lang=erlang line_numbers=false }
my_func(Param1, … ) →
%
intermediate stuff
Value.
{% end %}

!SLIDE
{% code lang=erlang line_numbers=false }
f(X) → X * 2.
{
end %}

!SLIDE

Overload

Image by Graeme Newcomb

!SLIDE

{% code lang=erlang line_numbers=false %}
my_func(Param1) →
my_func(Param1, “default”).

my_func(Param1, Param2) → %% actually do stuff…

{% end %}

!SLIDE
{% code lang=erlang line_numbers=false %}
my_func([]) → …

my_func([Only]) → … my_func([First, Second | Rest]) → …

{% end %}

!SLIDE
{% code lang=erlang line_numbers=false %}
my_func(0) → …

my_func(X) where X > 0 → … my_func(X) where X < 0 → …

{% end %}

!SLIDE
{% code lang=erlang line_numbers=false }
if
X >= 100 → 100;
X >= 0 → X;
true → 0
end
{
end %}

!SLIDE
{% code lang=erlang line_numbers=false }
case my_func(X) of
error → [];
List → List
end
{
end %}

!SLIDE
{% code lang=erlang line_numbers=false }
Output = case file:open(Filename, [read]) of
{ok, Handle} →
process_file(Handle);
{error, Reason} →
log_error(Reason, Filename),
[]
end.
{
end %}

  • Output = process_file(Handle)
  • Output = []

!SLIDE
{% code lang=erlang line_numbers=false %}
#!/usr/bin/escript

main(_Args) → io:format(“Hello World!~n”).

{% end %}

!SLIDE
{% code lang=erlang line_numbers=false %}
#!/usr/bin/escript

main(Args) → main(“World”, Args). main(_, [“-h” | _Args]) → io:format(“Help! Help!~n”); main(_, [“-n”, Name | Args]) → main(Name, Args); main(Name, []) → io:format(“Hello, ~s!~n”, [Name]).

{% end %}

!SLIDE

Detour

Image by chrisdlugosz

!SLIDE

       project
            action
            sub-project
                action
            action
        project
            sub-project
                sub-project
                    action

!SLIDE
p=. * work on presentation [computer, quiet] (2011-08-24) !!

!SLIDE

Input

!SLIDE

Output

!SLIDE

Ruby

Image from Wikimedia Commons

!SLIDE

ruby parser

!SLIDE

Hackers & Painters

Image from O’Reilly Media

!SLIDE

Train Wreck

Image from IMLS DCC

!SLIDE
{% code lang=java line_numbers=false }
String[] process(String[] input) {
String[] output = new String[input.length];
for (int i=0; i < input.length; i++) {
output[i] = do_stuff(input[i]);
}
return output;
}
{
end %}

!SLIDE
{% code lang=erlang line_numbers=false }
process([First|Rest], Output) →
NewFirst = do_stuff(First),
process(Rest, [NewFirst|Output]);
{
end %}

!SLIDE
{% code lang=erlang line_numbers=false %}
Output = process(Input, []).

process([First|Rest], Output) → NewFirst = do_stuff(First), process(Rest, [NewFirst|Output]); process([], Output) → lists:reverse(Output).

{% end %}

!SLIDE

thinking

Image by Brian Hillegas

!SLIDE

more thinking

Image by Alex E. Proimos

!SLIDE

lisp step 1

!SLIDE

lisp step 2

!SLIDE

lisp step 3

!SLIDE

lisp step 4

!SLIDE

lisp step 5

Erlang: A kinder, gentler Lisp?

!SLIDE
{% code lang=erlang line_numbers=false %}
build_nodes([{Content, Indent}|Rest]) →
IsChild = fun({_C, I}) → I > Indent end,
{ChildLines, SiblingLines} = lists:splitwith(IsChild, Rest),
Children = build_nodes(ChildLines),
Siblings = build_nodes(SiblingLines),
[{Content, Children}|Siblings];

build_nodes([]) → [].

{% end %}

!SLIDE

bowling

Image by Sam Howzit

!SLIDE

Python

Image from Wikipedia

!SLIDE
{% code lang=erlang line_numbers=false %}
score(Rolls) → frame(Rolls, 1, 0).

frame(_BonusRolls, 11, Score) → Score; frame([10|Rest], Frame, Score) → [Bonus1, Bonus2|_] = Rest, frame(Rest, Frame + 1, Score + 10 + Bonus1 + Bonus2); frame([First,Second|Rest], Frame, Score) when (First+Second==10)→ [Bonus1|_] = Rest, frame(Rest, Frame + 1, Score + 10 + Bonus1); frame([First,Second|Rest], Frame, Score) → frame(Rest, Frame + 1, Score + First + Second).

{% end %}

!SLIDE
{% code lang=erlang line_numbers=false %}
test() →
test(0, [0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0]),
test(20, [1,1, 1,1, 1,1, 1,1, 1,1, 1,1, 1,1, 1,1, 1,1, 1,1]),
test(150, [5,5, 5,5, 5,5, 5,5, 5,5, 5,5, 5,5, 5,5, 5,5, 5,5, 5]),
test(47, [1,1, 1,1, 1,1, 1,1, 1,1, 1,1, 1,1, 1,1, 1,1, 10, 10 ,9]),
test(173, [7,3, 7,3, 7,3, 7,3, 7,3, 7,3, 7,3, 7,3, 7,3, 7,3, 10]),

test(Expected, Rolls) → case score(Rolls) of Expected → io:fwrite(“Pass~n”); Scored → io:fwrite(“Fail: expected=~p, scored=~p~n”, [Expected, Scored]) end.

{% end %}

!SLIDE

Reflection

Image by Foxtongue

!SLIDE

Bonsai

Image by Cass Chin

!SLIDE

Fun

Image by orsocurioso

!SLIDE

Start Small

Image by Shedking

!SLIDE

Scale

Image by dullhunk

!SLIDE

The End

Image by somethingcommon

Colin MacDonald