hst337
Pathex - a library for performing fast actions with nested data structures
Hi there,
I’ve been working on a Pathex library for quite a while now, and I’ve finally managed to create a release which is able to cover all requirements in nested structures access. Think of Pathex as Elixir’s Access but on steroids, or like Clojure’s Spectre but easier to learn and more efficient.
Features
-
Efficient in time. It’s 2-5 times faster than
Access.HTMLmanipulations withPathexare 5 times faster thanFloki. To achieve this efficiency,Pathexgenerates pattern-matching cases at compile-time. -
Easy to use.
Pathexis like aMaporEnummodule, but for any structures. At it’s tiny core, it has everything you might need in your development needs. Errors are very descriptive. And I’ve put some effort into the documentation to be easy to navigate -
Functional.
Pathexis built around functional lens idea, but it differs in some ways from it. -
Rich toolkit.
Pathexworks fine with lists, tuples, maps, structures, nested structures like AST or HTML.Pathexis reusable, because composition of two paths creates another path and so on.
Feedback
Feedback is really appreciated. I’d like to know what you’re thinking about library design because this library tries to extend the language!
Most Liked
josevalim
Great project and documentation!
krstfk
This project is really great, and it’s gonna be a dependency for all of my future projects. I’m only worried that I might end up over using it. I kind of wish there was something like this in the standard library since nested data structures are so pervasive. And I really like the fact that you can throw maps, keyword list, and tuples, nested any which way without worrying about it.
In other words, thank you!
hst337
The difference between matching and filtering is that
matching accepts pattern as a predicate. For example
iex> admin_lens = matching(%{role: :admin})
iex> Pathex.view(%User{name: "emoragaf", role: :admin}, admin_lens ~> path(:name))
{:ok, "emoragaf"}
iex> Pathex.view(%User{name: "hissssst", role: nil}, admin_lens ~> path(:name))
:error
will work only with values, which match the pattern %{role: :admin}.
And filtering lens supports any predicate as a function passed into it. For example,
iex> admin_lens = filtering(fn user -> user.role == :admin end)
iex> Pathex.view(%User{name: "emoragaf", role: :admin}, admin_lens ~> path(:name))
{:ok, "emoragaf"}
iex> Pathex.view(%User{name: "hissssst", role: nil}, admin_lens ~> path(:name))
:error
This lens does essentially the same as a matching lens above.
As you can see, every lens created with matching can be expressed with filtering, but matchingis easier to write, read and a is little bit faster than filtering
hst337
Hi, I’ve just released 2.2.0, you can now use this function:
https://hexdocs.pm/pathex/Pathex.Accessibility.html#from_list/2
hst337
Pathex 2.3 release is here 
Nothing special, just
Pathex.Shortfor shorter path definition. One can use just:x / : yinstead ofpath :x / :ywhen usingPathex.ShortPathex.patternfor creating patterns from inlined paths. For example,
use Pathex, default_mod: :json
import Pathex
pattern(four, path :x / :y / :z / 3) = %{x: %{y: %{z: [1, 2, 3, 4]}}}
Pathexnow can beuse-d inside functions or anything like this. This is now tested and supported.- Paths inlining is now detected for aliased, imported and macro calls. This is done using
Macro.Env.lookup*functions, therefore pathex is compatible only with elixir 1.13 or higher - Spec fixes here and there, project formatting.
By the way, I’ve tested this release against gradient (from gradualizer project) and it seemed to be able to find some errors in specs, while failing to complete checking with exception and a bunch of false positives. And I’ve used mix_unused tool to find some unused functions (which helped me a lot)







