You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Transforming a map into a different one now requires mutating a map repeatedly. For example, to change all keys in a map to upper case:
use str
m = [&foo=1 &bar=2 &lorem=3]
new-m = [&]fork[(keys$m)]{
new-m[(str:to-upper $k)] = $m[$k]}
If we have a function, say make-map, that takes pairs and puts a map (inputs can be supplied either via pipe or a list argument), the code becomes much simpler:
use str
m = [&foo=1 &bar=2 &lorem=3]
for k [(keys $m)] { put [(str:to-upper $k) $m[$k]] } | new-m = (make-map)
The text was updated successfully, but these errors were encountered:
For those wondering why this feature might be useful see the discussion about Elvish persistent data structures. In functional programming languages, such as Haskell, such data structures are central to the design of the language. They therefore use very sophisticated solutions to the problem of cheaply mutating otherwise immutable lists or maps. Elvish currently uses the simplest, and typically least performant, solution for that problem: copy on write. See https://en.wikipedia.org/wiki/Persistent_data_structure.
Note that the
new-m[(str:to-upper $k)] = $m[$k]
in the first example in the problem description actually creates a new map and binds it to the new-m variable name. It does not mutate the existing map bound to new-m. Which, given the copy on write semantics, is why that pattern is inefficient and something like the proposed make-map command is useful.
Transforming a map into a different one now requires mutating a map repeatedly. For example, to change all keys in a map to upper case:
If we have a function, say
make-map
, that takes pairs and puts a map (inputs can be supplied either via pipe or a list argument), the code becomes much simpler:The text was updated successfully, but these errors were encountered: