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
Elixir metaprogramming
Search
Felipe Renan
November 29, 2018
Programming
0
44
Elixir metaprogramming
Felipe Renan
November 29, 2018
Tweet
Share
More Decks by Felipe Renan
See All by Felipe Renan
Elixir - Tic Tac Toe
feliperenan
0
45
Git
feliperenan
3
61
Other Decks in Programming
See All in Programming
プロダクトオーナーから見たSOC2 _SOC2ゆるミートアップ#2
kekekenta
0
200
AI によるインシデント初動調査の自動化を行う AI インシデントコマンダーを作った話
azukiazusa1
1
700
SourceGeneratorのススメ
htkym
0
190
組織で育むオブザーバビリティ
ryota_hnk
0
170
責任感のあるCloudWatchアラームを設計しよう
akihisaikeda
3
160
高速開発のためのコード整理術
sutetotanuki
1
390
Grafana:建立系統全知視角的捷徑
blueswen
0
330
OCaml 5でモダンな並列プログラミングを Enjoyしよう!
haochenx
0
140
AIで開発はどれくらい加速したのか?AIエージェントによるコード生成を、現場の評価と研究開発の評価の両面からdeep diveしてみる
daisuketakeda
1
970
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
510
LLM Observabilityによる 対話型音声AIアプリケーションの安定運用
gekko0114
2
420
MDN Web Docs に日本語翻訳でコントリビュート
ohmori_yusuke
0
640
Featured
See All Featured
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
0
320
Google's AI Overviews - The New Search
badams
0
900
Evolving SEO for Evolving Search Engines
ryanjones
0
120
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
60
42k
jQuery: Nuts, Bolts and Bling
dougneiner
65
8.4k
Embracing the Ebb and Flow
colly
88
5k
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
1
910
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
1
110
Learning to Love Humans: Emotional Interface Design
aarron
275
41k
How to Grow Your eCommerce with AI & Automation
katarinadahlin
PRO
0
100
Utilizing Notion as your number one productivity tool
mfonobong
3
220
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
Transcript
Carregando…
Felipe Renan feliperenan @feeliperenan
None
Elixir Metaprogramming
O que é metaprogramação?
“Metaprogramação é a programação de programas que escrevem ou manipulam
outros programas…”
https://pt.wikipedia.org/ “Metaprogramação é a programação de programas que escrevem ou
manipulam outros programas…”
Metaprogramação é código que gera código.
Elixir é escrito em Elixir.
Abstract Syntax Tree (AST) & Macros
Abstract Syntax Tree (AST) Para quem não é familiar com
AST, muitas linguagens de programação tem uma AST. Quando o nosso programa é compilado ou interpretado, o código que escrevemos é transformado em uma arvore antes de ser tornar bytecode ou machine code.
1 + 2 * 3
+ 1 * 2 3 Esse processo geralmente fica escondido
da gente e geralmente nunca precisamos pensar sobre isso. Porém, em Elixir, esse processo fica exposto para o desenvolvedor.
+ 1 * 2, 3
+ 1 * 2, 3 Func Func
+ 1 * 2, 3 Args Args
+(1, *(2, 3))
{func, metadata, args}
iex> quote do: 2 * 3 {:*, _, [2, 3]}
iex> quote do: 2 * 3 {:*, _, [2, 3]}
ARGS Func
iex> quote do: 1 + 2 * 3 {:+, ...,
[1, {:*, ..., [2, 3]}]}
{:+, ..., [1, {:*, ..., [2, 3]}]}
{:+ 1, {:* 2, 3}} {:+, ..., [1, {:*, ...,
[2, 3]}]}
{:+ 1, {:* 2, 3}} +(1, *(2, 3)) {:+, ...,
[1, {:*, ..., [2, 3]}]}
Um pouco mais sobre quote…
iex> name = "Felipe Renan" "Felipe Renan"
iex> name = "Felipe Renan" "Felipe Renan" iex> "My name
is name" "My name is name”
iex> name = "Felipe Renan" "Felipe Renan" iex> "My name
is name" "My name is name” iex> "My name is #{name}” "My name is Felipe Renan”
iex> quote do: 2 * 3 {:*, _, [2, 3]}
iex> num = 1 1 iex> quote do: 2 *
3 {:*, _, [2, 3]}
iex> num = 1 1 iex> quote do: num {:num,
[], Elixir} iex> quote do: 2 * 3 {:*, _, [2, 3]}
iex> num = 1 1 iex> quote do: unquote(num) 1
iex> quote do: num {:num, [], Elixir} iex> quote do: 2 * 3 {:*, _, [2, 3]}
iex> num = 1 1
iex> quote do: 1 + num {:+, …, [1, {:num,
[], Elixir}]} iex> num = 1 1
iex> quote do: 1 + unquote(num) {:+, …, [1, 1]}
iex> quote do: 1 + num {:+, …, [1, {:num, [], Elixir}]} iex> num = 1 1
Macros
iex> require Math iex> Math.say 1 + 2 1 +
2 ------ 3
None
{:+, _, args}
{:+, _, [2, 3]}
None
None
iex> require Math iex> Math.say 1 + 2 1 +
2 ------ 3
Quase tudo são macros in Elixir.
def & defmodule
If & unless
|>
Macros Expansion
ControlFlow.unless 1 == 1, do: "Entrou no bloco!!!” => nil
ControlFlow.unless 1 == 1, do: "Entrou no bloco!!!” => nil
ControlFlow.unless 1 != 1, do: "Entrou no bloco!!!” => “Entrou no bloco!!!”
None
None
None
ControlFlow.unless 1 == 1, do: "Entrou no bloco!!!”
ControlFlow.unless 1 == 1, do: "Entrou no bloco!!!” if !1
== 1, do: "Entrou no bloco!!!"
ControlFlow.unless 1 == 1, do: "Entrou no bloco!!!” if !1
== 1, do: "Entrou no bloco!!!" case !(1 == 1) do x when x in [false, nil] -> nil _ -> "block entered" end
Kernel.SpecialForms As macros definidas aqui são blocos fundamentais no Elixir
que não podem ser sobre-escritas. Elas também representam o fim da estrada das expansões das macros.
Macro rules
* Não escreva macros
* Não escreva macros * Mas aprenda como elas funcionam
Referências • https://pragprog.com/book/cmelixir/metaprogramming-elixir • Elixir in Elixir by Jay Hayes
• Don’t write macros But do learn how they work - Jesse Anderson
Obrigado :)