Define specification for map
#57075
Labels
collections
Data structures holding multiple items, e.g. sets
design
Design of APIs or of the language itself
The properties:
keys(x)
exists thenmap
must preserve it:keys(map(f, x)) == keys(x)
.xs
(isequal(xs, ys)
impliesall(isequal(x,y) for (x,y) in zip(x,y))
) thenmap(f, xs)
must preserve that order.length(x)
exists thenmap
must preserve it:length(map(f, x)) == length(x)
.isequal(map(f ∘ g, x), map(f, map(g, x)))
. This disallows, for example, a statefulx
that counts how many timesmap
is called.Therefore
map(f, x::String)::String
is disallowed since it fails to preservekeys(x)
for multi-unit code points.map(f, ::Set)::Vector
is allowed, sinceSet
haslength
but nokeys
and no iteration order.map(f, ::Set)::Set
would not be allowed, since it fails to preservelength
for non-injectivef
.map(f, (;v)::Some) = Some(f(v))
is allowed, since it has none ofkeys
,iterate
, orlength
but still obeys the composition property.Optional, to discuss, an extra property:
isequal(map(identity, x), x)
would disallowSet -> Vector
andGenerator -> Vector
implementations.Tuple -> Tuple
would still be ok.Ref #57071, #51703
The text was updated successfully, but these errors were encountered: