DaAnalyst

DaAnalyst

Extructure - a flexible destructure library for Elixir

Hey guys (and gals),

Letting you know I finally release this Extructure library. Here’s some info copied from its docs:

By default the library is using loose (flexible) matching, allowing for implicit structural conversions (maps, lists and tuples, from one to another). Tuple and list key pair element order are also taken loosely by default.

Supports destructure-like implicit keys (with the same name as the variable) as well as optional values, flexible keyword and key pair tuple size and order of elements, implicit transformation between a map, list and a key pair tuple.

Also supports toggling between the loose mode and the standard Elixir pattern matching (“rigid”) mode where none of the flexibilities except for the optional variables are allowed.

Fully enforces pattern matching between the left and right side once taken into account the optional values and structural transformation.

For example, instead of:

%{
  first_name: fist_name,
  last_name: last_name,
} = socket.assigns

age = socket.assigns[ :age]

just write:

%{ first_name, last_name, _age} <~ socket.assigns

or implicitly transform the map into a list:

[ first_name, last_name, _age] <~ socket.assigns

or use a different order:

[ last_name, _age, first_name] <~ socket.assigns

or set a default value:

[ last_name, age( 25), first_name] <~ socket.assigns

https://github.com/DaTrader/extructure

Enjoy!

First Post!

hst337

hst337

Great library for destructing!

I have several questions.

  1. Why did you chose arguments like age(25) instead of more common age \\ 25 like defaults in arguments of functions? This kind of specification of defaults is less intuitive for Elixir developers and it makes impossible to use macros inside the left part of <~/2

  2. Why did you chose _defaults_to_nil syntax ? I think explicit defaults_to_nil \\ 25 or defaults_to_nil(nil) is just more obvious

Most Liked

DaAnalyst

DaAnalyst

New features available in v0.2.1 (as per the changelog):

Enhancements

  • Support transforming entire structures on the right side by specifying an empty map, tuple or list, e.g.:
[ a: a = %{}] <~ [ a: [ b: 2, c: 3]]
# a
# => %{b: 2, c: 3}
  • Support destructuring from module (named) structures as if plain maps, e.g:
[ hour, minute, second] <~ DateTime.utc_now()
# => [hour: 15, minute: 44, second: 14]
DaAnalyst

DaAnalyst

Extructure v1.0 is out

DaAnalyst

DaAnalyst

Thanks for the compliments.

As for your question, it’s not like I had that option. Namely, the following generates a syntax error:

%{ a, b \\ 25} <~ %{ a: 1, b: 2}

as it does not work for maps, and I wanted to have a uniform way of expressing the variables (the same syntax works with lists and tuples).

Last Post!

DaAnalyst

DaAnalyst

Finally managed to find some (Claude’s) time to complete the long planned and postponed feature set for the :extructure library. I’m aware this probably matters far less now than it did while we still used to actually code but I still prefer using :extructure even though it’s Claude writing most of the code now, because in my view it’s more compact and readable (e.g. for reviews) than the vanilla Elixir’s Map.get/fetch, Keyword.get/fetch, etc.

So, in addition to the existing loose (& rigid) destructuring based on the <~/2 operator, there is now a match based destructuring for the likes of function headers and case statements:

-%{ a} = %{ a: 1, b: 2}   # for atom keys
-@%{ a} = %{ "a" => 1}    # for string keys

The analog syntax with keyword lists and tuple keypairs is also supported.

Also added something I’ve long liked about JS (and been missing in Elixir) - the structuring shorthand:

a = 1
b = 2
+%{ a, b}   # %{ a: 1, b: 2}
+@%{ a, b}  # %{ "a" => 1, "b" => 2}

Same for keywords or listed string keypairs and tuple atom and string keypairs.

Here’s the whole thing again with the examples and all: extructure | Hex

Where Next?

Popular in Announcing Top

jakub-zawislak
Hi everyone, I’m coming from the Symfony (PHP) framework. I like Phoenix, but it has a one thing that was build much better in the Symfo...
New
handnot2
Samly can be used to enable SAML 2.0 Single Sign On in a Plug/Phoenix application. This library uses Erlang esaml to provide plug enabl...
New
oltarasenko
Dear Elixir community, After a year of development, bug fixes, and improvements, we are proudly ready to share the release of Crawly 0.1...
New
fuelen
Hey folks! Want to present a toolkit for writing command-line user interfaces. It provides a convenient interface for colorizing text...
New
benlime
LiveMotion enables high performance animations declared on the server and run on the client. As a follow up to my previous thread A libr...
New
kip
Image is an image processing library for Elixir. It is based upon the fabulous vix library that provides a libvips wrapper for Elixir. I...
622 19610 194
New
type1fool
WebAuthnLiveComponent WebAuthnComponents See this post about renaming the package. Passwordless authentication for Phoenix LiveView app...
New

Other popular topics Top

gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement