Comments (10)
You might want to try:
lens = Lens.multiple([Lens.all |> Lens.key?(:sub_items_1), Lens.all |> Lens.key?(:sub_items_2), Lens.root])
|> Lens.all
|> Lens.key(:id)
|> Lens.to_list(data)
which gives [4, 5, 6, 7, 1, 2, 3]
.
Note that:
iex(13)> lens = Lens.root |> Lens.to_list(data)
[
[
%{id: 1},
%{
id: 2,
sub_items_1: [%{id: 4}, %{id: 5}],
sub_items_2: [%{id: 6}, %{id: 7}]
},
%{id: 3}
]
]
from lens.
Thanks a lot @lkuty, great help :)
Do you have any pointers as to when to bring in Lens.all?
from lens.
No sorry. But maybe @obrok might help with some use cases.
from lens.
Got it, thanks for the help again, @lkuty!
from lens.
You're welcome. The library is indeed super useful. In fact I cannot imagine writing code without it when it comes to handling nested data structures.
from lens.
Lens.all
is just a way to access all items in an Enum
. In many cases things like Lens.keys
or Lens.values
will be more useful, but if you have a list or set, then Lens.all
is your guy. You can also treat a map as an enumerable of pairs and use Lens.all
to access these pairs. Examples:
# The first `all` accesses the two nested lists, the second one each element on each list
get_in([[1,2,3], [4,5,6]], [Lens.all |> Lens.all])
# => [1, 2, 3, 4, 5, 6]
# The first all accesses each map, the second treats them as enums of pairs and accesses each pair:
get_in([%{c: :d}, %{a: :b}], [Lens.all |> Lens.all])
# => [c: :d, a: :b]
from lens.
Thank you very much, @obrok, this helps a lot :)
Might I ask you guys for one more pointer?
If, instead of getting the ids of the elements in @lkuty's answer, I wanted to add a key/value pair (e.g. key5: "abc"
) to each of the maps that have one of the ids from @lkuty's answer, how might I go about this?
The goal would be to get the original data structure back, but with key5: "abc"
in all maps that have an id...
Been reading the Lens docs up and down and trying various options, but haven't figured it out yet.
Thank you again!
from lens.
Given:
data = [
%{id: 1},
%{id: 2, sub_items_1: [%{id: 4}, %{id: 5}], sub_items_2: [%{id: 6}, %{id: 7}]},
%{id: 3}
]
When you do:
put_in(
data,
[
Lens.all()
|> Lens.both(
Lens.root(),
Lens.keys?([:sub_items_1, :sub_items_2]) |> Lens.all()
)
|> Lens.key(:key5)
],
:value
)
That produces:
[
%{id: 1, key5: :value},
%{
id: 2,
key5: :value,
sub_items_1: [%{id: 4, key5: :value}, %{id: 5, key5: :value}],
sub_items_2: [%{id: 6, key5: :value}, %{id: 7, key5: :value}]
},
%{id: 3, key5: :value}
]
Honestly, the best advise I can give is to build up the lens gradually, using get_in
. So I start with something like get_in(data, [Lens.all()])
, then get_in(data, [Lens.all() |> Lens.key?(:sub_items_1)])
, then get_in(data, [Lens.all() |> Lens.keys?([:sub_items_1, :sub_items_2])])
, etc. in an iex
session. I agree that it can be a bit mindbending how they fit together, so decomposing the problem is the key.
from lens.
Wow, awesome, that's exactly what I was trying to accomplish!
The advice to build the lens up gradually (and to decompose the problem) is very, very helpful...
Thank you for all the great and kind help :)
from lens.
Maybe one final point is that it's good to define named lenses - the deflens
and deflensp
macros in Lens.Macros
are supposed to help with that, adding some syntax sugar when you pack your lenses into functions. When your lenses are named it's easier to grok what a complex lens is doing, like all_widgets() |> sub_widgets() |> all_properties()
or something.
Since you don't have any more questions, I'm closing this issue, feel free to reopen or open another one in case of any problems.
from lens.
Related Issues (7)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from lens.