by awwaiid · 5y ago · 0 comments
One that is particularly funky is the "whatever-star". This is a "" char that can be used in a few places to mean more or less... whatever. For example:
<4 5 6 7>.map(-> $x { $x + 7 }) # result: (11 12 13 14)
That stabby inline lambda there can be sugared down in a lot of ways, including with the whatever-star: <4 5 6 7>.map(* + 7) # result: (11 12 13 14)
... I've not seen anything quite like that in other languages, not sure if it is a new invention or if only nobody else is crazy enough to try it. And yes, you can use multiplication next to it to make it look crazier: <4 5 6 7>.map(* * 7) # result: (28 35 42 49)
(also yes, ( * *) will take two inputs and multiply them.)I will note though that dwelling on these crazy/fun edge cases is amusing, but programs can look quite a bit more like a basic ruby app (with sigils) if you so desire.
_.methodname()
or _ + 1
is equivalent to x => x.methodname()
or x => x + 1
https://www.scala-lang.org/files/archive/spec/2.13/06-expres...But otoh, Raku nicely stretches the approach to both zero and multiple placeholders:
* The zero placeholder `.methodname` in Raku is the same as Scala's `_.methodname`
* Any number of placeholders are allowed, so one can write eg `* + *` as an anonymous binary add function.
> (also yes, (* * *) will take two inputs and multiply them.)
That's true, but Raku also supports non-Unicode math operators, so the more idiomatic way to write that would be
(* × *)
which, in isolation, still isn't that clear. But it usually is in context. And, if it isn't – well, that's why there's more than one way to do it! (4, 5, 6, 7) ! (. + 7)