elixir study group kickoff meetup

27
Elixir Study Group Kick-off meet-up Hosted by

Upload: yuri-leikind

Post on 20-Jul-2015

118 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Elixir Study Group Kickoff Meetup

Elixir Study GroupKick-off meet-up

Hosted by

Page 2: Elixir Study Group Kickoff Meetup

Why are we here

tonight?

Page 3: Elixir Study Group Kickoff Meetup

Things I know

• We are interested in Elixir.

• We need to learn.

• Learning together is fun.

• Elixir is fun.

• Our meet-ups well be practical: we’ll write code.

Page 4: Elixir Study Group Kickoff Meetup

Things I don’t know

• How often we get together.

• Your background and your level.

• Let’s get acquainted!

Page 5: Elixir Study Group Kickoff Meetup

Thus

• Let’s experiment!

• Everyone is welcome to take initiative and organize

the next session, possibly in a different way!

Page 6: Elixir Study Group Kickoff Meetup

Erlang the platform

and

Elixir

Page 7: Elixir Study Group Kickoff Meetup

ElixirErlang the language

Erlang the VM

Page 8: Elixir Study Group Kickoff Meetup

• In use for ~20 years.

• Designed at Ericsson

• Fault tolerant, scalable, distributed, responsive

systems.

• Erlang the language is a functional and concurrent

programming language with a dynamic type system

• Erlang the language is rather conservative

Page 9: Elixir Study Group Kickoff Meetup

What makes Erlang unique

• Lightweight isolated process as the building block

• Processes communicate via asynchronous

messages

• Error handling approach: LET IT CRASH approach

• OTP

Page 10: Elixir Study Group Kickoff Meetup

Hello, Joe!

Hello, Mike!

Google: “Erlang the movie”

and then: “Erlang the movie the sequel”

Page 11: Elixir Study Group Kickoff Meetup

Elixir• Modern

• Functional

• Concurrent

• Transparent integration with the Erlang world

• Protocol-based polymorphism

• Macros

• Focus on tooling

Page 12: Elixir Study Group Kickoff Meetup

Most importantly…

Elixir

possesses

the gene of

programmer happiness!

Page 13: Elixir Study Group Kickoff Meetup

Functional Programming

for the uninitiated

Page 14: Elixir Study Group Kickoff Meetup

Two pillars

• Higher-order functions

• Immutability

Page 15: Elixir Study Group Kickoff Meetup

words = ["takes", "one", "or", "more",

"functions", "as", "an", "input", "or",

"outputs", "a", "function"]

Enum.max_by words, fn(word) ->

String.length(word) end #=> "functions"

Higher-Order Functions

Page 16: Elixir Study Group Kickoff Meetup

Consequences of

Immutability

• You can’t modify an object in-place. Any

modification produces a new object.

Page 17: Elixir Study Group Kickoff Meetup

Consequences of

Immutability

• There is no question of equality and identity, like in

Java .

Date a = new Date(123);

Date b = new Date(123);

Date c = a;

System.out.println(a == b); //=> false

System.out.println(a.equals(b)); //=> true

System.out.println(a == c); //=> true

Page 18: Elixir Study Group Kickoff Meetup

a = {2014, 9, 11}

b = {2014, 9, 11}

c = a

IO.inspect a == b #=> true

IO.inspect a == c #=> true

IO.inspect b == c #=> true

Equality and Identity the

Functional Way

Page 19: Elixir Study Group Kickoff Meetup

Functions transform data

Pure functions:

Page 20: Elixir Study Group Kickoff Meetup

Functions transform data

defmodule Example do

def word_signature word do

without_spaces = String.strip(word)

downcased = String.downcase(without_spaces)

letters = String.split(downcased, "", trim: true)

sorted = Enum.sort(letters)

Enum.join(sorted)

end

end

IO.inspect Example. word_signature(” Higher ”)

Page 21: Elixir Study Group Kickoff Meetup

Functions transform data

defmodule Example do

def word_signature word do

Enum.join(Enum.sort(String.split(

String.downcase(

String.strip(word)), "", trim: true)))

end

end

IO.inspect Example. word_signature(” Higher ”)

Page 22: Elixir Study Group Kickoff Meetup

OR:

defmodule Example do

def word_signature word do

word |> String.strip

|> String.downcase

|> String.split("", trim: true)

|> Enum.sort

|> Enum.join

end

end

IO.inspect Example. word_signature(” Higher ”)

Page 23: Elixir Study Group Kickoff Meetup

FP & OO

• Functional programming does not contradict Object

Orientedness if objects are immutable. Example:

Scala.

• But neither Erlang nor Elixir has classes

• You separate code and data

Page 24: Elixir Study Group Kickoff Meetup

Abstracting with modules

peter = Person.new(“Peter”,”Peterson”, 20)

IO.inspect Person.can_drink_alcohol?(peter)

#=> false

peter = Person.birthday(peter)

IO.inspect Person.can_drink_alcohol?(peter)

#=> true

Page 25: Elixir Study Group Kickoff Meetup

Abstracting with modules

defmodule Person do

defstruct firstname: nil, lastname: nil, age: 0

@legal_drinking_age 21

def new(fname, lname, age) do

%Person{firstname: "Peter",

lastname: "Peterson", age: 20}

end

Page 26: Elixir Study Group Kickoff Meetup

Abstracting with modules

def can_drink_alcohol?(person) do

person.age >= @legal_drinking_age

end

def birthday(person) do

%{person | age: person.age + 1}

end

end

Page 27: Elixir Study Group Kickoff Meetup

Exercises:

https://github.com/belgian-elixir-study-group/meetup-materials

git clone

https://github.com/belgian-elixir-study-group/meetup-

materials.git

WiFi:

DevSpace-5GHz

DevSpace-2.4GHz

passwd: devspace2012ftw!

twitter:

@elixir_be

@xavierdefrang

@less_software

Elixir docs:

http://elixir-lang.org/docs/stable/elixir/