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!
Trending in Announcing
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
hst337
Great library for destructing!
I have several questions.
Why did you chose arguments like
age(25)instead of more commonage \\ 25like 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<~/2Why did you chose
_defaults_to_nilsyntax ? I think explicitdefaults_to_nil \\ 25ordefaults_to_nil(nil)is just more obviousDaAnalyst
Thanks for the compliments.
As for your question, it’s not like I had that option. Namely, the following generates a syntax error:
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
Hmm, yeah, for maps the only valid syntax is
left => right,left: rightandvariable,call(args). I’ve experienced the similar issue here: Using map syntax %{} as list brackets for integers · Issue #10964 · elixir-lang/elixir · GitHubDaAnalyst
Just released Extructure v0.1.1 with added support for loose list head | tail extraction.
Ex (from docs):
DaAnalyst
New features available in v0.2.1 (as per the changelog):
Enhancements
DaAnalyst
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.:
DaAnalyst
Extructure v1.0 is out
dmitriid
How and why didn’t I see this project before?!
Will use the hell out of it. Thank you!
DaAnalyst
You’re welcome.
pejrich
Is there anyway to do use this in function heads? I find function heads like:
def func(%{"variable1" => variable1, "variable2" => variable2}) docan 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})) doSince your library uses slightly different syntax
%{a, b} <~vsd(%{a, b}), I was wondering if it would be possible to support function head destructuring or not.