I guess, I am missing something obvious, but is there any way to destructure a Pair
in functions like mean
, sum
etc?
x = Dict(1 => 2, 3 => 4)
# works
mean(y -> y[2], x)
# not works
mean( (k, v) -> v, x)
Writing y[2]
doesn't look very clean
mean( ((k, v),) -> v, x)
Ah, good! Thank you.
But now I am thinking, that for such short functions, y[2]
is not that bad :-)
Too many brackets.
See also first
and last
, as in mean(y -> last(y), x)
There's also
mean(x) do (k, v)
v
end
if the ((k, v),) -> v
syntax is too hard to read
It's not to hard, it's just lengthy.
I wonder, is there any explanation, why mean((k, v) -> v, d)
is not working as expected? Is it a bug or a feature?
(k, v) -> ...
is how you define a two-argument anonymous function.
Ah, you are right, it's one argument-two elements function.
Yes, then ((k, v), )
make sense.
Last updated: Dec 28 2024 at 04:38 UTC