• About
  • Contact
  • Books

Sanjib Sinha

~ On Computer Science, PHP, Python, Ethical Hacking and more…

Sanjib Sinha

Tag Archives: Native iOS

Introduction to Collections: Arrays are Lists in Dart

11 Sunday Aug 2019

Posted by Sanjib Sinha in Android, Dart, Flutter, iOS, Programming Language

≈ Leave a comment

Tags

Android, anonymous function, array, arrays in Dart, collection, Dart, dart functions, dart programming, Lambda in Dart, List, Lists in Dart, Mobile Device, Native iOS, Object Oriented Methodology, Object Oriented Programming, programming in dart

laern-dart-the-hard-way

Learn Dart the Hard Way

This is the most common collection in every programming language: array or an “ordered group of objects”. In Dart, arrays are List objects. We will address them as ‘lists’ in our future discussion.
JavaScript array literals look like Dart lists. Here is a sample code we may consider to understand why this concept is important:

//code 2.14
main(List arguments) {
List fruitCollection = ['Mango', 'Apple', 'Jack fruit'];
print(fruitCollection[0]);
}

Consider another piece of code:

//code 2.15
main(List arguments) {
List fruitCollection = ['Mango', 'Apple', 'Jack fruit'];
var myIntegers = [1, 2, 3];
print(myIntegers[2]);
print(fruitCollection[0]);
}

What is the difference between these two code snippets? In the above code 2.14, we have explicitly mentioned that we are going to declare a collection of fruits. And we can pick any item from that collection from the key.

As we know, when in an array key is not mentioned with the value pair, it automatically infers that the key starts from 0. Therefore, the output of code 2.14 is ‘Mango’. In the second instance, Continue reading →

Share this:

  • Facebook
  • WhatsApp
  • Twitter

Like this:

Like Loading...

Anonymous Functions: Lambda, Higher Order Functions, and Lexical Closures

03 Saturday Aug 2019

Posted by Sanjib Sinha in Dart, iOS, Mobile Device, Native iOS, Object Oriented Programming, Programming Language

≈ Leave a comment

Tags

Android, anonymous function, Dart, dart functions, dart programming, Lambda in Dart, Mobile Device, Native iOS, Object Oriented Methodology, Object Oriented Programming, programming in dart

laern-dart-the-hard-way

Learn Dart the Hard Way

Lambda, Higher-Order functions, and Lexical Closures have some similarities. In their namelessness and anonymity, these features of Dart are very interesting. Let us start with Lambda. Then we will discuss Higher-Order Functions and Closures. In reality, you will find that Lambda actually implements Higher-Order Functions.
Today I am going to talk about the Lambda only.
Next day, I will write about the other anonymous functions.
Continue reading →

Share this:

  • Facebook
  • WhatsApp
  • Twitter

Like this:

Like Loading...

Why we need Constructor in Dart

20 Thursday Jun 2019

Posted by Sanjib Sinha in Dart, Mobile Device, Native iOS, Object Oriented Programming

≈ Leave a comment

Tags

Android, Dart, dart functions, dart programming, Mobile Device, Native iOS, Object Oriented Methodology, Object Oriented Programming, programming in dart

Learn Dart the Hard Way

The first and foremost task of constructors is the construction of objects. Whenever we try to create an object and write this line:
var fatherBear = Bear();
We actually try to arrange a spot in the memory for that object. The real work begins when we connect that spot with class properties and methods.
Using ‘constructor’ we can do that job more efficiently. Not only that, Dart allows to create more than one ‘constructor’, which is a great advantage.
Let us write our ‘Bear’ class in a new way of using constructor:

//code 3.10
class Bear {
int numberOfFish;
int hourOfSleep;
int weightGain;
Bear(this.numberOfFish, this.hourOfSleep);
int eatFish(int numberOfFish) => numberOfFish;
int sleepAfterEatingFish(int hourOfSleep) => hourOfSleep;
int weightGaining(int weightGain) => weightGain = numberOfFish * hourOfSleep;
}
main(List arguments){
var fatherBear = Bear(6, 10);
fatherBear.weightGain = fatherBear.numberOfFish * fatherBear.hourOfSleep;
print("Father bear eats ${fatherBear.eatFish(fatherBear.numberOfFish)} fishes. And he sleeps for ${fatherBear.sleepAfterEatingFish(fatherBear.hourOfSleep)} hours.");
print("Father bear has gained ${fatherBear.weightGaining(fatherBear.weightGain)} pounds of weight.");
}

Creating ‘constructor’ is extremely easy. Watch this line:

Bear(this.numberOfFish, this.hourOfSleep);

The same class name works as a function or method and we have passed two arguments through that method. Once we get those values, we would calculate the third variable. Writing constructor this way is known as “Syntactic Sugar”. In the later section of the book we will know more about the constructor.
Now it gets easier to pass the two values while creating the object. We could have done the same by creating constructor this way, which is more traditional:

//code 3.11
class Bear {
int numberOfFish;
int hourOfSleep;
int weightGain;
Bear(int numOfFish, int hourOfSleep){
this.numberOfFish = numOfFish;
this.hourOfSleep = hourOfSleep;
}
//Bear(this.numberOfFish, this.hourOfSleep);
int eatFish(int numberOfFish) => numberOfFish;
int sleepAfterEatingFish(int hourOfSleep) => hourOfSleep;
int weightGaining(int weightGain) => weightGain = numberOfFish * hourOfSleep;
}
main(List arguments){
var fatherBear = Bear(6, 10);
fatherBear.weightGain = fatherBear.numberOfFish * fatherBear.hourOfSleep;
print("Father bear eats ${fatherBear.eatFish(fatherBear.numberOfFish)} fishes. And he sleeps for ${fatherBear.sleepAfterEatingFish(fatherBear.hourOfSleep)} hours.");
print("Father bear has gained ${fatherBear.weightGaining(fatherBear.weightGain)} pounds of weight.");
}

In both cases, the output is same as before:

//output of code 3.11
Father bear eats 6 fishes. And he sleeps for 10 hours.
Father bear has gained 60 pounds of weight.

In the above code, you can even get the object’s type very easily. We can change the type of value quite easily. Watch the main() function again:

//code 3.12
main(List arguments){
var fatherBear = Bear(6, 10);
fatherBear.weightGain = fatherBear.numberOfFish * fatherBear.hourOfSleep;
print("Father bear eats ${fatherBear.eatFish(fatherBear.numberOfFish)} fishes. And he sleeps for ${fatherBear.sleepAfterEatingFish(fatherBear.hourOfSleep)} hours.");
print("Father bear has gained ${fatherBear.weightGaining(fatherBear.weightGain)} pounds of weight.");
print("The type of the object : ${fatherBear.weightGain.runtimeType}");
String weightGained = fatherBear.weightGain.toString();
print("The type of the same object has changed to : ${weightGained.runtimeType}");
}

And here is the output:

//output of code 3.12
Father bear eats 6 fishes. And he sleeps for 10 hours.
Father bear has gained 60 pounds of weight.
The type of the object : int
The type of the same object has changed to : String

Share this:

  • Facebook
  • WhatsApp
  • Twitter

Like this:

Like Loading...

Subscribe

  • Entries (RSS)
  • Comments (RSS)

Archives

  • August 2019
  • July 2019
  • June 2019
  • May 2019
  • April 2019
  • March 2019
  • January 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • May 2018
  • April 2018
  • March 2018
  • January 2018
  • December 2017
  • November 2017
  • August 2017
  • July 2017
  • June 2017
  • May 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
  • October 2016
  • September 2016
  • June 2016
  • May 2016
  • April 2016
  • March 2016
  • February 2016

Categories

  • Android
  • Classes and Object
  • CSS
  • Dart
  • Data Journalism
  • Data-Journalism
  • Ethical hacking
    • kali linux
  • Ethical Hacking for Brginners
  • Exceptions
  • Flutter
  • General
  • git
  • HTML
  • iOS
  • JavaScript
  • Laravel 5
  • Laravel 5.2.4: Be A Laravel Expert
  • laravel 5.7
  • laravel 5.7 all model relations explained
  • Laravel 5.8
  • laravel 5: learn easy
  • life
  • metasploitable
  • Mobile Device
  • MySQL
  • NASA
  • Native iOS
  • Object Oriented Programming
  • operating systems
  • PHP
  • php 7
  • PHP 7 in 7 Days
  • PHP Developers
  • Programming Language
  • Python
    • General Syntax
    • Variables, Objects annd Values
  • Software
  • tehelka
  • wordpress

Meta

  • Register
  • Log in

Create a free website or blog at WordPress.com.

Cancel
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy
%d bloggers like this: