45
Erlang 101 Istanbul Hacker Space https://istanbulhs.org/ 25 Kasim 2012 Gokhan Boranalp [email protected]

erlang 101

Embed Size (px)

DESCRIPTION

Erlang 101 giriş semineri istanbul hackerspace

Citation preview

Page 1: erlang 101

Erlang 101Istanbul Hacker Space

https://istanbulhs.org/

25 Kasim 2012Gokhan Boranalp

[email protected]

Page 2: erlang 101

● Nereden nereye. ○ Tarihçe ve şimdiki durum.

● Dil● Önemli Noktalar● Kullananlar● Made with Erlang● Sorular

Page 3: erlang 101

Tarihçe

Page 4: erlang 101

● SHARED MEMORY WITH LOCKS● SOFTWARE TRANSACTIONAL MEMORY

(STM)● FUTURES, PROMISES, AND SIMILAR● MESSAGE PASSING

Process Modelleri

Page 5: erlang 101

Karakteristik Özellikler

■ Message Passing■ Dynamic typed■ Actor model ■ Share Nothing■ Immutable Objects

Page 6: erlang 101

Dil

1> io:format("Naber dunya~n").Naber dunyaok2> io:format("Naber, ~s!~n", [kanka]).Naber, kanka!ok

- ~s = %s printf C- ~n = \n

Page 7: erlang 101

Dil

Atoms

- true, false, test_me, kunthar@local, 'EXIT'

Integers

- 10, -200, 12345678909999999999999999999

- 16#FFffFFff=4294967295

- 16#FF= 255

- $A = 65 (ASCII kod A)

Floats

3.14

-0.123

299792458.0

6.022137e23

6.6720e-11

Page 8: erlang 101

Dil

Tuples- {1, 2, 3} {bir, iki, uc, dort} {buradan, "IHS", "sizi selamlar"} {complex, {nested, "structure", {here}}}

Lists[][1, 2, 3] [bir, iki, uc] [[1,2,3],[4,5,6]] [{yarin, "biraz oku"}, {sonra, "yuruyus yap"}, {enson, "otur da yaz"}]

Page 9: erlang 101

Dil

String (list of ASCII codes) "merhaba" = [$m, $e, $r, $h, $a, $b, $a] = [109,101,114,104,97,98,97]

Binaries<<0, 1, 2, ..., 255>><<109,101,114,104,97,98,97>> = <<"merhaba">>

Binaries with Bit Syntax<<1:1,32787:15>> = 2#1111111111111111

Diger Data TipleriFuns (lambda expression or closure)Function-as-data-object

Page 10: erlang 101

Dil

Pids (Process Identifiers)self().<0.31.0>

Ports #Port<0.472>.

References5> make_ref().#Ref<0.0.0.42>

Page 11: erlang 101

Noktalama

○ (.) when hariç herşeyi sonlandir○ (;) koşulu sonlandır○ (,) ifadeyi (expressions) sonlandır

Page 12: erlang 101

List Comprehensions

{2n : n in L}in Erlang: ○ brackets ({}) become square brackets ([]), ○ the colon (:) becomes two pipes (||) ○ and the word 'in' becomes the arrow (<-).

1> [2*N || N <- [1,2,3,4]].[2,4,6,8]

Page 13: erlang 101

List Operations

Page 14: erlang 101

List Operations

Page 15: erlang 101

Anonymous Functions

Page 16: erlang 101

Functions

Page 17: erlang 101

Erlang Önemli Noktalar

● Declarative● Concurrency● Soft real-time● Robustness● Distribution● Hot code loading● External Interfaces● Portability

○ Functional programming language

○ High abstraction level○ Pattern matching○ Readable programs

Page 18: erlang 101

Erlang Önemli Noktalar

● Declarative● Concurrency● Soft real-time● Robustness● Distribution● Hot code loading● External Interfaces● Portability

○ Solid concurrency model○ Scales to handle complex

concurrency○ Simple abstractions

Örnekler

Page 19: erlang 101

spawn ile yeni bir process yaratmak

Pid = spawn(ex3,activity,[Joe,75,1024]) activity(Joe,75,1024)

Page 20: erlang 101

Asenkron mesajlar

Page 21: erlang 101

hard core concurrency

Page 22: erlang 101

Erlang Önemli Noktalar

● Declarative● Concurrency● Soft real-time● Robustness● Distribution● Hot code loading● External Interfaces● Portability

○ Lightweight processes ○ Fast message passing○ Response times in the order of

milliseconds efficient garbage collection

Oynat Uğurcuğum

Page 23: erlang 101

process creation times

Page 24: erlang 101

message passing times

Page 25: erlang 101

Erlang Önemli Noktalar

● Declarative● Concurrency● Soft real-time● Robustness● Distribution● Hot code loading● External Interfaces● Portability

○ Simple and consistent error recovery

○ Supervision hierarchies

Örnekler

Page 26: erlang 101

process'ler birbirine bağlı çalışabilir!

Page 27: erlang 101

proses hata kontrolü

- Proses sonlandığında, bağlı tüm proseslere exit sinyali gönderilir.

Page 28: erlang 101

hata kontrolü (2)

- Exit sinyalleri "tuzaklanarak" (trap) messages şeklinde alınabilir.

Page 29: erlang 101

Supervisors & Workers

Page 30: erlang 101

Error handling

● No global variables -- fewer side-effects ● No direct memory access -- no pointer errors ● No malloc/free bugs ● Solid concurrency model -- reduces synchronization

problems, reduces the state space (simpler programs) ● Fault isolation -- memory-protected lightweight

processes ● Built-in error recovery support -- more consistency

Concurrency & Fault Tolerance were designed into the language from the start!

Page 31: erlang 101

Debugging and Profiling Support

● Symbolic crash reports○ Usually sufficient info to locate bugs within minutes

● Built-in trace support○ Function calls (ability to filter on module name,

function and args) ○ Messages (+ sequence trace) ○ Process events (context switch, spawn, link, exit) ○ Garbage collections ○ Optionally with timestamps (can be used for profiling,

benchmarks)○ Trace to process, file, or port (network socket) ○ Also available on live systems

Page 32: erlang 101

Erlang Önemli Noktalar

● Declarative● Concurrency● Soft real-time● Robustness● Distribution● Hot code loading● External Interfaces● Portability

○ Explicit or transparent distribution

○ Network-aware runtime system

Örnekler

Page 33: erlang 101

Transparent Distribution

- başka bir bilgisayara prosesler arası mesaj gönderimi, aynı bilgisayarda mesaj gönderimi kadar kolaydır.

Page 34: erlang 101

Simple RPC

Page 35: erlang 101

Erlang Önemli Noktalar

● Declarative● Concurrency● Soft real-time● Robustness● Distribution● Hot code loading● External Interfaces● Portability

○ Easily change code in a running system

○ Enables non-stop operation○ Simplifies testing

Page 36: erlang 101

Erlang Önemli Noktalar

● Declarative● Concurrency● Soft real-time● Robustness● Distribution● Hot code loading● External Interfaces● Portability

○ Ports to the outside world○ behave as Erlang processes

- Erlang Port Drivers

Page 37: erlang 101

Erlang Önemli Noktalar

● Declarative● Concurrency● Soft real-time● Robustness● Distribution● Hot code loading● External Interfaces● Portability

○ Any UNIX○ Windows○ VxWorks etc.

Page 38: erlang 101

Systems Overview

Page 39: erlang 101

Erlang/OTP (Open Telecom Platform)

● Middleware for Erlang development ● Designed for fault tolerance and portability● Behaviors: A formalization of design patterns● Components

○ Error handling, reporting and logging○ Mnesia, distributed real-time database management

system CORBA, IDL Compiler, Java & C Interface Support ○ HTTP Server + Client, FTP Client ○ SNMP Agent + ASN.1 Compiler ○ H.248 ○ XML○ ...

Page 40: erlang 101

OTP Behaviors

● "A formalization of design patterns"○ A framework + generic code to solve a common problem

○ Built-in support for debugging and software upgrade

○ Makes it easier to reason about the behavior of a program

● Examples of OTP behaviors application defines how an application is implemented

supervisor used to write fault-tolerant supervision trees

gen_server for writing client-server applications

gen_event for writing event handlers

gen_fsm for finite state machine programming

Page 41: erlang 101

Büyük Projelerde Erlang

● Easy to build a first runnable application

● Easy to debug

● Easy to maintain

● Very strong support for fault tolerance

● Suitable for large systems/projects

● Great for prototyping

● Impressive performance/scalability in real applications Outstanding tool for test automation

● High programmer satisfaction

Page 42: erlang 101

Kullananlar

● Amazon Web Servisleri - SQS ve SimpleDB● Facebook, chat● Twitter● Mochimedia● del.icio.us / Yahoo● T-Mobile● Telia● Bluetail/Alteon/Nortel● Github

Page 43: erlang 101

Made with Erlang

○ Apache CouchDB – document-based DB with REST○ Disco – Map/Reduce framework for Erlang ○ Fuzed – Generic clustering framework (Powerset) ○ Mochiweb – Fast lightweight web server framework

(like Java Servlets for Erlang) ○ DHT – Distributed Key/Value Stores – 90% of all

implementations in Erlang – Dynomite, Scalaris, etc.○ Yaws web server○ Cowboy○ Nitrogen○ RabbitMQ○ Riak

Page 44: erlang 101

Good fit for

● Irregular concurrency ○ Task-level parallelism ○ Fine-grained parallelism

● Network servers ● Distributed systems ● Middleware:

○ Parallel databases ○ Message Queue servers

● Soft Real-Time / Embedded applications ● Monitoring, control and testing tools

Page 45: erlang 101

Kaynaklarhttp://en.wikipedia.org/wiki/Abstraction_%28computer_science%29http://is.gd/xHWvZ3

http://www.erlang.org

http://learnyousomeerlang.com/