Map Iteration
iandol opened this issue · comments
In Ruby at least, maps can be iterated over using each {|key, value| ..}
— this is very easy and intuitive. I thought if one value was specified it would be the key, and two would be key and value, the magic happening in each
:
put &a=1 &b=2 | each {|k| echo $k }
put &a=1 &b=2 | each {|k v| echo $k"="$v }
But there are more types of syntax for iteration. I was searching on previous proposals for iteration of maps and found #1243 which is supposed to be about strings but thanks to @krader1961 expanded to discuss iterating maps. @hanche thought of more options to iterate over maps:
@krader1961 You're doing great at being devil's advocate! At the opposite end of the available options, I've been thinking a bit about maps and how to iterate them. I was wondering above whether iterating a map should yield just the keys, or key-value pairs. So I had an idea: Why not both? So here we go, then:
Assuming
$map
holds a map, we could let$@map
expand to the keys, and (wait for it)$@@map
expand to key-value pairs.
What about
all
? Well, since we already havekeys
to extract the keys, we could letall $map
produce key-value maps. I know, that makes for a bit of inconsistency, since ideally,(all $map)
should be equivalent to$@map
. But, I am brainstorming here. Maybe someone can think of a better way.
And finally, we could extend the
for
syntax, allowing bothfor key $map { … }
andfor key value $map { … }
. The latter, being syntactically different, can't be confused with the single-variable version offor
.
A totally different option would be to let iteration of a map always return key-value pairs, and then using destructuring, we could write the above examples as
for [key _] $map { … }
andfor [key value] $map { … }
. This allows for greater consistency, perhaps. Then$@map
would return key-value pairs, as wouldall $map
. Come to think of it, then we could get the keys by$@map[0]
.
Originally posted by @hanche in #1243 (comment)