Qqwy

Qqwy

TypeCheck Core Team

During writing some code related to Arrays , I encountered a situation in which I wanted to map a function over a tuple.

Now, code that does this is very easy to write if you know exactly what size of tuple you have.
If you don’t… it becomes a little tedious, as Erlang supports tuples with up to 255 elements (EDIT: Turns out that nowadays it supports tuples with up to 2^24 number of elements… :open_mouth: ).
But in Elixir, we can have the code write itself, with some simple metaprogramming:

defmodule TupleMap do
  @max_tuple_size 255

  def map_tuple({}, _fun), do: {}
  def map_tuple({val}, fun), do: {fun.(val)}
  def map_tuple({val1, val2}, fun), do: {fun.(val1), fun.(val2)}

  # ... etc.
  # But writing this by hand is tiresome. So instead:

  for index <- 3..@max_tuple_size do
    vars = Enum.map(1..index//1, fn index -> Macro.var(:"val#{index}", nil) end)
    vars_tuple = {:{}, [], vars}

    result_vars =
      Enum.map(vars, fn var ->
        quote do
          unquote(Macro.var(:fun, nil)).(unquote(var))
        end
      end)

    result_tuple = {:{}, [], result_vars}

    def map_tuple(unquote(vars_tuple), fun) do
      unquote(result_tuple)
    end
  end

  # For comparison:
  def map_tuple_through_list(tuple, fun) do
    list =  Tuple.to_list(tuple)
    result_list =  :lists.map(fun, list)
    List.to_tuple(result_list)
  end
end

I ran a small benchmark on this code, for tuples with up to 20 elements (it would definitely be interesting to run a longer benchmark later).
The results are interesting: It seems that mapping over a tuple is ~30%-50% faster than mapping over a list.
The memory usage (not shown in this picture) is also only 1/4th of that of a list.

This benchmark is of course not very scientific, but I still thought it was interesting and something that might be nice to share.

I don’t know how frequent it is to map a function over all elements in a tuple, but if someone wants above code could easily be wrapped up in a library. Or even better: changed into a macro which includes a defp-implementation in a module that needs it, which will then guarantee that the tuple-call is inlined.

EDIT: I couldn’t help myself and have run a larger benchmark: :sweat_smile:

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
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
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
axelson
Hi there! :wave: @frigidcode and I (but mostly him) have been running an Elixir Book club, we’re almost done with Designing Elixir Syste...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
georgeguimaraes
Just published claude-code-elixir, a plugin marketplace for Claude Code with Elixir support. These are the plugins I’ve been using for my...
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews