Created
June 2, 2018 14:01
-
-
Save edutrul/d4a185a94a0ac9d51d19c9d70f4c2024 to your computer and use it in GitHub Desktop.
Dart language first steps
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var _nobleGases = [0, 1]; | |
/* Short way for functions in a single line of code, for instance: | |
* bool isNoble(int atomicNumber) { | |
* return _nobleGases[atomicNumber] != null; | |
* } | |
* // Becomes: | |
* bool isNoble(int atomicNumber) => _nobleGases[atomicNumber] != null; | |
*/ | |
bool isNoble(int atomicNumber) => _nobleGases[atomicNumber] != null; | |
String getPlayerName(String playerName) => playerName ?? 'Guest'; | |
String getMyBirthday() => '07/07/1991'; | |
double getAnyNumber() => 9 + 5.15; | |
void printElement(int element) { | |
print(element); | |
} | |
// This is a shorthand as well for void. | |
/** | |
* void printSomething() { | |
* print('Printing something'); | |
* } | |
*/ | |
printSomething() => print('Printing something'); | |
// fyi ~/ gets integer division. | |
printArithmeticOperation(int a, int b) => print('${a ~/ b} integer division'); | |
/// Returns a function that adds [addBy] to the | |
/// function's argument. | |
Function makeAdder(num addBy) { | |
return (num i) => addBy + i; | |
} | |
// This means returns null. | |
foo() {} | |
// This is the main function that dart recognizes and runs. | |
main() { | |
print(isNoble(1)); | |
String aName; | |
print(getPlayerName(aName)); | |
print(getMyBirthday()); | |
print(getAnyNumber()); | |
printSomething(); | |
printArithmeticOperation(10, 3); | |
print(foo()); | |
var callbacks = []; | |
for (var i = 0; i < 2; i++) { | |
callbacks.add((int a) => print(a + i)); | |
} | |
print(callbacks); | |
callbacks.forEach((c) => c(2)); | |
var list = [1, 2, 3]; | |
print(list[0]); | |
print('ForEach'); | |
// Pass printElement as a parameter. | |
list.forEach(printElement); | |
// list ForEach as "anonymous/closure/lambda" function. | |
list.forEach((int element) { | |
print(element); | |
}); | |
// Anonymous function use in variables. | |
var loudify = (msg) => '!!! ${msg.toUpperCase()} !!!'; | |
print(loudify('Hello world')); | |
List colors = new List(); | |
colors.add('Red'); | |
colors.add('Orange'); | |
colors.add('Purple'); | |
colors.forEach((String color) { | |
print('[${colors.indexOf(color)}] - Color: $color'); | |
}); | |
// It is the same from above but using shorthand function sintax. | |
colors.forEach( | |
(String color) => print('[${colors.indexOf(color)}] - Color: $color')); | |
// Or var adderA = makeAdder(5); | |
// Where 'var' stands for any type of variable. | |
Function adderA = makeAdder(5); | |
// Print 10. | |
print(adderA(5)); | |
// Prints 19 - This is because first evaluate function, then evaluates anonymous function(the one that returns). | |
print(makeAdder(10)(9)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment