Course of Raku / Functional, concurrent, reactive, and web programming / Functional programming / Higher-order functions / Exercises / Filter with a block

Solution: Filter with a block

Here is a possible solution to the task.

Code

say (1..10).grep(* > 5).elems;

🦋 You can find the source code in the file filter-block.raku.

Output

5

Comments

  1. grep takes the block * > 5 and keeps the numbers for which it is true: 6, 7, 8, 9, 10.

  2. .elems counts them, giving 5.

Course navigation

Filter with a block   |   Lambdas and closures