Metaprogramming Elixir: Write Less Code, Get More Done (and Have Fun!)

Write code that writes code with Elixir macros. Macros make metaprogramming attainable and outline the language itself. during this publication, you are going to easy methods to use macros to increase the language with quickly, maintainable code and percentage performance in methods you by no means suggestion attainable. you will find find out how to expand Elixir along with your personal firstclass good points, optimize functionality, and create domain-specific languages.

Metaprogramming is one among Elixir's maximum good points. might be you've got performed with the fundamentals or written a couple of macros. Now you need to take it to the subsequent point. This ebook is a guided sequence of metaprogramming tutorials that take you step-by-step to metaprogramming mastery. you are going to expand Elixir with robust positive factors and write quicker, extra maintainable courses in methods unequalled by way of different languages.

You'll commence with the fundamentals of Elixir's metaprogramming method and learn the way macros have interaction with Elixir's summary layout. Then you will expand Elixir along with your personal top quality positive factors, write a checking out framework, and realize how Elixir treats resource code as development blocks, instead of rote traces of directions. you will proceed your trip through the use of complicated code iteration to create crucial libraries in strikingly few traces of code. eventually, you are going to create domain-specific languages and research while and the place to use your talents effectively.

When you are performed, you've mastered metaprogramming, received insights into Elixir's internals, and feature the boldness to leverage macros to their complete strength on your personal projects.

Show description

Preview of Metaprogramming Elixir: Write Less Code, Get More Done (and Have Fun!) PDF

Similar Programming books

Joe Celko's SQL for Smarties: Advanced SQL Programming Third Edition (The Morgan Kaufmann Series in Data Management Systems)

SQL for Smarties used to be hailed because the first ebook dedicated explicitly to the complicated ideas had to remodel an skilled SQL programmer into a professional. Now, 10 years later and within the 3rd version, this vintage nonetheless reigns best because the publication written by means of an SQL grasp that teaches destiny SQL masters.

Designing Audio Effect Plug-Ins in C++: With Digital Audio Signal Processing Theory

Not only one other theory-heavy electronic sign processing publication, nor one other uninteresting build-a-generic-database programming booklet, Designing Audio influence Plug-Ins in C++ provides every thing you every little thing you must be aware of to just do that, together with absolutely labored, downloadable code for dozens audio impression plug-ins and virtually awarded algorithms.

Effective C++: 55 Specific Ways to Improve Your Programs and Designs (3rd Edition)

“Every C++ specialist wishes a replica of powerful C++. it's an absolute must-read for a person deliberating doing severe C++ improvement. If you’ve by no means learn powerful C++ and also you imagine you recognize every little thing approximately C++, re-examine. ”— Steve Schirripa, software program Engineer, Google “C++ and the C++ neighborhood have grown up within the final fifteen years, and the 3rd variation of potent C++ displays this.

Cocoa Design Patterns

“Next time a few child exhibits up at my door inquiring for a code evaluation, this is often the publication that i'm going to throw at him. ”   –Aaron Hillegass, founding father of significant Nerd Ranch, Inc. , and writer of Cocoa Programming for Mac OS X   Unlocking the secrets and techniques of Cocoa and Its Object-Oriented Frameworks   Mac and iPhone builders are usually crushed by way of the breadth and class of the Cocoa frameworks.

Extra info for Metaprogramming Elixir: Write Less Code, Get More Done (and Have Fun!)

Show sample text content

Exs"​ ​ ( ​  ​def​(t(locale, direction, binding \\ [])) ​  [[[​def​(t(​"fr"​, ​"flash. hello"​, bindings)) ​do​ ​  ((((​""​ <> ​"Salut "​) <> to_string(Dict. fetch! (bindings, :first))) <> ​" "​) <> ​  to_string(Dict. fetch! (bindings, :last))) <> ​"! "​ ​  ​end​, ​def​(t(​"fr"​, ​"flash. bye"​, bindings)) ​do​ ​  ((​""​ <> ​"Au revoir, "​) <> to_string(Dict. fetch! (bindings, :name))) <> ​"! "​ ​  ​end​], [​def​(t(​"fr"​, ​"users. title"​, bindings)) ​do​ ​  ​""​ <> ​"Utilisateurs"​ ​  ​end​]], [[​def​(t(​"en"​, ​"flash. hello"​, bindings)) ​do​ ​  ((((​""​ <> ​"Hello "​) <> to_string(Dict. fetch! (bindings, :first))) <> ​" "​) <> ​  to_string(Dict. fetch! (bindings, :last))) <> ​"! "​ ​  ​end​, ​def​(t(​"en"​, ​"flash. bye"​, bindings)) ​do​ ​  ((​""​ <> ​"Bye, "​) <> to_string(Dict. fetch! (bindings, :name))) <> ​"! "​ ​  ​end​], [​def​(t(​"en"​, ​"users. title"​, bindings)) ​do​ ​  ​""​ <> ​"Users"​ ​  ​end​]]] ​  ​def​(t(_locale, _path, _bindings)) ​do​ ​  {:error, :no_translation} ​  ​end​ ​ ) ​ [I18n]​​ ​ ​​ ​ ​iex>​ I18n. t(​"en"​, ​"flash. hello"​, first: ​"Chris"​, final: ​"McCord"​) ​ ​"Hello Chris Mccord! "​​​ ​ ​​ ​ ​iex>​ I18n. t(​"fr"​, ​"flash. hello"​, first: ​"Chris"​, final: ​"McCord"​) ​ ​"Salut Chris McCord! "​​​ ​ ​​ ​ ​iex>​ I18n. t(​"en"​, ​"users. title"​) ​ ​"Users"​ The ever-helpful Macro. to_string unearths the compile-time optimized functionality our bodies for every t/3 definition. we will be able to see that our interpolated ASTs accurately multiplied to easy string concatenation operations. this sort of functionality optimization isn’t attainable in so much languages and gives a considerable functionality bring up over hoping on average expressions at runtime. you can be thinking about how we have been in a position to at once reference the bindings variable inside interpolate with no utilizing the var! macro. We didn’t need to fear approximately hygiene right here as the quote blocks are all within the comparable module, in order that they percentage a similar context. With the tough components out of ways, let’s recognize our paintings. the entire Translator Module Let’s have a look our accomplished library to work out how all the things healthy jointly. whereas you’re reviewing the code, take into consideration how every one step of our metaprogramming rubric drove the layout judgements of our ultimate implementation. advanced_code_gen/translator. exs​ ​defmodule​ Translator ​do​ ​  ​  ​defmacro​ __using__(_options) ​do​ ​  ​quote​ ​do​ ​  Module. register_attribute __MODULE__, :locales, gather: ​true​, ​  persist: ​false​ ​  ​import​ ​unquote​(__MODULE__), purely: [locale: 2] ​  ​@before_compile​ ​unquote​(__MODULE__) ​  ​end​ ​  ​end​ ​  ​  ​defmacro​ __before_compile__(env) ​do​ ​  compile(Module. get_attribute(env. module, :locales)) ​  ​end​ ​  ​  ​defmacro​ locale(name, mappings) ​do​ ​  ​quote​ bind_quoted: [name: identify, mappings: mappings] ​do​ ​  ​@locales​ {name, mappings} ​  ​end​ ​  ​end​ ​  ​  ​def​ compile(translations) ​do​ ​  translations_ast = ​for​ {locale, resource} <- translations ​do​ ​  deftranslations(locale, ​""​, resource) ​  ​end​ ​  ​  ​quote​ ​do​ ​  ​def​ t(locale, course, binding \\ []) ​  ​unquote​(translations_ast) ​  ​def​ t(_locale, _path, _bindings), ​do​: {:error, :no_translation} ​  ​end​ ​  ​end​ ​  ​  ​defp​ deftranslations(locale, current_path, translations) ​do​ ​  ​for​ {key, val} <- translations ​do​ ​  direction = append_path(current_path, key) ​  ​if​ key-phrase.

Download PDF sample

Rated 4.92 of 5 – based on 38 votes