Write code that writes code with Elixir macros. Macros make metaprogramming attainable and outline the language itself. during this publication, you are going to the right way to use macros to increase the language with quick, maintainable code and proportion performance in methods you by no means inspiration attainable. you will find tips to expand Elixir along with your personal first class beneficial properties, optimize functionality, and create domain-specific languages.

Metaprogramming is one in all Elixir's maximum positive aspects. might be you've gotten performed with the fundamentals or written a number of macros. Now you need to take it to the subsequent point. This publication is a guided sequence of metaprogramming tutorials that take you step-by-step to metaprogramming mastery. you will expand Elixir with robust good points and write swifter, extra maintainable courses in methods unrivaled by means of different languages.

You'll begin with the fundamentals of Elixir's metaprogramming method and learn how macros engage with Elixir's summary structure. Then you are going to expand Elixir together with your personal firstclass positive aspects, write a checking out framework, and realize how Elixir treats resource code as development blocks, instead of rote strains of directions. you will proceed your trip by utilizing complicated code new release to create crucial libraries in strikingly few traces of code. ultimately, you will create domain-specific languages and examine whilst and the place to use your talents effectively.

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

Show description

Read Online or Download Metaprogramming Elixir: Write Less Code, Get More Done (and Have Fun!) PDF

Similar Programming books

Embedded C

For those who are approximately to jot down their first embedded application - or were suffering to profit approximately this significant zone - this e-book will store weeks of attempt.

Cocoa Design Patterns

“Next time a few child exhibits up at my door soliciting for a code evaluate, this can be the ebook that i'm going to throw at him. ”   –Aaron Hillegass, founding father of monstrous 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 via the breadth and class of the Cocoa frameworks.

C++ Primer (5th Edition)

Bestselling Programming educational and Reference thoroughly Rewritten for the recent C++11 average   totally up to date and recast for the newly published C++11 general, this authoritative and finished advent to C++ may also help you to profit the language quickly, and to take advantage of it in sleek, powerful methods.

Operating System Concepts

Preserve speed with the fast-developing international of working platforms Open-source working structures, digital machines, and clustered computing are one of the prime fields of working structures and networking which are quickly altering. With big revisions and organizational alterations, Silberschatz, Galvin, and Gagne’s working process recommendations, 8th version is still as present and proper as ever, supporting you grasp the elemental recommendations of working platforms whereas getting ready your self for today’s rising advancements.

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

Show sample text content

Macros: The construction Blocks of Elixir It’s time to get our arms soiled and spot what macros are all approximately. You’ve been promised customized language gains, so let’s commence small through re-creating a vital Elixir function. From there, we’ll disclose a number of basic macro positive factors and spot how the AST ties in. Re-Creating Elixir’s except Macro Let’s faux for a second that Elixir lacks a integrated until build. In so much languages, we'd need to accept if ! expressions and learn how to settle for this syntactic shortcoming. thankfully for us, Elixir isn’t like such a lot languages. Let’s outline our personal until macro, utilizing if as a development block of our implementation. Macros has to be outlined inside of modules, so let’s outline a ControlFlow module. Head again on your editor and create the next except. exs dossier: macros/unless. exs​Line 1 ​defmodule​ ControlFlow ​do​ ​2  ​defmacro​ ​unless​(expression, ​do​: block) ​do​ ​3  ​quote​ ​do​ ​4  ​if​ ! unquote​(expression), ​do​: ​unquote​(block) ​5  ​end​ ​6  ​end​ ​7 ​end​ Now open up iex within the similar listing and let’s test it out: ​ ​iex>​ c ​"unless. exs"​ ​ [ControlFlow]​​ ​ ​​ ​ ​iex>​ ​require​ ControlFlow ​ ​nil​​​ ​ ​​ ​ ​iex>​ ControlFlow. except​ 2 == five, ​do​: ​"block entered"​ ​ ​"block entered"​​​ ​ ​​ ​ ​iex>​ ControlFlow. except​ five == five ​do​​​ ​ ​... >​ ​"block entered"​​​ ​ ​... >​ ​end​ ​ ​nil​ We needs to first require ControlFlow prior to invoking its macros in circumstances the place the module hasn’t already been imported. for the reason that macros obtain the AST illustration of arguments, we will settle for any legitimate Elixir expression because the first argument to except on-line 2. In our moment argument, we will development fit without delay at the supplied do/end block and bind its AST worth to a variable. take into account, a macro’s function in lifestyles is to absorb an AST illustration and go back an AST illustration, so we instantly commence a quote to come an AST. in the quote, we practice a unmarried line of code new release, remodeling the except key-phrase into an if ! expression: ​ ​quote​ ​do​ ​  ​if​ ! unquote​(expression), ​do​: ​unquote​(block) ​ ​end​ this alteration is often called macro growth. the ultimate AST back from except is accelerated in the caller’s context at assemble time. The produced code will include if ! expressions wherever except used to be used. right here we extensively utilized the unquote macro that we first observed in our Math. say definition. unquote The unquote macro permits values to be injected into an AST that's being outlined. you could contemplate quote/unquote as string interpolation for code. for those who have been increase a string and had to inject the worth of a variable into that string, you are going to interpolate it. an analogous is going whilst developing an AST. We use quote to start producing an AST and unquote to inject values from an outdoor context. this enables the skin certain variables, expression and block, to be injected at once into our if ! transformation. Let’s do that out. We’ll use Code. eval_quoted to without delay evaluation an AST and go back the end result.

Rated 4.92 of 5 – based on 50 votes