Course of Raku / Advanced / Subroutines / Calling with a colon 🆕 / Exercises / A colon map

Solution: A colon map

Here is a possible solution to the task.

Code

say (1..10).map(* * 2).grep: * > 10;

🦋 You can find the source code in the file colon-map.raku.

Output

(12 14 16 18 20)

Comments

  1. Only grep, the last call in the chain, can use the colon form. The colon makes * > 10 its argument, exactly as grep(* > 10) would. As we still need to print the results, say is now used as a function, not as a method.

  2. The map call must keep its parentheses. If you wrote .map: * * 2 instead, the colon would swallow .grep(* > 10) as part of map’s arguments, and the chain would break.

Course navigation

A colon map   |   Join with a colon