Course of Raku / Functional, concurrent, reactive, and web programming / Functional programming / Higher-order functions / Exercises / Apply twice
Solution: Apply twice
Here is a possible solution to the task.
Code
sub twice(&f, $x) {
f(f($x));
}
say twice(* * 3, 2);🦋 You can find the source code in the file apply-twice.raku.
Output
18Comments
The
&fparameter receives a subroutine; the innerf($x)is fed into the outerf(...).Tripling
2gives6, and tripling again gives18.
Course navigation
← Apply twice | Make a multiplier →