Dart Higher Order Functions


Higher-Order Functions in Dart

A higher-order function is a function that can:

  1. Take one or more functions as arguments, or
  2. Return a function as its result.

Higher-order functions are a powerful concept in functional programming, and Dart supports them seamlessly. These functions allow you to write more reusable and flexible code.


Higher-Order Function Example: Passing Functions as Arguments

In Dart, you can pass a function as an argument to another function. Let’s see an example:

// Define a higher-order function that accepts a function as a parameter void performOperation(int a, int b, Function operation) { var result = operation(a, b); // Calling the passed function print("The result of the operation is: $result"); } // Define two functions that we can pass to the higher-order function int add(int a, int b) => a + b; int multiply(int a, int b) => a * b; void main() { // Passing functions as arguments performOperation(5, 3, add); // Output: 8 performOperation(5, 3, multiply); // Output: 15 }

Output:

The result of the operation is: 8 The result of the operation is: 15

In this example:

  • performOperation is a higher-order function because it takes a function (operation) as an argument.
  • We pass add and multiply functions to performOperation, and it uses them to perform the corresponding operations on the given numbers.

Higher-Order Function Example: Returning Functions

In addition to accepting functions as arguments, Dart allows functions to return other functions. Here's an example:

// Define a function that returns another function Function makeMultiplier(int multiplier) { return (int number) { return number * multiplier; }; } void main() { var multiplyBy2 = makeMultiplier(2); // Returns a function that multiplies by 2 var multiplyBy3 = makeMultiplier(3); // Returns a function that multiplies by 3 print(multiplyBy2(5)); // Output: 10 print(multiplyBy3(5)); // Output: 15 }

Output:

10 15

In this example:

  • makeMultiplier is a higher-order function because it returns a function (a closure).
  • multiplyBy2 and multiplyBy3 are functions that "remember" the multiplier value they were created with and use it when called.

Higher-Order Functions with Anonymous Functions

You can also pass anonymous functions (also called lambdas or arrow functions) as arguments to higher-order functions. This is a common pattern in Dart, especially in callbacks and asynchronous programming.

void processList(List<int> numbers, Function operation) { for (var number in numbers) { print(operation(number)); // Applying the operation to each number } } void main() { List<int> numbers = [1, 2, 3, 4, 5]; // Using an anonymous function as an argument processList(numbers, (number) => number * 2); // Doubles each number }

Output:

2 4 6 8 10

In this example:

  • processList is a higher-order function that accepts a list of numbers and a function (operation).
  • We pass an anonymous function that multiplies each number by 2, demonstrating how higher-order functions can be used with inline functions.

Using Built-In Higher-Order Functions

Dart also provides built-in higher-order functions, particularly for collections. For example, you can use map(), reduce(), forEach(), and other functions that take callbacks or return new functions.

Example with map():

void main() { List<int> numbers = [1, 2, 3, 4, 5]; // Using the built-in `map` method (higher-order function) var doubledNumbers = numbers.map((number) => number * 2).toList(); print(doubledNumbers); // Output: [2, 4, 6, 8, 10] }

Output:

[2, 4, 6, 8, 10]

In this example:

  • map() is a higher-order function that takes a function ((number) => number * 2) as an argument and applies it to each element in the list.
  • It returns a new iterable (converted to a list using toList()), which is the result of applying the function to each element.

Summary of Higher-Order Functions in Dart

  1. Passing Functions as Arguments: A higher-order function can accept a function as a parameter.
  2. Returning Functions: A higher-order function can return a function.
  3. Anonymous Functions: Dart allows you to pass anonymous functions as arguments, providing more flexibility.
  4. Built-in Higher-Order Functions: Dart provides many built-in higher-order functions like map(), reduce(), forEach(), etc., to operate on collections.

Higher-order functions help in writing more modular, flexible, and reusable code by allowing functions to act as first-class citizens, just like variables.