Tags

, , , , , ,

I teach computer science and programming languages, such as C, C++, C#, Python, PHP, Java, and Dart; you may also learn PHP framework like Laravel. Just drop me a message either at my email or Skype.

E-mail - sanjib12sinha@gmail.com
Skype ID - live:sanjib12sinha

Learn Dart the Hard Way

In this article we will take a look at some special functions in Dart programming language – Anonymous Functions: Lambda, Higher Order Functions, and Lexical Closures
Lambda, Higher Order functions, and Lexical Closures have some similarities. In their namelessness and anonymity, these features of Dart are very interesting. Let us first start with Lambda. Then we will discuss Higher Order functions and Closures. In reality, you will find that Lambda actually implements Higher Order Functions.
Lambda: The Anonymous Function
As the name suggests, Lambda is a nameless function and we can use it in two ways. We can use it in a traditional method and also we can use the ‘Fat Arrow’.
Consider the first code snippet:
//code 8.1
class LambdaCode{
// here addingTwonumbers is a nameless function
Function addingTwonumbers = (int x, int y){
var sum = x + y;
return sum;
};
}
main(List arguments){
var lambdaShow = LambdaCode();
print(lambdaShow.addingTwonumbers(12, 47));
}

I will give the output after adding the ‘Fat Arrow’ method. The whole code snippet looks like this:
//code 8.2
class LambdaCode{
// here addingTwonumbers is a nameless function
Function addingTwonumbers = (int x, int y){
var sum = x + y;
return sum;
};
Function divideByFour = (int num) => num ~/ 4;
}
main(List arguments){
var lambdaShow = LambdaCode();
print(lambdaShow.addingTwonumbers(12, 47));
print(lambdaShow.divideByFour(56));
}

The output is quite expected, in the first anonymous function ‘Function addingTwonumbers’ we have passed two parameters and added them. And using the ‘Fat Arrow’ method, we have passed a number through another nameless function ‘Function divideByFour’ and divided it by 4.
//output of code 8.2
59
14

While building a native iOS or Android app, you will see how these nameless functions come to your help.

Exploring Higher Order Functions

The specialty of Higher Order functions is it can accept a function as a parameter. That is why it is named Higher Order Function. It not only can accept a function as a parameter, but it can also return it; actually, it can do both.
We will see a very simple code snippet to get accustomed to the idea.
//code 8.3
//returning a function
Function DividingByFour(){
Function LetUsDivide = (int x) => x ~/ 4;
return LetUsDivide;
}
main(List arguments){
var result = DividingByFour();
print(result(56));
}

The output is 14.
So we have passed a nameless function ‘Function LetUsDivide’ as a parameter and returned the function quite easily through a higher order function ‘Function DividingByFour()’.
The closure is a Special Function
We can define Closure in two ways. According to the first definition, we can say that Closure is the only function that has access to the parent scope, even after the scope is closed.
To understand this definition, let us see a very short code snippet:
//code 8.4
//a closure can modify the parent scope
String message = "Any Parent String";
Function overridingParentScope = (){
String message = "Overriding the parent scope";
print(message);
};
main(List arguments){
print(message);
overridingParentScope();
}
The output is:
Any Parent String
Overriding the parent scope

By the second definition, we can say that a Closure is a function object that has access to the variables in its lexical scope, even when the function is used outside of its original scope.
//code 8.5
Function show = (){
String pathToImage = "This is an old path.";
Function gettingImage(){
String path = "This is a new path to image.";
print(path);
}
return gettingImage;
};
main(List arguments){
var showing = show();
showing();
}
Here is the output: This is a new path to image.

It actually returns a function object ‘gettingImage’ that has accessed the variable in its lexical scope.

So at the end of this section, we can summarize the points about Closure.
1. In several other languages, you are not allowed to modify the parent variable.
2. However, within a closure, you can mutate or modify the values of variables present in the parent scope.
3. All together, Closure is a very special type of function.

Now we will conclude our whole journey to study the nameless functions in one single code base, and we will also see the output.
//code 8.6
//Lambda is an anonymous function
class AboutLambdas{
//first way of expressing Lambda or anonymous function
Function addingNumbers = (int a, int b){
var sum = a + b;
//print(sum);
return sum;
};
Function multiplyWithEight = (int num){
return num * 8;
};
//second way of expressing Lambda by Fat Arrow
Function showName = (String name) => name;
//higher order functions pass function as parameter
int higherOrderFunction(Function myFunction){
int a = 10;
int b = 20;
print(myFunction(a, b));
}
//returning a function
Function returningAFunction(){
Function showAge = (int age) => age;
return showAge;
}
//a closure can modify the parent scope
String anyString = "Any Parent String";
Function overridingParentScope = (){
String message = "Overriding the parent scope";
print(message);
};
Function show = (){
String pathToImage = "This is an old path.";
Function gettingImage(){
String path = "This is a new path to image.";
print(path);
}
return gettingImage;
};
}
main(List arguments){
var add = AboutLambdas();
var addition = add.addingNumbers(5, 10);
print(addition);
var mul = AboutLambdas();
var result = mul.multiplyWithEight(4);
print(result);
var name = AboutLambdas();
var myName = name.showName("Sanjib");
print(myName);
var higher = AboutLambdas();
var higherOrder = higher.higherOrderFunction(add.addingNumbers);
higherOrder;
var showAge = AboutLambdas();
var showingAge = showAge.returningAFunction();
print(showingAge(25));
var sayMessage = AboutLambdas();
sayMessage.overridingParentScope();
var image = AboutLambdas();
var imagePath = image.show();
imagePath();
}

And in the output we will see how the nameless functions work in different ways.
//output of code 8.6
15
32
Sanjib
30
25
Overriding the parent scope
This is a new path to image.