1. Trang chủ
  2. » Luận Văn - Báo Cáo

AN INTRODUCTION TO ERLANG FOR PYTHON PROGRAMMERS

95 0 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề An Introduction to Erlang for Python Programmers
Tác giả Paul Barry
Trường học Institute of Technology, Carlow
Chuyên ngành Computer Science
Thể loại Presentation
Năm xuất bản 2011
Thành phố Carlow
Định dạng
Số trang 95
Dung lượng 3,63 MB

Nội dung

Công Nghệ Thông Tin, it, phầm mềm, website, web, mobile app, trí tuệ nhân tạo, blockchain, AI, machine learning - Công Nghệ Thông Tin, it, phầm mềm, website, web, mobile app, trí tuệ nhân tạo, blockchain, AI, machine learning - Công nghệ thông tin An Introduction to Erlang for Python programmers Paul Barry – Institute of Technology, Carlow in Ireland PyCon Ireland 2011 - October 2011 2 Grab the slides: http:paulbarry.itcarlow.ieErlangWebcast.pdf 3 Disclaimer 4 What exactly is Erlang? 5 “Erlang is a declarative,dynamically-typed, functional, concurrent, distributed and fault-tolerant programming language with garbage collection and code hot-swapping built into its runtime.” Buzz-word city, dude 6 Scratching an itch in the early ''''80s... 7 Why learn Erlang? 8 “Learn a new programming language every year” 9 “Buy at least one new programming book every year” 10 So, really, why learn Erlang? 11 A better programmer, you will be... 12 Learn how others solve problems with a different language, then apply these new techniques to your current language 13 Learn what each language is good at, then pick the right tool for the job 14 Works with a really big hammer 15 What is Erlang good at? 16 What Erlang Wasn''''t Designed To Do 17 Erlang wasn''''t designed to...  Run in a browser  Process text efficiently  Be easy to teach  Be easy to learn  Build dynamic websites  Run on mobile phones  Allow non-programmers to program  Build GUIs 18 Erlang was designed to build software systems that never stop 19 What exactly do you mean by “never”? 20http:www.infoq.compresentationsSystems-that-Never-Stop-Joe-Armstrong “Never” at Ericsson means “no more than 4 minutes downtime per year”... 21 Wow That''''s 5 nines availability 99.999 uptime 22 Let''''s build software that''''s 100 defect free 23 Let''''s throw lots and lots of code at the problem and it''''ll go away, eh? 24 There has to be a better way 25 Erlang programmers concentrate on coding for the correct case and crashing on failure Let it crash 26 Robustness is achieved by having programmers concentrate on processes and the interactions (or messages) between them 27 Early error detectionrecovery and the use of concurrent programming techniques lets you build fault tolerant systems 28 COP: Concurrency-Oriented Programming 29 The problem is... 30 Erlang is a little weird The problem is... 31 Consider this code... -module(helloserver). -export(hello0). hello() -> receive {FromPID, Who} -> case Who of robert -> FromPID "Hello Robert."; mike -> FromPID "Hello Mike."; joe -> FromPID "Hello Joe."; -> FromPID "I don''''t know you." end, hello() end. 32 Strange punctuation symbols . ; , -module(helloserver). -export(hello0). hello() -> receive {FromPID, Who} -> case Who of robert -> FromPID "Hello Robert."; mike -> FromPID "Hello Mike."; joe -> FromPID "Hello Joe."; -> FromPID "I don''''t know you." end, hello() end. 33 Lots of arrows -> in lots of places -module(helloserver). -export(hello0). hello() -> receive {FromPID, Who} -> case Who of robert -> FromPID "Hello Robert."; mike -> FromPID "Hello Mike."; joe -> FromPID "Hello Joe."; -> FromPID "I don''''t know you." end, hello() end. 34 Even stranger symbols - -module(helloserver). -export(hello0). hello() -> receive {FromPID, Who} -> case Who of robert -> FromPID "Hello Robert."; mike -> FromPID "Hello Mike."; joe -> FromPID "Hello Joe."; -> FromPID "I don''''t know you." end, hello() end. 35 What''''s the deal with {} and () ? -module(helloserver). -export(hello0). hello() -> receive {FromPID, Who} -> case Who of robert -> FromPID "Hello Robert."; mike -> FromPID "Hello Mike."; joe -> FromPID "Hello Joe."; -> FromPID "I don''''t know you." end, hello() end. 36 Functions that call themselves -module(helloserver). -export(hello0). hello() -> receive {FromPID, Who} -> case Who of robert -> FromPID "Hello Robert."; mike -> FromPID "Hello Mike."; joe -> FromPID "Hello Joe."; -> FromPID "I don''''t know you." end, hello() end. 37 This is weird... and there''''s even more 38 Erlang Culture Shock 39 Shock 1 Erlang''''s syntax is based on Prolog -module(howdy). -export(hi1). hi(Name) -> io:format(''''Hi there, ~p~n'''', Name). {person, First, Last} = {person, ''''Paul'''', ''''Barry''''}. howdy:hi(Last). 40 Shock 2 Erlang is not object-oriented 41 Shock 3 Everything in Erlang is immutable Not just tuples and not just strings... EVERYTHING 42 So... this doesn''''t work X = 10. X = X + 1. exception error: no match of right hand side value 11 Y = X + 1. 43 Erlang''''s troublesome = operator The “=” operator does not mean “assign” It means “match” or “bind to” 44 Destructive updates are forbidden and (as an added twist) Variables must start with an Uppercase letter: X Pid Func No side effects here 45 Shock 4 There are no built-in looping constructs No for and no while 46 So... how do you loop? 47 List Comprehensions Erlang: Alist =...

Trang 1

An Introduction to Erlang

for Python programmers

Paul Barry – Institute of Technology, Carlow in Ireland

PyCon Ireland 2011 - October 2011

Trang 2

Grab the slides:

http://paulbarry.itcarlow.ie/ErlangWebcast.pdf

Trang 3

Disclaimer

Trang 4

What exactly is Erlang?

Trang 5

“Erlang is a declarative,dynamically-typed,

functional, concurrent, distributed and

fault-tolerant programming language

with garbage collection and code

hot-swapping built into its runtime.”

Buzz-word city,

dude!

Trang 6

Scratching an itch in

the early '80s

Trang 7

Why learn Erlang?

Trang 8

“Learn a new programming

language every year”

Trang 9

“Buy at least one new

programming book every year”

Trang 10

So, really, why learn Erlang?

Trang 11

A better programmer,

you will be

Trang 12

Learn how others solve problems with a

different language, then apply these new

techniques to your current language

Trang 13

Learn what each language is good at,

then pick the right tool for the job

Trang 14

Works with a really big hammer!

Trang 15

What is Erlang good at?

Trang 16

What Erlang Wasn't Designed To Do

Trang 17

 Build dynamic websites

 Run on mobile phones

 Allow non-programmers to program

 Build GUIs

Trang 18

Erlang was designed to build

software systems that never stop

Trang 19

What exactly do you

mean by “never”?

Trang 20

http://www.infoq.com/presentations/Systems-that-Never-Stop-Joe-Armstrong

“Never” at Ericsson means “no more than 4 minutes downtime per year”

Trang 21

Wow! That's 5 nines

availability!*

* 99.999% uptime

Trang 22

Let's build software

that's 100% defect free!

Trang 23

Let's throw lots and lots

of code at the problem and

it'll go away, eh?

Trang 24

There has to be a better way

Trang 25

Erlang programmers concentrate on

coding for the correct case

and crashing on failure

Let it crash!

Trang 26

Robustness is achieved by having

programmers concentrate on processes and the

interactions (or messages) between them

Trang 27

Early error detection/recovery and the use

of concurrent programming techniques

lets you build fault tolerant systems

Trang 28

COP: Concurrency-Oriented Programming

Trang 29

The problem is

Trang 30

Erlang is a little weird

The problem is

Trang 31

robert -> FromPID ! "Hello Robert.";

mike -> FromPID ! "Hello Mike.";

joe -> FromPID ! "Hello Joe.";

_ -> FromPID ! "I don't know you."

end,

hello()

end

Trang 32

robert -> FromPID ! "Hello Robert.";

mike -> FromPID ! "Hello Mike.";

joe -> FromPID ! "Hello Joe.";

_ -> FromPID ! "I don't know you."

end,

hello()

end

Trang 33

robert -> FromPID ! "Hello Robert.";

mike -> FromPID ! "Hello Mike.";

joe -> FromPID ! "Hello Joe.";

_ -> FromPID ! "I don't know you."

end,

hello()

end

Trang 34

robert -> FromPID ! "Hello Robert.";

mike -> FromPID ! "Hello Mike.";

joe -> FromPID ! "Hello Joe.";

_ -> FromPID ! "I don't know you."

end,

hello()

end

Trang 35

robert -> FromPID ! "Hello Robert.";

mike -> FromPID ! "Hello Mike.";

joe -> FromPID ! "Hello Joe.";

_ -> FromPID ! "I don't know you."

end,

hello()

end

Trang 36

robert -> FromPID ! "Hello Robert.";

mike -> FromPID ! "Hello Mike.";

joe -> FromPID ! "Hello Joe.";

_ -> FromPID ! "I don't know you."

end,

hello()

end

Trang 37

This is weird

and there's even more

Trang 38

Erlang Culture Shock

Trang 39

io:format('Hi there, ~p!~n', [Name]).

{person, First, Last} = {person, 'Paul', 'Barry'}.

howdy:hi(Last).

Trang 40

Shock #2

Erlang is not object-oriented

Trang 41

Shock #3

Everything in Erlang is immutable

Not just tuples and not just strings

EVERYTHING

Trang 43

Erlang's troublesome = operator

The “=” operator does not mean “assign”

It means “match” or “bind to”

Trang 44

Destructive updates are forbidden

and (as an added twist)

Variables must start with an Uppercase letter:

X Pid Func

No side effects here!

Trang 45

Shock #4

There are no built-in looping constructs

No for and no while

Trang 46

So how do you loop?

Trang 47

doubled = [x*2 for x in alist]

Note: alist and doubled are mutable; Alist and Doubled are not

Trang 48

Shock #5

“Regular” looping is via recursion

Trang 49

robert -> FromPID ! "Hello Robert.";

mike -> FromPID ! "Hello Mike.";

joe -> FromPID ! "Hello Joe.";

_ -> FromPID ! "I don't know you."

end,

hello()

end.

Trang 50

Shock #6

Strings are stored as a

list of integers

Trang 51

Sweet mother of all

things Erlang! Whose bright

idea was that?

Trang 52

Shock #7

There is no if then else

statement

Trang 53

There are if and case expressions

but they're kinda weird

Trang 54

54

Trang 55

With enough pig-headed persistence,

weirdness can be overcome

Trang 56

So who do I call to

help me learn Erlang?

Trang 57

57

Trang 59

Erlang is a language you study

as getting up-to-speed with

Erlang takes time

Trang 61

Some Unique “Characteristics”

Trang 62

Banned by Ericsson in 1998 for

“not being open enough”

Trang 63

http://video.google.com/videoplay?docid=-5830318882717959520

Trang 64

64

Trang 65

This is all great, but

how exactly do I use

Erlang to build a fault-tolerant system?

Trang 66

Learn Three Things

 Create an Erlang process with spawn()

 Send a message to a process with !

 Process a message with receive

Trang 67

robert -> FromPID ! "Hello Robert.";

mike -> FromPID ! "Hello Mike.";

joe -> FromPID ! "Hello Joe.";

_ -> FromPID ! "I don't know you."

end,

hello()

end.

Trang 68

Create the hello() process

Pid = spawn(fun hello_server:hello/0).

<0.38.0>

Trang 69

Send a message to a process with !

Pid ! robert.

robert

Trang 70

A little twist

Pid ! {self(), robert}.

{<0.31.0>,robert}

Trang 71

Messages sent to a process with !

are received by receive

Trang 72

robert -> FromPID ! "Hello Robert.";

mike -> FromPID ! "Hello Mike.";

joe -> FromPID ! "Hello Joe.";

_ -> FromPID ! "I don't know you."

end,

hello()

end.

Trang 73

robert -> FromPID ! "Hello Robert.";

mike -> FromPID ! "Hello Mike.";

joe -> FromPID ! "Hello Joe.";

_ -> FromPID ! "I don't know you."

end,

hello()

end.

Trang 74

robert -> FromPID ! "Hello Robert.";

mike -> FromPID ! "Hello Mike.";

joe -> FromPID ! "Hello Joe.";

_ -> FromPID ! "I don't know you."

end,

hello()

end.

Trang 75

spawn, send (!) then receive

Pid = spawn(fun hello_server:hello/0), Pid ! {self(), robert},

receive

Response ->

Response end.

"Hello Robert."

Trang 76

Things get really interesting when the processes are

linked together with spawn_link()

and the trap_exit signal

Trang 77

Processes that are linked together can

react appropriately to EXIT messages from

each other and what happens next is

controlled by the programmer

Trang 78

as 'hr'

Trang 80

Over time, you'll end up repeating

a lot of your process management code

Trang 81

81

Trang 82

OTP is the jewel in Erlang's crown

Trang 83

So if Erlang's so

cool, how come no one

is using it?

Trang 84

The TIOBE Index

Trang 85

85

Trang 86

Erlang in Telecoms

Trang 87

Erlang in Data

Trang 88

Erlang on the Web

Trang 89

Erlang in Gaming

“…Mochi Media is the world's

largest browser-based games network, with more than 140 million monthly active users and 15,000 games on nearly 40,000 publisher websites…”

Check out Bob Ippolito’s Erlang-Factory talks!

Trang 90

More Erlang in Gaming

Thanks to Malcolm Dowse (of DemonWare, Dublin) for permission to use these

images, which were first delivered as part of Malcolm's talk to Erlang Factory London 2011.

Trang 91

So is Erlang

worth learning? So is Erlang

worth learning?

Trang 92

Yes

Trang 93

Lots more to discover

 Fun with fun

 Bit-strings and bit syntax

 ETS and DETS

 “Live” code updating

 The Mnesia Distributed Database

 The Standard Library

 CEAN

Trang 94

Friendly Community

erlang-questions mailing list

Trang 95

Questions?

Ngày đăng: 14/06/2024, 14:01

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN