Exploring the Power of Lambda Function in Dart

Home » Dart » Exploring the Power of Lambda Function in Dart

Lambda in Dart is a concept that enables you to write code that is both concise and powerful. It is a feature that allows you to create anonymous functions in a very simple way.

You can call a function by its name and pass arguments to it, and it will return a value based on the instructions it contains. A Lambda is a special type of function that is defined without a name. It is also known as an anonymous function.

Exploring the Power of Lambda Function in Dart

In Dart, you can create a Lambda using the => symbol. The syntax for a Lambda is:

(parameters) => expression;

Let’s break down this syntax. The parameters are the inputs that the Lambda function takes. They are listed inside the parentheses. The expression is the code that the Lambda function executes. It is placed after the => symbol.

The expression can be a single line of code or a block of code wrapped in curly braces.

Here’s an example of a Lambda that takes two parameters and returns their sum:

(int a, int b) => a + b;

This Lambda takes two integer parameters, a and b, and returns their sum. It is equivalent to the following named function.

int sum(int a, int b) {
  return a + b;
}

One advantage of using a Lambda is that it allows you to write more concise and readable code.

To check whether a number is odd or even using a Lambda in Dart

Odd or Even using Lambda in Dart

Open the Dart Pad and Write the following code.

void main()
{
  
  final isOdd = (int number) => number % 2 != 0;
  final isEven = (int number) => number % 2 == 0;
  
  int num = 11;
  
  if(isOdd(num))
  {
    print('$num is odd');
    
  }else if(isEven(num))
  {
    print('$num is even');
  }
 
  
}

Output will be 11 is odd

In this example, we define two Lambdas: isOdd and isEven. Each Lambda takes an integer parameter and returns a boolean value indicating whether the number is odd or even, respectively.

To check if a number is odd or even, we assign a value of 11 to num. We then use an if statement to check if num is odd or even by calling the isOdd and isEven Lambdas, respectively.

You can easily modify this code to check whether a different number is odd or even by changing the value assigned to num.

One advantage of using a Lambda is that it allows you to write more concise and readable code. For example, if you want to sort a list of integers in ascending order, you can use the sort() method with a Lambda

void main()
{
  
  
  List<int> numbers = [4,6,7,3,5,6];
  print(numbers);
  
  numbers.sort((a,b) => a.compareTo(b));
  
  print(numbers);
  
  
}

This Lambda takes two integer parameters, a and b, and uses the compareTo() method to compare them. The sort() method uses this Lambda to sort the list in ascending order.

Exploring the Power of Lambda Function in Dart

How to find even numbers of a list using Dart Lambda function ?

Solution

void main()
{
  
  
  List<int> numbers = [3,1,4,1,5,9,44,68];
  List<int> evenNumbers = numbers.where((number)=>number.isEven).toList();
  print(evenNumbers);
  
  
}
[4, 44, 68]

This Lambda takes a single integer parameter, number, and returns a boolean value indicating whether the number is even. The where() method uses this Lambda to filter the list and return a new list containing only the even numbers.

Lambda in Dart is a powerful feature that allows you to write concise and readable code. It enables you to create anonymous functions that can be passed as arguments to other functions, making it a powerful tool for functional programming

You may also like...