erlang is not a city in germany

23
Erlang is not a city in Germany!

Upload: momo-13

Post on 14-Dec-2014

1.159 views

Category:

Documents


1 download

DESCRIPTION

Short introduction to the programming language Erlang hold at the GeekGirlMeetup in Berlin 2012

TRANSCRIPT

Erlangis not a city in Germany!

About meMonika Moser

@momo13

Software Architect

Erlangis a general-purpose language and

a runtime environment.

LanguageFunctional.

Strict evaluation.Single assignment.

Dynamic typing.

Erlanghas built in support for concurrency,

distribution and fault tolerance.

Ericsson Languagesince 1986

Developedto build near-real-time fault tolerant

distributed non-stop applications.

Used byCouchDB: NoSQL DB that uses JSON to store data

Facebook: For the chat backend

Wooga: Backend for Facebook games

Klarna: Electronic payment systems

Functional-module(hello).-export([world/0]).

world() -> io:format("Hello, world!~n", []).

Let it run!$ erlErlang R15B01 (erts-5.9.1) [source] [64-bit] [smp:2:2] [async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.9.1 (abort with ^G)1> c(hello).{ok,hello}2> hello:world().Hello, world!ok

Expressions> % A comment

> 1 + 3.4

> “This is a string”.“This is a string”

Expressions> [1, 2, 3].[1,2,3].

> [72, 101, 108, 108, 111, 33].“Hello!”

Expressions> Variable = “GeekGirls”.“GeekGirls”

> Variable = "are boring".** exception error: no match of right hand side value "are boring"

Pattern Matching> Tuple = { “Erlang”, “Joe A.”}.{ “Erlang”, “Joe A.”}.

> { Language, Inventor } = Tuple.{ “Erlang”, “Joe A.”}.

> Language.“Erlang”

Recursionfac(0) -> 1;fac(N) when N > 0, is_integer(N) -> N*fac(N-1).

Higher order functions> lists:foreach(fun(Word) ->! ! ! ! io:format("~p~n", [Word])! ! ! end,! ! ! ["Geek", "Girl", "Meetup"]! ! ).

“Geek”“Girl”“Meetup”

Fun with ListsList comprehension:

> [ X*X || X <- [2, 3, 4], X rem 2 == 0].[4,16]

ConcurrencyLightweight processes

Message passing primitives

Sending MessagesPID ! { message, “Content”}.

Receiving Messagesloop() -> receive { message, Content } -> io:format(“Content: ~p”, [Content]); loop(); Any -> io:format(“Received unexpected: ~p”, [Any]) end.

Spawning a processPID = spawn(fun loop/0).

PID ! {message, “GeekGirlMeetup”}.is_process_alive(PID).

true.PID ! stop.

is_process_alive(PID).false.

Monitor processesProcesses can be monitored.

EXIT signals with a reason will be received.

process_flag(trap_exit, true).{‘EXIT’, From, Reason}.

Useful forDistributed, reliable, concurrent systems