Top Banner
New software for new hardware Elixir meetup 4.0, Lviv, December 17, 2016 Elixir introduction
55

Elixir intro

Apr 16, 2017

Download

Software

Anton Mishchuk
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Elixir intro

New software

for new hardware

Elixir meetup 4.0, Lviv, December 17, 2016

Elixir introduction

Page 2: Elixir intro

Hello!I am Anton Mishchuk

- Ruby developer at

Matic Insurance

- Elixir fan

- Author and maintainer of

ESpec BDD test framework

Github: antonmi

2

Page 3: Elixir intro

What I’d tell you

▪ No pictures, sorry

▪ No code samples

▪ No live coding

▪ Lots of philosophy

▪ Few words about Erlang and Elixir

3

Page 4: Elixir intro

The plan

▪ Software crisis?

▪ Resurrection of FP

▪ Erlang

▪ Erlang concurrency model

▪ Elixir

▪ Elixir is not Ruby

▪ You should try Elixir

4

Page 5: Elixir intro

1Software crisis?

5

Page 6: Elixir intro

Common characteristic of

present software

▪ low quality

▪ very inefficient

▪ difficult to maintain

6

Page 7: Elixir intro

Nathan Myhrvold’s laws

▪ “Software is a gas”

▪ “Software grows until it becomes

limited by Moore’s Law”

▪ “Software growth makes Moore’s

Law possible”

▪ “Software is only limited by

human ambition and expectation”

7

Nathan Myhrvold, formerly CTO at Microsoft

Page 8: Elixir intro

Niklaus Wirth’s Law:

“Software gets slower

faster than hardware

gets faster”

8

Niklaus Wirth, computer scientist,

designer of several PL (incl. Pascal)

Page 9: Elixir intro

What next?

9

Page 10: Elixir intro

▪ Better hardware?

or

▪ Better software?

10

Page 11: Elixir intro

We are experiencing a period

of “dramatic changes”

▪ New hardware!

▪ New software!

11

Page 12: Elixir intro

New hardware:

▪ Multicore systems

▪ ASIC (Application-specific

integrated circuit)

▪ FPGA (Field-programmable gate

array)

12

“The von Neumann Syndrome”

Page 13: Elixir intro

New software:

Multicore crisis:

To take advantage of the additional

cores most applications have to be

rewritten

13

Page 14: Elixir intro

2Resurrection of FP

14

Page 15: Elixir intro

Functional programming

FP is a programming paradigm

that treats computation as the

evaluation of mathematical

functions.

FP: Data structures manipulated

through functions

OOP: Objects manipulated through

methods

15

Page 16: Elixir intro

FP concepts

▪ High-order functions

▪ Pure functions

▪ Recursions

▪ Lazy evaluations

▪ Currying

▪ Closures

16

Page 17: Elixir intro

History

FP:

▪ Lisp, 1950s

▪ APL, 1960s

▪ Haskell, late 80s

▪ Erlang, 1986

OOP:

▪ Simula, 1960s

▪ Smalltalk, 1970s

▪ Java, Ruby, 1995

17

Page 18: Elixir intro

CPU:

▪ Single-processor machines

▪ 50%/year clock speed growth

▪ From ~10MHz to ~1GHz

RAM:

▪ Relatively expensive and slow

▪ $100/1Mb in 1990. $1/1Mb in 2000

▪ CPU cache

OOP dominating in 90s

18

Page 19: Elixir intro

▪ Is OOP the only right way to to

create software?

▪ Is OOP the RIGHT way?

19

Page 20: Elixir intro

Why OO sucks?

(c) Joe Armstrong

20

Page 21: Elixir intro

▪ Functions do things

▪ Data structures just are

Since functions and data structures

are completely different types of

animal it is fundamentally

incorrect to lock them up in the

same cage.

Objection 1:

Data structure and functions

should not be bound together

21

Page 22: Elixir intro

▪ Should “time” to be an instance

of some type?

▪ Should a simple integer number to

be an instance of some type?

Objection 2:

Everything has to be an object

22

Page 23: Elixir intro

▪ lots of types with limited amount

of specific functions

Isn't it better have a smallish

number of ubiquitous data types and

a large number of small functions

that work on them?

Objection 3:

Data type definitions are

spread out all over the place

23

Page 24: Elixir intro

The state is hidden and visible

only through access functions.

State is the root of all evil!

Objection 4:

Objects have private state

24

Page 25: Elixir intro

Why OO was popular?

(c) Joe Armstrong

25

Page 26: Elixir intro

▪ It was thought to be easy to

learn.

▪ It was thought to make code reuse

easier.

▪ It was hyped.

▪ It created a new software

industry.

Why OO was popular?

(c) Joe Armstrong

26

Page 27: Elixir intro

I see no evidence of 1 and 2.

Reasons 3 and 4 seem to be

the driving force behind the

technology.

27

Page 28: Elixir intro

If a language technology is so bad

that it creates a new industry to

solve problems of its own making then

it must be a good idea for the guys

who want to make money.

28

Page 29: Elixir intro

3Erlang

29

Page 30: Elixir intro

▪ Elixir compiler is written in

Erlang

▪ Both Elixir and Erlang compiles

into BEAM byte code

Elixir == Erlang

30

Page 31: Elixir intro

▪ Ericsson, 1986

▪ Joe Armstrong, Robert Virding and

Mike Williams

▪ Open-sourced in 1998

▪ Telecom systems (telephone

switches)

▪ The latest version is 19.1

Erlang

31

Page 32: Elixir intro

▪ Fault tolerance

▪ Scalability

▪ Distribution

▪ Live update

Erlang buzzwords

32

Page 33: Elixir intro

▪ Ericsson

▪ WhatsApp

▪ Facebook

▪ Amazon

▪ Bet365

▪ Demonware

▪ ...

Companies using Erlang

33

Page 34: Elixir intro

▪ Modules

▪ Functions

▪ Primitive data types:

atoms, integers, floats, tuples,

lists, maps, strings, binaries,

records, references, pids, ports,

funs.

Erlang components

34

Page 35: Elixir intro

4Erlang concurrency model

35

Page 36: Elixir intro

▪ Native threads

▪ Green threads

▪ Event loop

It is not about:

36

Page 37: Elixir intro

▪ lightweight processes

▪ with isolated memory

▪ communication via messages

It is about “actors”

37

Page 38: Elixir intro

In response to a message actor

receives, can concurrently:

▪ send a finite number of messages

to other actors

▪ create a finite number of new

actors

▪ Set the “state” to be used for

the next message it receives

Actors

38

Page 39: Elixir intro

▪ Controlled by BEAM VM

▪ ~ 1µs to create process

▪ ~ 1kB memory per process

▪ ~ 1µs to pass a message

Erlang actors

39

Page 40: Elixir intro

▪ encapsulate state

▪ communicate with other actors by

exchanging messages

More OO than objects

40

Page 41: Elixir intro

▪ Tools

▪ Libraries

Design principles:

▪ Workers and Supervisors

▪ GenServer, GenFSM

▪ Event Handler and Manager

▪ Application

Erlang OTP

41

Page 42: Elixir intro

5Elixir

42

Page 43: Elixir intro

▪ Jose Valim, 2012

▪ Current version 1.3

▪ Hex.pm

▪ >3000 packages, ~20000 down/day

▪ Elixir Conf EU, Elixir Conf US

▪ Elixir Companies

▪ Phoenix web framework

(2M websocket conns)

Elixir

43

Page 44: Elixir intro

What is Elixir?

44

Elixir is Erlang with pretty syntax

and metaprogramming

▪ Consistent stdlib

▪ Pipe operator

▪ Tools (mix, iex, logger)

Page 45: Elixir intro

Elixir metaprogramming

45

▪ Easy code sharing

▪ Perfect Ruby-like DSLs

Page 46: Elixir intro

6Elixir is not Ruby

46

Page 47: Elixir intro

Elixir is functional

47

▪ Function is a primary Elixir

abstraction.

▪ Data is immutable

▪ Minimal state manipulation

Page 48: Elixir intro

Ruby is OO language

with some FP features

48

▪ Procs, lambdas

▪ No FP optimizations

▪ Data is mutable

Page 49: Elixir intro

Concurrency model

49

In Ruby, one moves through a

monolithic call stack, manipulating

objects. Threads or fibers uses the

same memory heap.

Elixir processes have their own

heaps, and the VM collects process'

garbage independently of each

other.

Page 50: Elixir intro

What is the same?

50

“programmer happiness” philosophy:

▪ Pretty syntax

▪ Good documentation

▪ Tools for easy development

Page 51: Elixir intro

7You should try Elixir

51

Page 52: Elixir intro

Think about future software

52

▪ Multi-core computers

▪ IoT with up to 30 billion devices

to be online by 2020

Page 53: Elixir intro

Double paradigm switch

53

▪ Functional programming

▪ Actor concurrency model

Page 54: Elixir intro

You’ll found it exciting

54

▪ It’s easy to get started

▪ Lots of tutorials

▪ Nice documentation

▪ Great community

Page 55: Elixir intro

Thanks!ANY QUESTIONS?

55