Adzz

Adzz

What’s it all about?

A keyword lens is a nested keyword-like structure used to describe paths into certain data types. It is similar to the list you can provide to Ecto’s Repo.preload/2

You can describe a KeywordLens like this:

[a: :b, c: [d: :e]]

Such a list is handy for describing subsets of nested data structures. For example, you can imagine the following KeywordLens: [a: :b] applied to this map: %{a: %{b: 1}} points to the value 1. In contrast this KeywordLens: [:a, :b] applied to this map %{a: 1, b: 2} points to both values 1 and 2.

It’s not a proper Keyword list because we allow any key for convenience, so these are valid:

[{"a", :b}]
[a: [{"b", [c: :d]}]
[{%{}, :b}]
[{1, {2, 3}}]

One KeywordLens can point to many different values inside a given data structure.
Here are some examples of different KeywordLenses and the unique set of lenses they represent.

keyword_lens = [a: :b]
lenses = [[:a], [:b]]

keyword_lens = [a: [b: [:c, :d]]]
lenses = [[:a, :b, :c], [:a, :b, :d]]

keyword_lens = [a: [:z, b: [:c, d: :e]]]
lenses = [[:a, :z], [:a, :b, :c], [:a, :b, :d, :e]]

keyword_lens = [:a, "b", :c]
lenses = [[:a], ["b"], [:c]]

You can use KeywordLens.Helpers.expand/1 to see which unique lenses are encoded in a given KeywordLens.

KeywordLens.Helpers.expand([a: :b])
[:a, :b]

KeywordLens.Helpers.expand([a: [b: [:c, :d]]])
[[:a, :b, :c], [:a, :b, :d]]

This library provides a protocol you can implement for your own data structures and structs. We provide a map implementation to get started.

Examples

KeywordLens.map(%{a: %{b: 1}}, [a: :b], &(&1 + 1))
%{a: %{b: 2}}

data = %{a: 1, b: 2}
reducer = fn {key, value}, acc ->
  {:cont, Map.merge(acc, %{key => value + 1})}
end
KeywordLens.reduce_while(data, [:a, :b], %{}, reducer)
%{a: 2, b: 3}

The implementation uses zipper like traversal so it is memory efficient.

Showing Posts 1 to 3

RudManusachi

RudManusachi

Thanks @Adzz, for the lib! I would suggest to add some small example closer to real world usage where it could be applied.
That comparison with get_in/update_in in the bottom of the README.md is helpful though.

Adzz

Adzz OP

Thanks that’s a great suggestion i shall do that.

I’ve been working on getting it working more fully with more of the enum functions for now and making it more fully featured. Updates to follow!

— All posts loaded —

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
MRdotB
I needed to reuse React components from my Chrome extension in my Phoenix/LiveView backend. I noticed that for Svelte/Vue, there are live...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
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 happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
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
AstonJ
This showed up on my feed.. anyone heard of it? Just hype? Ox Alpha is a reasoning model designed for coding, sustained ag...
New
sergio
It’s not that it’s vocabulary is too advanced. It’s something worse. I get lost trying to follow even a paragraph written by Claude. It’...
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
akoutmos
@hugobarauna, Dr. Dimitrios Koutmos (my brother) and I (Alex Koutmos) have been hard at work on writing a book on how you can use Elixir ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews