Course of Raku / Addendum 🆕 / Flow and functions / Subroutines and functional style / Exercises / Sum of even squares
Solution: Sum of even squares
Here is a possible solution to the task.
Code
say [+] (1..10).grep(* %% 2).map(* ** 2);🦋 You can find the source code in the file sum-even-squares.raku.
Output
220Comments
The chain reads left to right:
.grep(* %% 2)keeps the even numbers,.map(* ** 2)squares each, and[+]reduces the squares to their sum.The even numbers
2 4 6 8 10square to4 16 36 64 100, which add up to220.The same steps can be written as a feed pipeline, where
==>passes each result on to the next stage — so the flow reads top to bottom rather than as a method chain:(1..10) ==> grep(* %% 2) ==> map(* ** 2) ==> sum() ==> say();
Each
==>feeds its left-hand list into the next routine, and the final==> say()prints the total,220.