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

Enjoy!

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).

hst337

hst337

Hmm, yeah, for maps the only valid syntax is left => right, left: right and variable, call(args). I’ve experienced the similar issue here: Using map syntax %{} as list brackets for integers · Issue #10964 · elixir-lang/elixir · GitHub

pejrich

pejrich

Is there anyway to do use this in function heads? I find function heads like:

def func(%{"variable1" => variable1, "variable2" => variable2}) do

can get quite verbose, of course you can extract values in the first line of the function body, but sometimes having it in the function head serves additional benefits. I’ve used the Destructure library before which allows you to do the following:

def func(s(%{variable1, variable2})) do

Since your library uses slightly different syntax %{a, b} <~ vs d(%{a, b}), I was wondering if it would be possible to support function head destructuring or not.

Where Next?

Popular in Libraries Top

Qqwy
Solution is a library to help you with working with ok/error-tuples in case and with-expressions by exposing special matching macros, as ...
New
scohen
Lexical Lexical is a next-generation language server for the Elixir programming language. Features Context aware code completion As-you...
New
Qqwy
TypeCheck: Fast and flexible runtime type-checking for your Elixir projects. Core ideas Type- and function specifications are const...
336 13801 100
New
marcuslankenau
I feel kind of stuck with the absence of a proper xml library for Elixir. Currently I use SweetXML which was ok for me more or less to pa...
New
pkrawat1
Hey guyz We at @aviabird are working on a payment library in elixir/phoenix. We are targeting March 2018 to add 56 Gateways to it. Have...
New
mathieuprog
Hello :wave: Allow me to introduce you to Tz, an alternative time zone database support to Tzdata. Why another library? First and fore...
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
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
OvermindDL1
I created a new library (rather I pulled out a couple files from my big project), it manages an operating system PID file for the BEAM. ...
New

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
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
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
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
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

Sub Categories:

We're in Beta

About us Mission Statement