Course of Raku / Addendum 🆕 / Flow and functions / Subroutines and functional style / Exercises / Compose two functions

Solution: Compose two functions

Here is a possible solution to the task.

Code

sub compose(&f, &g) {
    return -> $x { f(g($x)) };
}

my $combined = compose(* * 2, * + 1);

say $combined(5);

🦋 You can find the source code in the file compose.raku.

Output

12

Comments

  1. The &f and &g parameters accept any callables — here the Whatever functions * * 2 and * + 1.

  2. compose returns a fresh anonymous function -> $x { f(g($x)) }. Calling it on 5 runs g first (5 + 1), then f (6 * 2), giving 12.

Course navigation

Compose two functions   |   A running accumulator