7stud

7stud

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.

Showing Posts 1 to 4

7stud

7stud OP

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.

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.

chrismccord

chrismccord

Creator of Phoenix

can confirm :slight_smile:

7stud

7stud OP

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 .

— All posts loaded —

Where Next? Top

Trending in Chat/Questions Top

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews