32
Programming Erlang June 17, 2009 Ken Pratt http://kenpratt.net /

Programming Erlang

Embed Size (px)

DESCRIPTION

(Given to the Vancouver Erlang and Ruby/Rails Meetup groups on June 17, 2009.) A follow-up to last month's "Intro to Erlang" (http://www.slideshare.net/kenpratt/intro-to-erlang), this talk will dive into the core components of the Erlang programming language, including processes, pattern matching, and message passing. Unlike the previous talk, it will be light on hype and loaded with code samples. If you missed the previous talk, you might want to run through the slides quickly at http://www.slideshare... before coming to get a brief overview Erlang. Speaker Bio: Ken Pratt has been developing software for the web for over 10 years. He fell in love with Ruby four years ago, but is still passionate about learning other languages and platforms. He has developed scalable web services for Electronic Arts, built Rails-based web applications since pre-1.0, and been featured in interactive art installations.

Citation preview

Page 1: Programming Erlang

Programming Erlang

June 17, 2009

Ken Pratt

http://kenpratt.net/

Page 2: Programming Erlang

Erlang philosophy

Simple language

Ultra-lightweight concurrency

No shared state (and no locks, no mutexes)

<3 asynchronous message passing

Processes should be able to run forever

Page 3: Programming Erlang

The language

Functional

Strong, dynamic typing

NO shared state(!!) (and no mutable variables)

Pattern matching on steroids

<3 tail recursion

Compiled to bytecode, runs on VM

Page 4: Programming Erlang

Basic data types

Integers

Floats

Atoms

Tuples

Lists

Page 5: Programming Erlang

Integers

No maximum size (a growing integer will keep eating memory until you run out of RAM -- that’s a big number!)

Page 6: Programming Erlang

Floating-point numbers

64-bit precision

Page 7: Programming Erlang

Atoms

Non-numerical constants

Like Ruby symbols

“Natural” atoms: [a-z][a-zA-Z0-9_@]+

Otherwise, anything single-quoted is an atom

Page 8: Programming Erlang

Tuples

Fixed-length sets of elements

Used sort of like C/C++ structs, for passing structured data around

Page 9: Programming Erlang

Lists

Variable-length collections of elements

Usually homogenous, but can be heterogeneous (like Ruby)

Library of list operations (foreach, map, filter, sort, etc)

Page 10: Programming Erlang

NOT Types

Booleans

Strings (oh dear!!)

Hashes

Classes & objects ;)

Page 11: Programming Erlang

Booleans

Just a special case of atoms

Page 12: Programming Erlang

Strings

Just a list of integers in the range 0..255

Double quotes are syntactic sugar

Unicode support added March 2009 in Erlang R13A

io:format and io_lib:format for printf-like formatting

Page 13: Programming Erlang

Hash tables/dictionaries

Property list

Just a list of two-element tuples

“proplists” library has common functionality

“dict” library is a heavier, more feature-full option

Page 14: Programming Erlang

Classes & objects

Erlang is functional, and doesn’t have classes/objects

However, it has records, which are a “named tuple”

They are a compile-time feature, which is ANNOYING

Page 15: Programming Erlang

Functions

Named functions & anonymous functions (funs)

Differ by arity (convention is [func_name/arity])

myfun/1 and myfun/2 are different functions

Page 16: Programming Erlang

Pattern matching

Pattern matching is awesome

“=” is NOT an assignment operator! It is a pattern match operator

Unbound variables will get bound during the match

Page 17: Programming Erlang

Nested patterns

You can match nested structures

Page 18: Programming Erlang

Matching list elements

It’s easy to grab one or more elements from a list

Page 19: Programming Erlang

Matching function clauses

Can have multiple named functions with the same arity, using pattern matching on arguments

Page 20: Programming Erlang

Matching + functions

Page 21: Programming Erlang

Conditionals

Supports case and if statements, but they are very different from other languages

Both have pattern matching support

Both use “guards”, which are simple operations like comparisons (<, >, etc), is_atom, length(List), etc

Page 22: Programming Erlang

Case statements

Page 23: Programming Erlang

If statements

Avoid them, as case is cleaner, even for true/false

Page 24: Programming Erlang

Processes

Erlang processes are sort of like threads, but without the headaches of shared state

Page 25: Programming Erlang

Messages

Each process has a message queue

Easy to send messages around

Common convention is to send a tuple where first element is an atom with the message type

Page 26: Programming Erlang

Receiving messages

Use a receive block to pull a message off the queue

Usual pattern is a server “loop” that waits for messages

Page 27: Programming Erlang

Synchronous messages

Asynchronous by default

But easy to implement synchronous messages

Need to pass own pid along with message, so server can reply (self() returns pid of current process)

Page 28: Programming Erlang

Actor model

Erlang is built around the Actor model

Think of a process like an object

It handles incoming messages (like calling methods with arguments)

It sends replies (like method return values)

It incapsulates state (passed around explicitly)

Page 29: Programming Erlang

Architecture

= light-weight process with a built-in mailbox

Page 30: Programming Erlang

Distributed architecture

= light-weight process with a built-in mailbox

Page 31: Programming Erlang

Networking patterns

Erlang has built-in functions for common network operations, such as RPC

OTP (“Open Telecom Platform”) libraries provide frameworks for many architecture patterns, such as servers and state machines

gen_server is most common -- supports synchronous and asynchronous calls, and server state

Page 32: Programming Erlang

Where to start

“Programming Erlang” by Joe Armstrong (creator of Erlang)

Slides & videos online!(I’ll post the link to the mailing list)http://www.erlang-factory.com/

conference/SFBayAreaErlangFactory2009