Is it possible to access the "callee" ("this") in Raku map code block and/or whatevercode?
E.g. if one wanted to calculate arithmetic mean
my $data = <1 10 0 7 2> ;
say $data.sum
/ $data ; # or @$data, same as $data.elems in this numeric context
# OUTPUT: «4»
by sum of fractions
say $data.map(* / $data).sum
# OUTPUT: «4»
like
<1 10 0 7 2>.map({ $_ / $this }).sum # but there's no $this
or
<1 10 0 7 2>.map( * / $this ).sum
If not, is there an idiomatic way to do it with only a chain of method calls on the <…>
data literal, or different way, without storing the data in variable?
There is not, to my knowledge. You would need to be able to know how many values an iterator will produce.
Under the hood, a foo.map: &callable
is basically a:
my $iterator = foo.iterator;
callable($iterator.pull-one) until iterator exhausted;
Note that all knowledge of the source of the iterator is lost at that point: there's just an iterator producing values.
Some types of iterators (the so-called PredictiveIterator
s can tell you how many values it can still produce without actually producing them. But most iterators are not able to do that.
I guess technically it would be possible for map
to check whether it got a PredictiveIterator
and set a dynamic variable with the number of values it will produce, which you could then check inside the &callable
.
Still, it doesn't feel that this is worth the extra check for all map
s and for
loops, so I don't think a PR for such a feature would be accepted.
you could do this, by andthen
<1 10 0 7 2>
andthen .sum / .elems
andthen .say
or
<1 10 0 7 2>
andthen .map: * / .elems
andthen .sum
andthen .say
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With