Post AadCdn2ho4SYHRVHAu by codesections@fosstodon.org
(DIR) More posts by codesections@fosstodon.org
(DIR) Post #AadCdiCRoQHVGD1UAK by massa@hachyderm.io
2023-10-10T14:22:06Z
0 likes, 0 repeats
Not even a one-liner: the weekly challenge today is "Write a script to return the running sum of the given array. Input: @int = (1, 2, 3, 4, 5); Output: (1, 3, 6, 10, 15)"There is a metaoperator in #RakuLang for that (YAY #BatteriesIncluded):It's the `[\]` metaoperator, that applies to another operator and returns the running reduction to another operation. So, the answer to the challenge is ```sub runinng-sum(@_) { [\+] @_ }```more examples (and one with * instead of)...
(DIR) Post #AadCdjMlTY80sUrEsy by codesections@fosstodon.org
2023-10-10T14:46:48Z
0 likes, 0 repeats
@massa @_@hachyderm.io > It's the `[\]` metaoperator, that applies to another operator and returns the running reduction to another operation. So, the answer to the challenge is sub runinng-sum(@_) { [\+] @_ }Or (as I bet you know), Raku also provides the same functionality with &produce. So the following all work: sub running-sum(@_) { produce &sum, @_} my &running-sum = &produce.assuming(&sum); my &running-sum = *.produce(&sum);#rakulang 1/2
(DIR) Post #AadCdkUFJDhsLzMjBY by codesections@fosstodon.org
2023-10-10T14:53:20Z
0 likes, 0 repeats
@massa @_@hachyderm.io @_ The final form ^^^^ (`my &running-sum = *.produce(&sum);`) is probably my favorite: even though it's (slightly) longer than the `[\+]` form, it's more explicit.But where the `[\+]` form *really* shines, imo, is in *not* writing a running-sum function. After all `[\+] 1..9` is both shorter and – assuming you know the syntax – clearer than `running-sum(1..9)`. So there's really no reason to define &running-sum at all.The clearest code is code you don't write at all!#rakulang
(DIR) Post #AadCdlmiU24kNZ0zc8 by massa@hachyderm.io
2023-10-10T14:55:30Z
0 likes, 0 repeats
@codesections @_ @_@fosstodon.org I agreee 100%In the context of another program, the distraction-free option is to write```[\+] @adequately-named-input```whenever a produce, or running sum is needed.
(DIR) Post #AadCdn2ho4SYHRVHAu by codesections@fosstodon.org
2023-10-10T14:58:29Z
0 likes, 0 repeats
@massa @_@hachyderm.io @_ Oops, sorry for the people named @_ accidentally mentioned upthread! (Though you *did* pick that handle 😁 )