First-Order Functions

Passing an anonymous function into an iterator like forEach already demonstrates some nice support for first-class objects—the ability to treat functions as variables that can be assigned and passed around.

Dart’s functional programming capabilities are strong enough to support things like partial function application. The classic example of partial application is converting an add function that returns the sum of three numbers into another function that fixes one of those numbers.

functional_programming/first_order.dart
 
add(x, y, z) {
 
return​ x + y + z;
 
}
 
makeAdder2(fn, arg1) {
 
return​ (y, z) {
 
return​ fn(arg1, y, z);
 
};
 
}
 
var​ add10 = makeAdder2(add, 10);

The name partial application comes from returning ...

Get Dart 1 for Everyone now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.