obrok / lens Goto Github PK
View Code? Open in Web Editor NEWA utility for working with nested data structures.
License: MIT License
A utility for working with nested data structures.
License: MIT License
just ran into an issue where i have a focus on something that may not exist, how should i handle this?
Hello,
Thank you for writing this library!
My apologies, any chance you might be able to give me a hint where my logic might be off with the following lens and data structure?
data = [%{id: 1}, %{id: 2, sub_items_1: [%{id: 4}, %{id: 5}], sub_items_2: [%{id: 6}, %{id: 7}]}, %{id: 3}]
lens = Lens.all
|> Lens.multiple([Lens.key?(:sub_items_1), Lens.key?(:sub_items_2), Lens.root])
|> Lens.all
|> Lens.key(:id)
|> Lens.to_list(data)
The Lens.multiple() step works if I only include the first two entries in the argument list, but once I include Lens.root, I get the "no function clause matching in Access.fetch/2" error.
Thank you very much.
Hello,
Thanks for this fantastic library which is very useful. I stumbled upon a weird behaviour of Lens.into
when used inside a map, not a the top level. IMHO I think test4_ko/1
should return a map instead of a list thanks to Lens.into
in the expression:
Lens.map_values |> Lens.all |> Lens.into(%{}) |> Lens.map(h, fn {k,v} -> {k,v} end)
The code is copied below.
iex(1)> h = LensTest.get_a_map
%{
"top" => %{
"257" => %{
"datetime" => "2020-03-03T14:58:30.200896+01:00",
"value" => false
}
}
}
iex(2)> LensTest.test1_ok(h)
[
{"top",
%{
"257" => %{
"datetime" => "2020-03-03T14:58:30.200896+01:00",
"value" => false
}
}}
]
iex(3)> LensTest.test2_ok(h)
%{
"top" => %{
"257" => %{
"datetime" => "2020-03-03T14:58:30.200896+01:00",
"value" => false
}
}
}
iex(4)> LensTest.test3_ok(h)
%{
"top" => [
{"257",
%{"datetime" => "2020-03-03T14:58:30.200896+01:00", "value" => false}}
]
}
iex(5)> LensTest.test4_ko(h)
%{
"top" => [
{"257",
%{"datetime" => "2020-03-03T14:58:30.200896+01:00", "value" => false}}
]
}
defmodule LensTest do
def get_a_map do
%{"top" => %{"257" => %{"datetime" => "2020-03-03T14:58:30.200896+01:00", "value" => false}}}
end
def test1_ok(h) do
Lens.all |> Lens.map(h, fn {k,v} -> {k,v} end)
end
def test2_ok(h) do
Lens.all |> Lens.into(%{}) |> Lens.map(h, fn {k,v} -> {k,v} end)
end
def test3_ok(h) do
Lens.map_values |> Lens.all |> Lens.map(h, fn {k,v} -> {k,v} end)
end
def test4_ko(h) do
Lens.map_values |> Lens.all |> Lens.into(%{}) |> Lens.map(h, fn {k,v} -> {k,v} end)
end
end
maybe lenses are not designed for this use-case, but i thought it would be really neat if i could focus on keys in a map, and do Lens.map over them.
very cool library/concept!
There is the Svbtle article that introduces to lens, and it contains this example:
import Integer
data = 1..10
lens = Lens.filter(&is_odd/1)
get_in(data, [lens])
# => [1, 3, 5, 7, 9]
update_in(data, [lens], &(-&1))
# => [-1, 2, -3, 4, -5, 6, -7, 8, -9, 10]
When I follow that in iex, line 3 using filter/1
fails with this error:
iex(31)> lens = Lens.filter(&is_odd/1)
** (UndefinedFunctionError) function Lens.filter/1 is undefined or private. Did you mean one of:
* filter/2
* filter/3
(lens) Lens.filter(#Function<6.99386804/1 in :erl_eval.expr/5>)
Which kind of makes sense, as the hex docs say it's expecting a lens as first argument. But then the article example contradicts that. What's going on? (Is that an API change?)
In the source code of lens.ex
, the definition of into
is missing a typespec. That is the cause of a dialyxir error.
deflens_raw into(lens, collectable) do
fn data, fun ->
{res, updated} = get_and_map(lens, data, fun)
{res, Enum.into(updated, collectable)}
end
end
Function call without opaqueness type mismatch.
Call does not have expected opaque term of type Lens.t() in the 2nd position.
Common.Utilities.lens_map(
any(),
_lens4 :: (:get | :get_and_update, _, (_ -> any()) -> any()),
...
It may be that I'm just being really dumb but here goes, in the case of both the following examples, typed in the console, the terminal just hung, I'm guessing they're off on a infinite recursion loop?
Lens.recur(Lens.key(:a)) |> Lens.to_list( %{a: %{b: :c}})
Lens.recur( Lens.root() |> Lens.key) |> Lens.map( %{a: %{b: :c}}, &IO.inspect/1)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.