ProductPromotion
Logo

Elixir

made by https://0x3d.site

GitHub - hissssst/pathex: Fastest tool to access data in Elixir
Fastest tool to access data in Elixir. Contribute to hissssst/pathex development by creating an account on GitHub.
Visit Site

GitHub - hissssst/pathex: Fastest tool to access data in Elixir

GitHub - hissssst/pathex: Fastest tool to access data in Elixir

Pathex

Speed or composability? Choose both!

Elixir 1.17 warning

It turns out that Elixir 1.17 release has 3 bugs which block reliable usage of Pathex library, one of which is critical. So please, do not use Elixir 1.17 with the Pathex library. However, these issues are fixed in upcoming Elixir 1.18 release.

What is Pathex?

Pathex is a library for performing fast actions with nested data structures in Elixir. With pathex you can trivially set, get and update values in structures in a functional manner. It provides all necessary logic to manipulate data structures in different ways using flexible functional lens pattern.

Why another library?

Existing methods of accesssing data in nested structures are either slow (like Focus) or do not provide enough functionality (like Access). For example setting the value in structure with Pathex is 70-160x faster than Focus or 2-3x faster than put_in and get_in

You can checkout benchmarks at https://github.com/hissssst/pathex_bench

Usage

Pathex is really simple and straightforward to use (almost like Enum). You don't need to learn any specific language, just create paths with path and use verbs with them.

Add it to your module

# This will import path macro and operators and require Pathex
use Pathex

Or just import Pathex

Create the path

path_to_streets = path :user / :private / :addresses / 0 / :street
path_in_json = path "users" / 1 / "street", :json

This creates closure which can get, set, update and delete values in this path

Use the path

{:ok, "6th avenue" = street} =
  %{
    user: %{
      id: 1,
      name: "hissssst",
      private: %{
        phone: "123-456-789",
        addresses: [
           [city: "City", street: "6th avenue", mail_index: 123456]
        ]
      }
    }
  }
  |> Pathex.view(path_to_streets)

%{
  "users" => %{
    1 => %{"street" => "6th avenue"}
  }
} = Pathex.force_set!(%{}, path_in_json, street)

Features

Pathex has a lot of different features and can even compete with code written by hand in terms of efficiency. Pathex significantly reduces the time to write a code which manipulates nested structure, while providing efficiency and composability. No more functions like get_users, set_users, update_users! No more XPaths, JSONPaths, CSS Selectors!

Easy to use

It's not harder to use than Map or Enum! Check out the cheatsheet for common tasks.

Pathex also provides more information about errors than any other tool.

iex(1)> field = :email
iex(2)> Pathex.view!(%{}, path(:users) ~> all() ~> path(:personal / field))
** (Pathex.Error)
  Couldn't find element

    Path:      path(:users) ~> all() ~> path(:personal / :email)

    Structure: %{}

Fast

Paths are just a set of pattern-matching cases. This is done to extract maximum efficiency from BEAM's pattern-matching compiler.

# Code for viewing variables for path
path(1 / "y", :map)

# Almost equals to
case input do
  %{1 => %{"y" => res}} ->
    {:ok, res}

  _ ->
    :error
end

Reusable

One path can be used to update, get, set, delete or update a value in the structure! And these paths can even be composed together. This composition is very efficient, there's no need to concatenate lists like Access does.

# User structure
user = %User{
  personal: %{fname: "Kabs", sname: "Rocks"},
  phone: "123-456-789"
}

# Path to username in user structure
username = path(:personal / :fname)

# Get a username
{:ok, "Kabs"} = Pathex.view(user, username)

# Set a username
another_user =
  %User{
    personal: %{fname: "Blabs", sname: "Rocks"},
    phone: "123-456-789"
  } = Pathex.set!(user, username, "Blabs")

# Get all usernames!
import Pathex.Lenses
["Kabs", "Blabs"] =
  [
    user,
    another_user
  ]
  |> Pathex.view!(all() ~> username)

Pathex can be used to manipulate different nested data structures. From GenServer state to HTML or Elixir's AST!

Extensible

Pathex is built around simple primitive called path-closure, which is a simple closure with clearly defined specification. Anything complying with Pathex.t() spec can be used within Pathex.

Installation

def deps do
  [
    {:pathex, "~> 2.0"}
  ]
end

Pathex is significantly faster with OTP 26

Contributions

Welcome! If you want to get your hands dirty, you can check existing TODO's.

By the way

If you have any suggestions or want to change something in this library don't hesitate to open an issue. If you have any whitepapers about functional lenses, you can add them in a PR to the bottom of this readme

Articles
to learn more about the elixir concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here 🔥.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here 🔥.

Queries
or most google FAQ's about Elixir.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory