fceruti

fceruti

Happy Macro Monday to everyone!

I was wonder what kinds of crafty macros you think are awesome and have come to rely upon.

I’ll go first:

Sigil m


  defmacro sigil_m({:<<>>, _line, [string]}, []) do
    spec =
      string
      |> String.split()
      |> Stream.map(&String.to_atom/1)
      |> Enum.map(&{&1, {&1, [], nil}})

    {:%{}, [], spec}
  end

This one I copy-pasted from the web (can’t remember the origin) and it’s truly a killer in readability and speed of development.

I use it in many places, but on tests & liveview really shines:

Tests

test "happy path", ~%{user: user, project: project, task_1: task_1, task_2: task_2} do
test "happy path", ~m{user project task_1 task_2} do

Liveview

%{current_user: current_user,  project: project} = socket.assigns
~m{current_user project} = socket.assigns

Maybe Case

This one is “my own creation” (quotes apply as I received tons of help).

Think tap/1 meets case.

  defmacro maybe_case(prev, do: block) do
    quote do
      prev = unquote(prev)

      case prev do
        unquote(block ++ [{:->, [], [[{:_, [], nil}], nil]}])
      end

      prev
    end
  end

The use I have for this is the following. In my context layer I have all this functions such as schedule_task, unschedule_task etc, which in principle all look something like this:

Multi.new()
|> Multi.run(...)
|> Multi.run(...)
|> Multi.run(...)
|> Repo.transaction()
|> case do
   {:ok, %{action: ...}} -> {:ok, ...}
   {:ok, %{action_2: ...}} -> {:ok, ...}
   {:error, %{action: ...}} -> {:error, ...}
end

That’s great until you want to introduce side effects like logging and pubsubing. I could of course capture the result of the transaction, make conditional statements for publish & log and then shape the response.

results = Multi.new()
  |> Multi.run(...)
  |> Multi.run(...)
  |> Multi.run(...)
  |> Repo.transaction()

case results do
  {:ok, _, _} -> PubSub.publish_change()
  {:error, _, _} -> Logger.error()
end

case results do
   {:ok, %{action: ...}} -> {:ok, ...}
   {:ok, %{action_2: ...}} -> {:ok, ...}
   {:error, %{action: ...}} -> {:error, ...}
end

But being a pipe-man myself, I decided I needed my code to look like this:

Multi.new()
|> Multi.run(...)
|> Multi.run(...)
|> Multi.run(...)
|> Repo.transaction()
|> maybe_case do
  {:ok, _} -> PubSubs.publish_change(...)
  {:error, _} -> Logger.error(...)
end
|> case do
   {:ok, %{action: ...}} -> {:ok, ...}
   {:ok, %{action_2: ...}} -> {:ok, ...}
   {:error, %{action: ...}} -> {:error, ...}
end

Now it’s your turn!

Showing Posts 1 to 1

Nicd

Nicd

I don’t like macros. They’re obscure and inscrutable and irritating, and they get everywhere.

That said, I have two macros that I use to add type information to my code, my typed struct and typed Ecto schema macros. The latter is ugly and incomplete, but the former I can link for you: lib/geo_therminator/typed_struct.ex · 8fa72cbfa5b0402c46c53e13e5e994bf5a390f89 · Mikko Ahlroth / GeoTherminator · GitLab

I use it like this then:

defmodule Register do
  deftypedstruct(%{
    register_name: String.t(),
    register_value: {integer(), 0},
    timestamp: DateTime.t()
  })
end

It defines the struct, a @type t, and @enforce_keys for me all in one go. I copy it to every new project I start.

— All posts loaded —

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 94592 917
New
cblavier
Hey there, It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
New
caslu
I want to open this thread for you all to discuss and help those who really like Ash but are still hesitant to use it in a real project. ...
New
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
heathen
Quite interesting article Google brought me. Didn’t find any mentions about it here. What do you think in general? Would you use togethe...
New
marciol
It would be helpful to have a list of companies worldwide that hire engineers without prior experience in Elixir. Often, it can be quite ...
New
durvia
Anyone running long-lived stateful processes on BEAM? We’re building an AI agent runtime and would love to compare notes. We’re a small ...
New

Other Trending Topics Top

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
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
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; 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
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