7stud

7stud

Metaprogramming Elixir book: what does this mean?

On p. 6, there is this example:

defmodule Math do

  # {:+, [context: Elixir, import: Kernel], [5, 2]}
  defmacro say({:+, _, [lhs, rhs]}) do   #line 4
    quote do
      lhs = unquote(lhs)
      rhs = unquote(rhs)
      result = lhs + rhs
      IO.puts "#{lhs} plus #{rhs} is #{result}"
      result
    end
  end

  # {:*, [context: Elixir, import: Kernel], [8, 3]}
  defmacro say({:*, _, [lhs, rhs]}) do   #line 15
    quote do
      lhs = unquote(lhs)
      rhs = unquote(rhs)
      result = lhs * rhs
      IO.puts "#{lhs} times #{rhs} is #{result}"
      result
    end
  end
end

The text says:

Let’s break down the code. Since we know macros receive the AST representation of the arguments we pass to them, we pattern matched directly on the AST to determine which say definition to invoke. On lines 4 and 15, we can see that macros , like functions, can have multiple signatures. Having the example representation from our quoted results allowed us to easily bind the left- and right-hand side values to variables and print a message accordingly. Too complete the macro, we used quote to return an AST for the caller to replace our Math.say invocations. Here we also used unquote for the first time…

What in the world does:

Having the example representation from our quoted results allowed us to easily bind the left- and right-hand side values to variables and print a message accordingly.

mean?! I recognize the words as English, but they have no intelligible meaning to me.

Most Liked

7stud

7stud

Ahh, I figured it out. The phrase “Having the example representation from our quoted results” refers to:

 # {:+, [context: Elixir, import: Kernel], [5, 2]}

The “quoted results” are on the previous page where the book used quote() on some math expressions.

chrismccord

chrismccord

Creator of Phoenix

can confirm :slight_smile:

blatyo

blatyo

Conduit Core Team

That is my understanding as well. Just for some confirmation.

If you’re looking at the commented out representation of the AST and then imagine how that will be matched against the function head, it’s easy to see how 5 and 2 will be bound to the right-hand and left-hand variables.

Last Post!

7stud

7stud

lol. Thanks!

I would, however, suggest a rewrite. I read that sentence 20 times, and I couldn’t figure out what it meant until just after I posted my question (as sometimes happens!). Maybe something as simple as:

With the AST in the comments as a guide we were easily able to bind the left- and right-hand side values to variables and print a message accordingly .

Where Next?

Popular in Chat/Questions Top

asfand
Hi Everyone, I am a student and know basics of web development, used some php and ruby, but I am not an expert in any. I want to learn E...
New
logesh
Could someone provide a learning path for functional programming for who came from oops background.? Thanks in advance
New
makeitrein
More Ecto questions! More madness! The context: there’s a list of books that I want to filter with a dropdown… The dropdown: looks some...
New
Fl4m3Ph03n1x
Background After following the communitiy suggestion, I bought the Elixir in Action 2nd Edition book and I am about to finish it now. I ...
New
New
aswinmohanme
I recently finished the Udemy course on Elixir and Phoenix and I am thinking about using it for the next project. But I am stuck as how t...
New
jace
I wanted to write a library for interacting with a Web API as a practical way of learning Elixir. However, there seem to be a lot of diff...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 55125 245
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New

We're in Beta

About us Mission Statement