smpoulsen / focus Goto Github PK
View Code? Open in Web Editor NEWLightweight Elixir lenses
License: BSD 2-Clause "Simplified" License
Lightweight Elixir lenses
License: BSD 2-Clause "Simplified" License
la = Lens.make_lens("a")
lb = Lens.make_lens("b")
i0 = Lens.idx(0)
This works
la ~> i0 ~> lb |> Lens.safe_view(%{"a" => [%{"b" => 3}]})
This crashes
la ~> i0 ~> lb |> Lens.safe_view(%{"a" => []})
** (FunctionClauseError) no function clause matching in Keyword.get/3
(elixir) lib/keyword.ex:150: Keyword.get([], 0, nil)
lib/focus.ex:147: anonymous fn/3 in Focus.compose/2
Shouldn't "safe_view" be safe and not crash when something is missing?
Hi !
just noticed that the second link in the References section (Lenses in Functional Programming) is broken
Is there any convention or best practice for using lens vs pattern-matching?
Hey, thanks for focus, this is quite superb way to deal with large payloads!
I have one question and I could not answer it after looking through Readme / codebase / tests.
Given the following datastructure:
payload = %{
items: [
%{id: 1, price: 12.99},
%{id: 2, price: 34.60},
%{id: 3, price: 99.28},
]
}
how can one
Does focus support targeting all items in a list in a generic way, means without explicit index option?
Thanks and have a nice day!
Roman
The original intention with making the lens the first argument was to support composing a long chain of lenses and piping into Focus.view/2
, Focus.set/3
, Focus.over/3
etc….
There is obvious value in making the data structure the first argument, as focus could then slot directly into pipeline operations.
iex(1)> Lens.make_lens(:id) |> Focus.set(%{id: nil}, 666)
{:error, {:lens, :bad_path}}
iex(2)> Lens.make_lens(:id) |> Focus.set(%{id: "foo"}, 666)
%{id: 666}
I've narrowed it down to this line. Is this intentional?
import Focus
data = %{a: []}
a = Lens.make_lens(:a)
b = Lens.make_lens(:b)
c = Lens.make_lens(:c)
d = Lens.make_lens(:d)
i0 = Lens.idx(0)
This works fine:
a ~> b ~> c ~> d |> Lens.safe_view(data)
{:error, {:lens, :bad_path}}
This crashes
a ~> i0 ~> b ~> c |> Lens.safe_view(data)
** (ArithmeticError) bad argument in arithmetic expression
Lensable.Tuple.getter/2
lib/lens/lens.ex:227: Lens.safe_view/2
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.