[erlang-questions] Maps

Björn-Egil Dahlberg wallentin.dahlberg@REDACTED
Tue May 14 20:21:32 CEST 2013


2013/5/14 Loïc Hoguin <essen@REDACTED>

> On 05/13/2013 08:27 PM, Björn-Egil Dahlberg wrote:
>
>> - Deep updates of Maps are not covered by this EEP. We would welcome
>> suggestions on this.
>>
>
> I think what Kenneth had in his talk was good. But
>
> Simple update:
>   M#{ K => V }
>
> Nested update:
>   M#{ K }#{ DK => DV }
>

I believe this was a hasty example since it would, i gather, return the
inner Map.

1> M = #{ a => #{ b => 1 } }.
#{ a => #{ b => 1 } }
2> M#{ a }#{ b := 2 }.
#{ b => 2 }




> I personally would prefer a more powerful:
>   M#{ K #{ DK => DV }}
>
> Because it allows you to write things like:
>   M#{ K #{ DK => DV }, K2 => V2 }
>
> And update multiple levels at once.
>
> I know K can be a map, but deep update only works on values, the same way
> normal update only works on values, so no confusion is possible, we're only
> updating values, not K itself. If you feel an operator is needed it can be
> introduced between K and # in the two examples above.
>
> Programmatically this would translate as extracting the map found at key
> K, and ensuring it's a map, updating this map with DK => DV, then placing
> this map back into the key K in map M along with setting V2 in key K2.
>
> It doesn't sound hard to implement in the compiler, it's just unrolling
> things for access and rolling back again for the actual update. Exactly
> what we do manually today.
>
> It should also be easy to compile to an optimized deep update with this
> syntax as you got everything in a single expression.
>
> Note: If you are not interested in it despite how simple it is please at
> least ensure it can be done with a parse transform.
>
> Thoughts?


I think we want to be explicit here. Meaning using an operator and "seeing"
the value in action. (I have bad memories of perl).

Just some first thoughts on deep updates here to see if I get the intention
right. What I think we need is someway to use an associated value within
the map. I figure, something like "escaping" the key which would fetch the
associated value. Let's use '~' in this example.

Say I have a nested map like above, M = #{ a => #{ b => 1 }} and I would
like to update the inner b value with it self plus four. I could write it
like this:

3> M#{ a := ~a#{ b := ~b + 4 }}.
#{ a => #{ b => 5 }}

Another possibility would perhaps be to bind values first (wrapped in { ..
} )and then use them in updates, say

M#{ a := M }{ a := M#{ b := V }{ b := V + 4 } }

.. that looks even more horrible .. =)

I haven't given this much thought but I'm trying to get a sense of it.
Something like this?

// Björn-Egil
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20130514/357201ae/attachment.htm>


More information about the erlang-questions mailing list