obrok / lens

A utility for working with nested data structures.
MIT License
189 stars 11 forks source link

Lens.multiple Question #15

Closed dbi1 closed 3 years ago

dbi1 commented 3 years ago

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.

lkuty commented 3 years ago

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}
  ]
]
dbi1 commented 3 years ago

Thanks a lot @lkuty, great help :)

Do you have any pointers as to when to bring in Lens.all?

lkuty commented 3 years ago

No sorry. But maybe @obrok might help with some use cases.

dbi1 commented 3 years ago

Got it, thanks for the help again, @lkuty!

lkuty commented 3 years ago

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.

obrok commented 3 years ago

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]
dbi1 commented 3 years ago

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!

obrok commented 3 years ago

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.

dbi1 commented 3 years ago

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 :)

obrok commented 3 years ago

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.