Course of Raku / Functional, concurrent, reactive, and web programming / Functional programming

Higher-order functions

In Raku a subroutine is an ordinary value: you can store it in a variable, pass it to another subroutine, and return it as a result. A subroutine that takes or returns another subroutine is called a higher-order function, and it is the heart of functional programming.

You have already used higher-order functions without naming them — map, grep, and sort all take a block of code as an argument. This section shows how to write your own.

These block-taking methods are also a natural place for the colon call form, which lets you drop the parentheses — (1..10).grep: * %% 2 instead of (1..10).grep(* %% 2). It is introduced in Calling with a colon, and the * shorthand it uses is the Whatever star.

Topics in this section

Practice

Complete the quizzes that cover the contents of this section.

Exercises

This section contains 3 exercises. Examine all the topics of this section before doing the coding practice.

  1. Apply twice
  2. Make a multiplier
  3. Filter with a block

Course navigation

Solution: Count up   |   Passing subroutines