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:
- Hide looping through types in the macro
- Use one-term binary typespec:
::typespecinstead 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?
Trending in Questions
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
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
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
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
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
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
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
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
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
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
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
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 8- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
OvermindDL1
This isn’t going to return anything, perhaps you means
Enum.map?versilov
Perhaps you are right.
But it does not help to make it work.
In this variant (no matter
.eachor.map) it does defines functions, but does not passtype,spec&bitsvalues to the definitions, so, we cannot do pattern matching ontypeparameter and use correct binary spec.josevalim
Defining functions is a side-effect, so
eachis fine.NobbZ
defisn’t called, but unquoted only, so its sideeffect won’t fire. So to my understanding we need to usemaphere to get thedefinto 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
eachinside the quoting, therefore my comment is irrelevantjosevalim
My suggestion would be to avoid the macro for definitions altogether in favor of a simple
type_and_sizemacro and then organize your codebase like this:This way tou will define one clause per type, where you guard on the type with the
@guardattribute, and use thetype_and_size()macro to inject the proper binary modifiers.You should also consider not defining
:float64types (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@guardpart and matches on@type_and_size: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 sizepair. It would look something like this: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
Thanks, Jose!
For now I will stay with the first version, because using and atom
:float64will 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
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.eachwouldn’t do anything? So.. why wouldEnum.eachwork here when it is being quoted inside of it @josevalim ? o.Ojosevalim
Wait, I thought Enum.each was inside the quote but it was outside, haha. Yes, it has to be a map then.