DaAnalyst

DaAnalyst

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!

Showing Posts 1 to 10

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

DaAnalyst

DaAnalyst OP

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

DaAnalyst

DaAnalyst OP

Just released Extructure v0.1.1 with added support for loose list head | tail extraction.

Ex (from docs):

[ b | rest] <~ [ a: 1, b: 2, c: 3]
# => [ b: 2, a: 1, c: 3]

[ b | [ a, c]] <~ [ a: 1, b: 2, c: 3, d: 4]
# => [ b: 2, a: 1, c: 3]

[ a | [ b, c( 25)]] <~ %{ a: 1, b: 2}
# => [a: 1, b: 2, c: 25]

[ b | %{ c: %{ d}}] <~ [ a: 1, b: 2, c: %{ d: 5}]
# => [ { :b, 2} | %{ a: 1, c: %{ d: 5}}
DaAnalyst

DaAnalyst OP

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 OP

The new v0.3.1 now supports string keys (in addition to atoms) to facilitate destructuring in such use cases as JSON properties and LiveView params, e.g.:

params = %{
  "a" => 1,
  "b" => 2
}
...

@[ a, b, _c] <~ params
# a => 1
# b => 2
# c => nil
DaAnalyst

DaAnalyst OP

Extructure v1.0 is out

dmitriid

dmitriid

How and why didn’t I see this project before?!

Will use the hell out of it. Thank you!

DaAnalyst

DaAnalyst OP

You’re welcome.

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? Top

Trending in Announcing Top

wojtekmach
Hey everyone! Req is an HTTP client for Elixir that I’ve been working on for quite some time. There is already a lot of HTTP clients out...
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
woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
restlessronin
The repo is at GitHub - cyberchitta/openai_ex: Community maintained Elixir library for OpenAI API · GitHub. Docs are at OpenaiEx User Gu...
152 11030 135
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
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
fuelen
Hi all! I want to present a small library which provides a mix task for generating an Entity-Relationship Diagram for Ecto schemas. You...
New

Other Trending Topics Top

mudasobwa
I am seeing a lot of aplications of Argumentum ad Vericundiam in software discussions. They do link some piece of writing and point us to...
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
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
sorenone
Today we’re releasing Oban for Python. Not an Oban client in Python. Not a pythonx wrapper embedded in Elixir. Nope, it’s a fully operati...
New
Herve37
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using. We’re particularly inte...
New
matt-savvy
Is there a word for the ~> symbol used in Version strings? Do you also just call it a Squiggle Arrow™ ?!
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews