Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Introduction to Elixir
Search
Sheharyar Naseer
February 27, 2016
Programming
3
1.3k
Introduction to Elixir
My talk on Elixir that I delivered at IslamabadJS.
Sheharyar Naseer
February 27, 2016
Tweet
Share
More Decks by Sheharyar Naseer
See All by Sheharyar Naseer
Supercharging Development with Docker
sheharyar
0
160
Using Docker for your Applications
sheharyar
1
120
Building LLM Apps with Google Vertex AI and PaLM
sheharyar
0
310
Cloud Basics: Google App Engine
sheharyar
0
230
Docker on Google Cloud
sheharyar
0
430
Self-Paced GCP for Students
sheharyar
1
280
Powering Real-time Collaboration with Operational Transform
sheharyar
1
1.3k
Building a Real-time Collaborative Editor with Phoenix
sheharyar
1
1.9k
Self-Healing Applications with Kubernetes
sheharyar
0
640
Other Decks in Programming
See All in Programming
AI時代におけるSRE、 あるいはエンジニアの生存戦略
pyama86
6
1.1k
EventSourcingの理想と現実
wenas
6
2.3k
タクシーアプリ『GO』のリアルタイムデータ分析基盤における機械学習サービスの活用
mot_techtalk
4
1.4k
NSOutlineView何もわからん:( 前編 / I Don't Understand About NSOutlineView :( Pt. 1
usagimaru
0
330
AWS IaCの注目アップデート 2024年10月版
konokenj
3
3.3k
LLM生成文章の精度評価自動化とプロンプトチューニングの効率化について
layerx
PRO
2
190
What’s New in Compose Multiplatform - A Live Tour (droidcon London 2024)
zsmb
1
470
Streams APIとTCPフロー制御 / Web Streams API and TCP flow control
tasshi
2
350
リアーキテクチャxDDD 1年間の取り組みと進化
hsawaji
1
220
レガシーシステムにどう立ち向かうか 複雑さと理想と現実/vs-legacy
suzukihoge
14
2.2k
TypeScript Graph でコードレビューの心理的障壁を乗り越える
ysk8hori
2
1.1k
Amazon Bedrock Agentsを用いてアプリ開発してみた!
har1101
0
330
Featured
See All Featured
StorybookのUI Testing Handbookを読んだ
zakiyama
27
5.3k
A Modern Web Designer's Workflow
chriscoyier
693
190k
Building Applications with DynamoDB
mza
90
6.1k
Happy Clients
brianwarren
98
6.7k
How to train your dragon (web standard)
notwaldorf
88
5.7k
Side Projects
sachag
452
42k
Become a Pro
speakerdeck
PRO
25
5k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
665
120k
GraphQLの誤解/rethinking-graphql
sonatard
67
10k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
42
9.2k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
109
49k
How To Stay Up To Date on Web Technology
chriscoyier
788
250k
Transcript
ELIXIR INTRODUCTION TO
SHEHARYAR NASEER sheharyarn
STORY TIME
- Rails is Immensely Popular - Developer productivity is through
the roof - But has concurrency issues - José Valim to make it threadsafe BACK IN THE DAY
- Can never fully guarantee it - Looked towards Functional
Languages - Erlang! THREADSAFE RAILS
- Fast and Powerful - Immutable - Fault-tolerant - Concurrent
and Distributed ERLANG-LOVE
JOSÉ VALIM I liked everything I saw in Erlang, but
hated the things I didn’t see “
- Awful Syntax - Lack of Metaprogramming - No Polymorphism
- No proper build tools ERLANG-HATE
Decided to build his own language on the ERLANG VM
ELIXIR IS BORN
ELIXIR-LANG.COM A dynamic, functional language designed for building scalable and
maintainable applications “
$ brew install elixir $ iex iex> IO.puts "hello world!"
hello world! GETTING STARTED
iex> 99 # integer iex> 4.2 # float iex> true
# boolean iex> :atom # atom iex> "i ♥ elixir" # string iex> [1, 2, 3] # list iex> {4, 5, 6} # tuple BASIC TYPES
iex> 10 + 3 => 13 iex> 10 / 5
=> 2.0 iex> "hello" <> " world" => "hello world" iex> "num: #{4 * 5}" => "num: 20" iex> true and false => false iex> nil || :something => :something iex> [1, 2, 3] ++ [4, 5] => [1, 2, 3, 4, 5] iex> [1, 2, 3] —- [2] => [1, 3] BASIC OPERATORS
iex> double = fn x -> x * 2 end
iex> double.(7) 14 iex> Enum.map [1,2,3,4,5], double [2, 4, 6, 8, 10] HIGHER ORDER FUNCTIONS
iex> person = %{name: "Sheharyar", occupation: :developer} iex> person.name #
=> "Sheharyar" iex> person[:occupation] # => :developer iex> person[:other] # => nil iex> person.other # => ** (KeyError) key :other not found MAPS & KEYWORD LISTS
iex> keywords = [{:a, 1}, {:b, 2}, {:c, 3}] [a:
1, b: 2, c: 3] iex> keywords[:b] 2 iex> keywords = [a: 1, b: 2, a: :something_else] [a: 1, b: 2, a: :something_else] MAPS & KEYWORD LISTS
iex> my_list = [1, 2, 3, 4, 5] iex> List.delete(my_list,
4) [1, 2, 3, 5] iex> my_list [1, 2, 3, 4, 5] IMMUTABILITY
iex> my_list = [1, 2, 3, 4, 5] iex> my_list
= List.delete(my_list, 4) [1, 2, 3, 5] iex> my_list [1, 2, 3, 5] IMMUTABILITY
iex> x = 10 10 iex> 10 = x 10
PATTERN MATCHING
iex> x = 10 10 iex> 10 = x 10
iex> 20 = x ** (MatchError) no match of right hand side value: 10 PATTERN MATCHING
iex> {a, b, c} = {"horror", :friday, 13} iex> a
# => "horror" iex> b # => :friday iex> c # => 13 PATTERN MATCHING
some_data = [:fire, {32, 45, 73}, %{name: "Foo", desc: "Bar"}]
iex> [_, {a, b, _}, %{name: name}] = some_data iex> a # => 32 iex> b # => 45 iex> name # => "Foo" iex> _ # => ** (CompileError) iex: unbound variable _ PATTERN MATCHING
some_data = [:fire, {32, 45, 73}, %{name: "Foo", desc: "Bar"}]
iex> [:water, {a, b, _}, %{name: name}] = some_data ** (MatchError) no match of right hand side value PATTERN MATCHING
result = Directory.read_filenames() case result do {:ok, files} -> Enum.map(files,
&IO.puts/1) {:error, message} -> IO.puts "Can't read files: #{message}" end PATTERN MATCHING
defmodule Area do def rectangle(l, w) do l * w
end end iex> Area.rectangle(2, 3) 6 MODULES
defmodule Area do def rectangle(l, w), do: l * w
def square(l), do: rectangle(l, l) def circle(r), do: 3.14 * square(r) end Area.square(4) # => 16 Area.circle(5) # => 78.5 MODULES
iex> circle_radii = [1, 2, 3] iex> Enum.map circle_radii, fn
r -> Area.circle(r) end [3.14, 12.56, 28.26] CAPTURE OPERATOR
iex> circle_radii = [1, 2, 3] iex> Enum.map circle_radii, fn
r -> Area.circle(r) end [3.14, 12.56, 28.26] iex> Enum.map circle_radii, &Area.circle/1 [3.14, 12.56, 28.26] CAPTURE OPERATOR
defmodule Rectangle do defstruct [:length, :width] def area(rect), do: rect.length
* rect.width end iex> rect = %Rectangle{length: 10, width: 5} iex> Rectangle.area(rect) 50 STRUCTS
# Double, reverse and then print items in a list
my_list = [1, 2, 3, 4, 5] doubled_list = Enum.map(my_list, fn x -> x * 2 end) reverse_list = Enum.reverse(doubled_list) Enum.map(reverse_list, &IO.puts/1) PIPE OPERATOR
# Double, reverse and then print items in a list
Enum.map( Enum.reverse( Enum.map([1, 2, 3, 4, 5], fn x -> x * 2 end) ), &IO.puts/1 ) PIPE OPERATOR
# Double, reverse and then print items in a list
[1, 2, 3, 4, 5] |> Enum.map(fn x -> x*2 end) |> Enum.reverse |> Enum.map(&IO.puts/1) PIPE OPERATOR
defprotocol JSON do def encode(item) end POLYMORPHISM defimpl JSON, for:
String do def encode(s), do: "\"#{s}\"" end defimpl JSON, for: CustomModule do def encode(item) do # do something end end
METAPROGRAMMING - via Macros - Elixir is written in Elixir!
- Almost everything is a Macro def, defmodule, if-else, and, or, !, |>, and so much more
defmacro macro_unless(clause, expression) do quote do if(!unquote(clause), do: unquote(expression)) end
end METAPROGRAMMING
$ mix new my_app $ mix deps.get $ mix test
$ mix run $ iex -S mix MIX
THE FUTURE
- Growing at an amazing rate - Backed by major
players from Rails, Erlang & Node.js communities - Erlang libraries at disposal COMMUNITY
- Elixir’s Rails - Super-fast and developer-friendly - Requests completed
in microseconds (µs) - Tons of guides and resources already available PHOENIX FRAMEWORK
- PHP with Open Source - Rails with Developer Productivity
- Elixir with Performance (without sacrificing developer happiness) THE NEXT REVOLUTION
- Official Website (elixir-lang.com) - Awesome Books - Elixir in
Action - Programming Elixir - Screencasts - Elixir Sips (elixirsips.com) - Learn Elixir (learnelixir.tv) RESOURCES
QUESTIONS? sheharyarn
[email protected]