versilov

versilov

In my Matrex project I need to define a set of functions for different data types and I need to do it serveral times.

Now I have this code, which works but looks cumbersome:

defmodule Array
  [
    float64: {:float, 64},
    float32: {:float, 32},
    byte: {:integer, 8},
    int16: {:integer, 16},
    int32: {:integer, 32},
    int64: {:integer, 64}
  ]
  |> Enum.each(fn {type, {spec, bits}} ->
    defp list_to_binary([e | tail], unquote(type)),
      do: <<e::unquote(spec)()-little-unquote(bits), list_to_binary(tail, unquote(type))::binary>>
  end)
end

I want to make a macro, which would allow to do the same thing with this syntax:

  deftyped list_to_binary([e | tail], type), do:
    <<e::type_spec, list_to_binary(tail, type)::binary>>

So, generally I want to:

  1. Hide looping through types in the macro
  2. Use one-term binary typespec: ::typespec instead of ::unquote(spec)()-little-unquote(bits)

My last attempt is below. It compiles, but does not work.

  defmacro deftyped(call, expr \\ nil) do
    [
      float64: {:float, 64},
      float32: {:float, 32},
      byte: {:integer, 8},
      int16: {:integer, 16},
      int32: {:integer, 32},
      int64: {:integer, 64}
    ]
    |> Enum.each(fn {type, {spec, bits}} ->
      quote do
        def(unquote(call), unquote(expr))
      end
    end)
  end

Please, tell me what is possible to accomplish here and how?

Showing Posts 1 to 8

OvermindDL1

OvermindDL1

This isn’t going to return anything, perhaps you means Enum.map?

versilov

versilov OP

Perhaps you are right.
But it does not help to make it work.

In this variant (no matter .each or .map) it does defines functions, but does not pass type, spec & bits values to the definitions, so, we cannot do pattern matching on type parameter and use correct binary spec.

defmacro deftyped(call, do: block) do
    quote do
      [
        float64: {:float, 64},
        float32: {:float, 32},
        byte: {:integer, 8},
        int16: {:integer, 16},
        int32: {:integer, 32},
        int64: {:integer, 64}
      ]
      |> Enum.each(fn {type, {spec, bits}} ->
        def unquote(call), do: unquote(block)
      end)
    end
  end
josevalim

josevalim

Creator of Elixir

Defining functions is a side-effect, so each is fine.

NobbZ

NobbZ

def isn’t called, but unquoted only, so its sideeffect won’t fire. So to my understanding we need to use map here to get the def into the hosting modules AST and then actually “run” it there in the new context.

I do see your point without the quoting though.

edit

Nevermind, I just realise we have the each inside the quoting, therefore my comment is irrelevant :wink:

josevalim

josevalim

Creator of Elixir

My suggestion would be to avoid the macro for definitions altogether in favor of a simple type_and_size macro and then organize your codebase like this:

defmodule Matrex do
  # Functions that do not depend on the type

  def foo(...) do
  def bar(...) do
  def baz(...) do

  # Headers of functions that depend on the type

  @doc "..."
  def concatenate(matrex, foo)

  # Implementation of functions that depend on the type

  defmacrop type_and_size() do
    {type, size} = Module.get_attribute(__CALLER__.module, :type_and_size)
    quote do
      size(unquote(size))-unquote(type)()
    end
  end

  types = [
    float64: {:float, 64},
    float32: {:float, 32},
    byte: {:integer, 8},
    int16: {:integer, 16},
    int32: {:integer, 32},
    int64: {:integer, 6}}
  ]

  for {guard, type_and_size} <- types do
    @guard guard
    @type_and_size type_and_size

    def concatenate(%Matrex{type: @guard}) do
      <<x,y,z::type_and_size()>>
    end
  end
end

This way tou will define one clause per type, where you guard on the type with the @guard attribute, and use the type_and_size() macro to inject the proper binary modifiers.

You should also consider not defining :float64 types (and similar). Instead you can simply pass the type/size around. So you have {:float, 64} instead of :float64. This allows two other implementations. The first one simply removes the @guard part and matches on @type_and_size:

  defmacrop type_and_size() do
    {type, size} = Module.get_attribute(__CALLER__.module, :type_and_size)
    quote do
      size(unquote(size))-unquote(type)()
    end
  end

  types = [
    {:float, 64},
    {:float, 32},
    {:integer, 8},
    {:integer, 16},
    {:integer, 32},
    {:integer, 6}}
  ]

  for type_and_size <- types do
    @type_and_size type_and_size

    def concatenate(%Matrex{type: @type_and_size}) do
      <<x,y,z::type_and_size()>>
    end
  end

However, we can also use this approach to define only two clauses, one for integer and another for floats instead of one clause per type x size pair. It would look something like this:

  defmacrop type_and_size(size) do
    type = Module.get_attribute(__CALLER__.module, :matrix_type)
    quote do
      size(unquote(size))-unquote(type)()
    end
  end

  for type <- [:float, :integer] do
    @matrix_type type

    def concatenate(%Matrex{type: {@matrix_type, size}}) do
      <<x,y,z::type_and_size(size)>>
    end
  end

This last approach will be easier on compilation time but it is likely slower in terms of runtime performance as you are giving the compiler less information when building/matching binaries. I would personally go with the earlier solutions unless you find yourself having to define many other types, such as int1, int4, etc.

versilov

versilov OP

Thanks, Jose!

For now I will stay with the first version, because using and atom :float64 will be more convenient for users, than using tuples. And it will also resemble them NumPy, if they are familiar.

Gradually most of these ‘typed’ functions will be converted to NIFs, I believe. So now I have to solve similar problem in the C language and then will be able to head towards first version of typed and shaped multi-dimensional matrices:)

OvermindDL1

OvermindDL1

Oh I read it as quoted def’s for some reason! ^.^;

Hmm, actually that is because it is (see the original post)… So doesn’t this hold true, that Enum.each wouldn’t do anything? So.. why would Enum.each work here when it is being quoted inside of it @josevalim ? o.O

josevalim

josevalim

Creator of Elixir

Wait, I thought Enum.each was inside the quote but it was outside, haha. Yes, it has to be a map then.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews