IvanR

IvanR

A library to work with tagged tuples.
Provides functions to work with chains of tags for nested tuples.

Some of the supported operations are: add/remove tag chain to a core value, converting a tuple to list or to map and back. The latter one is useful for converting tuples to/from JSON representation.

iex> use TaggedTuple
...> core_value = 12
...> :tag --- core_value
{:tag, 12}
...> tagged_tuple = :a --- :tag --- :chain --- core_value
{:a, {:tag, {:chain, 12}}}
...> match?(:a --- :tag --- _tail, tagged_tuple)
true

iex> {chain, value} = TaggedTuple.split({:a, {:tag, {:chain, 2}}})
...> chain == {:a, {:tag, :chain}}
...> value == 2
iex> TaggedTuple.tag(2, {:a, {:tag, :chain}})
{:a, {:tag, {:chain, 2}}}

iex> TaggedTuple.to_map({:a, {:tag, {:chain, 2}}})
%{a: %{tag: %{chain: 2}}}
iex> TaggedTuple.from_map(%{a: %{tag: %{chain: 2}}})
{:a, {:tag, {:chain, 2}}}

More information here:

First 7 of 7 Posts Switch mode

dimitarvp

dimitarvp

Super great work, man. I like the tagged_tuple library a lot, already used it in a small hobby project as a test!

One thing that might be worth mentioning: would you consider making the custom syntax (the 3 dashes: ---) optional and injectable through use or a configuration setting? Not critical but I’d like to be able to not inject a custom Elixir syntax in my projects.

hauleth

hauleth

Well, you can just do not use use TaggedTuple and instead just require TaggedTuple.

IvanR

IvanR OP

I can imagine the situation that the --- operator is to be used somehow else in the project. Let’s see what can be done in regards to optional overriding.

thojanssens1

thojanssens1

Is it possible to see a snippet demonstrating where TaggedTuple can typically be useful?

BartOtten

BartOtten

I’m not sure about the use case over Records (besides nice helper functions for conversion). To me it seems ‘Tagged Tuple vs Record’ is like ‘Map vs Struct’. Dynamic vs static.

IvanR

IvanR OP

Sure. tagged_tuple is to operate with sum types. Which are one of the ways to making impossible states impossible in the app. Here is a good article about these https://thoughtbot.com/blog/better-domain-modeling-in-elixir-with-sum-types

Here is an example of ShipmentWeight sum type that can be persisted with Ecto.
And this is generic Domo/example_avialia/lib/example_avialia/tagged_tuple_ecto_type.ex at master · IvanRublev/Domo · GitHub to do so for any tagged tuple.

thojanssens1

thojanssens1

Very interesting article! Thank you

— All posts loaded —

Where Next? Top

Trending in Announcing Top

bluzky
You may know https://ui.shadcn.com/, a UI component library for React. I really love it’s design style and components. I’ve built some co...
387 15136 120
New
woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
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
shahryarjb
The Chelekom project is a library of Phoenix and LiveView components generated via Mix tasks to fit developer needs seamlessly. One of i...
New
woylie
Phoenix components for pagination, sortable tables and filter forms with Flop and (optionally) Ecto. pagination cursor pagination sorta...
New
kip
Please say hi to a new lib, Astro that aims to deliver easy-to-consume astronomy calculations of practical use. For now it only calculat...
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

Other Trending Topics Top

juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New
bartblast
Hey folks, I just published a post about Hologram’s funding and where the project goes next - the short version: Curiosum as Main Spons...
New

We're in Beta

About us Mission Statement