28
Introduction to Functional Programming with Elixir

Introduction to functional programming, with Elixir

Embed Size (px)

Citation preview

Page 1: Introduction to functional programming,  with Elixir

Introduction to Functional Programmingwith Elixir

Page 2: Introduction to functional programming,  with Elixir

Unlearn you some OOP for great good

Page 3: Introduction to functional programming,  with Elixir

● OOP is not the only good way to design software

● Assignment & Control flow are not essential to programming

● Locks & Semaphores are not essential to concurrency

Page 4: Introduction to functional programming,  with Elixir

Learn you some Functional Programming for good

Page 5: Introduction to functional programming,  with Elixir

Functional Programming

● Another programming paradigmo Functions as first class citizenso Higher order functionso Immutable datao Pure functions

Page 6: Introduction to functional programming,  with Elixir

Pure Functions

● Stateless● Produces the same output always for

the same input● No side effects

Page 7: Introduction to functional programming,  with Elixir

Functional Programming Languages

● Haskell● Erlang/Elixir● Common Lisp and more

● Scala● JavaScript● more

Page 8: Introduction to functional programming,  with Elixir

The functional programming Boom

● Physical constraints● Need for:

o Simple concurrency and parallelismo Easier vertical and horizontal scalingo Optimal utilization of resources

Page 9: Introduction to functional programming,  with Elixir

Emerging Languages

● Go● Scala● Erlang● Elixir● Rust● Haskell● Nimand more

Page 10: Introduction to functional programming,  with Elixir

Elixir?

● Built on top of the Erlang VM● Compiles down to BEAM bytecode

Page 11: Introduction to functional programming,  with Elixir

Wth is Erlang?

● Designed for scalability and real-time systems

● Functional● Simplifies concurrent programming● Fault-tolerant (‘let it crash’ philosophy)● Bytecode runs on the Erlang VM

Page 12: Introduction to functional programming,  with Elixir

Erlang is used by

● Facebook (Chat)● Amazon● Whatsapp● Yahoo!● Heroku● Github● and more

Page 13: Introduction to functional programming,  with Elixir

Story of Whatsapp

● 2 Million connections on a single node● 450 Million users handled by 32

engineers● Acquired by Facebook for $19 Billion● Backend powered by Erlang

Page 14: Introduction to functional programming,  with Elixir

Elixir

● Dynamic● Functional● Built on top of the Erlang VM

o implies Concurrent, Distributed and Fault-tolerant

● 1.0.0 was released in Sep 2014● Created by Jose Valim

Page 15: Introduction to functional programming,  with Elixir

Why use Elixir?

● All the benefits of Erlang● Easily reuse Erlang libraries

o No additional performance costs● Better tooling, which allows for greater

productivity● Better syntax● Simplified metaprogramming

Page 16: Introduction to functional programming,  with Elixir

Concurrency!

Concurrency in erlang/elixir vs other languages is like branching in git vs subversion.

● Its simple and cheap

Page 17: Introduction to functional programming,  with Elixir

Everything is an expression

Page 18: Introduction to functional programming,  with Elixir

World of Elixir

DataModules

Processes

Page 19: Introduction to functional programming,  with Elixir

Datatypes

● Atoms● Integers● Floating point numbers● Binaries● Tuples● Lists● Strings and Character lists● Maps, Hashdicts and Keyword Lists

Page 20: Introduction to functional programming,  with Elixir

Modules

● Means of organizing code● Contains all named functions

Page 21: Introduction to functional programming,  with Elixir

Functions

● Elixir functions are defined by:o Name

o Arity● Types of functions

o Anonymous & Inline Anonymous inherits scope

o Named does not inherit scope

Page 22: Introduction to functional programming,  with Elixir

Pattern Matching

● Assert, not assign● Equals sign isn’t really assignment, its

more like an assertion● Behaviour of tuples, lists and variables

in pattern matching is important to understand

● Ignore values using _● Force use variables using ^

Page 23: Introduction to functional programming,  with Elixir

Pipeline operator |>

● Formulating transformation of data● Similar to Linux pipes● Making functions/methods composable● Data |> Method 1 |> Method 2 |>

Method 3

Page 24: Introduction to functional programming,  with Elixir

Live Examples

● Sum, Multiply● Factorial, Fibonacci, GCD● List operations● File operations● Maps, Typed maps

Page 25: Introduction to functional programming,  with Elixir

Tooling

● Mix, a wonderful build system● First class documentation

o Inline documentationo Doctests

● Dependency Management using Mix● Testing library included

Page 26: Introduction to functional programming,  with Elixir

Topics not covered

● Mix, `maven` for Elixiro compatible with hex, the `maven` for Erlang

● OTP● Metaprogramming● Protocols● Structs● Streams, Enumerables, Collectables

Page 27: Introduction to functional programming,  with Elixir

Elixir frameworks

● Phoenix: Web framework● Ecto: LINQ for Elixir

More: Awesome Elixir List

Page 28: Introduction to functional programming,  with Elixir

Questions?

Interested? You can read more here: https://github.com/hashd/LearnStack/blob/master/notes/elixir.md