Formatting the dart example

This commit is contained in:
Kyle Mendes 2019-10-27 12:41:50 -05:00
parent ef1ccd2b0f
commit 7e27297ea5

View File

@ -29,6 +29,7 @@ example1() {
nested2() => print("Example1 nested 1 nested 2"); nested2() => print("Example1 nested 1 nested 2");
nested2(); nested2();
} }
nested1(); nested1();
} }
@ -37,6 +38,7 @@ example2() {
nested1(fn) { nested1(fn) {
fn(); fn();
} }
nested1(() => print("Example2 nested 1")); nested1(() => print("Example2 nested 1"));
} }
@ -47,9 +49,12 @@ example3() {
planA(fn(informSomething)) { planA(fn(informSomething)) {
fn("Example3 plan A"); fn("Example3 plan A");
} }
planB(fn) { // Or don't declare number of parameters.
planB(fn) {
// Or don't declare number of parameters.
fn("Example3 plan B"); fn("Example3 plan B");
} }
planA((s) => print(s)); planA((s) => print(s));
planB((s) => print(s)); planB((s) => print(s));
} }
@ -60,17 +65,20 @@ example4() {
nested1(fn(informSomething)) { nested1(fn(informSomething)) {
fn(example4Something); fn(example4Something);
} }
nested1((s) => print(s)); nested1((s) => print(s));
} }
// Class declaration with a sayIt method, which also has closure access // Class declaration with a sayIt method, which also has closure access
// to the outer variable as though it were a function as seen before. // to the outer variable as though it were a function as seen before.
var example5method = "Example5 sayIt"; var example5method = "Example5 sayIt";
class Example5Class { class Example5Class {
sayIt() { sayIt() {
print(example5method); print(example5method);
} }
} }
example5() { example5() {
// Create an anonymous instance of the Example5Class and call the sayIt // Create an anonymous instance of the Example5Class and call the sayIt
// method on it. // method on it.
@ -86,6 +94,7 @@ class Example6Class {
print(instanceVariable); print(instanceVariable);
} }
} }
example6() { example6() {
new Example6Class().sayIt(); new Example6Class().sayIt();
} }
@ -96,10 +105,12 @@ class Example7Class {
static sayItFromClass() { static sayItFromClass() {
print(classVariable); print(classVariable);
} }
sayItFromInstance() { sayItFromInstance() {
print(classVariable); print(classVariable);
} }
} }
example7() { example7() {
Example7Class.sayItFromClass(); Example7Class.sayItFromClass();
new Example7Class().sayItFromInstance(); new Example7Class().sayItFromInstance();
@ -111,7 +122,7 @@ example7() {
// by default. But arrays and maps are not. They can be made constant by // by default. But arrays and maps are not. They can be made constant by
// declaring them "const". // declaring them "const".
var example8Array = const ["Example8 const array"], var example8Array = const ["Example8 const array"],
example8Map = const {"someKey": "Example8 const map"}; example8Map = const {"someKey": "Example8 const map"};
example8() { example8() {
print(example8Array[0]); print(example8Array[0]);
print(example8Map["someKey"]); print(example8Map["someKey"]);
@ -172,6 +183,7 @@ example13() {
print("Example13 regexp doesn't match '${s}'"); print("Example13 regexp doesn't match '${s}'");
} }
} }
match(s1); match(s1);
match(s2); match(s2);
} }
@ -190,7 +202,7 @@ example14() {
} }
// dynamic typed null can be convert to bool // dynamic typed null can be convert to bool
var b;// b is dynamic type var b; // b is dynamic type
b = "abc"; b = "abc";
try { try {
if (b) { if (b) {
@ -240,9 +252,11 @@ example15() {
// StringBuffer. Or you could join a string array. // StringBuffer. Or you could join a string array.
example16() { example16() {
var sb = new StringBuffer(), a = ["a", "b", "c", "d"], e; var sb = new StringBuffer(), a = ["a", "b", "c", "d"], e;
for (e in a) { sb.write(e); } for (e in a) {
sb.write(e);
}
print("Example16 dynamic string created with " print("Example16 dynamic string created with "
"StringBuffer '${sb.toString()}'"); "StringBuffer '${sb.toString()}'");
print("Example16 join string array '${a.join()}'"); print("Example16 join string array '${a.join()}'");
} }
@ -302,11 +316,13 @@ class Example21 {
set names(List<String> list) { set names(List<String> list) {
_names = list; _names = list;
} }
int get length => _names.length; int get length => _names.length;
void add(String name) { void add(String name) {
_names.add(name); _names.add(name);
} }
} }
void example21() { void example21() {
Example21 o = new Example21(); Example21 o = new Example21();
o.add("c"); o.add("c");
@ -320,7 +336,9 @@ class Example22A {
var _name = "Some Name!"; var _name = "Some Name!";
get name => _name; get name => _name;
} }
class Example22B extends Example22A {} class Example22B extends Example22A {}
example22() { example22() {
var o = new Example22B(); var o = new Example22B();
print("Example22 class inheritance '${o.name}'"); print("Example22 class inheritance '${o.name}'");
@ -334,19 +352,21 @@ example22() {
// single inheritance doesn't get in the way of reusable code. // single inheritance doesn't get in the way of reusable code.
// Mixins follow the "with" statement during the class declaration. // Mixins follow the "with" statement during the class declaration.
class Example23A {} class Example23A {}
class Example23Utils { class Example23Utils {
addTwo(n1, n2) { addTwo(n1, n2) {
return n1 + n2; return n1 + n2;
} }
} }
class Example23B extends Example23A with Example23Utils { class Example23B extends Example23A with Example23Utils {
addThree(n1, n2, n3) { addThree(n1, n2, n3) {
return addTwo(n1, n2) + n3; return addTwo(n1, n2) + n3;
} }
} }
example23() { example23() {
var o = new Example23B(), r1 = o.addThree(1, 2, 3), var o = new Example23B(), r1 = o.addThree(1, 2, 3), r2 = o.addTwo(1, 2);
r2 = o.addTwo(1, 2);
print("Example23 addThree(1, 2, 3) results in '${r1}'"); print("Example23 addThree(1, 2, 3) results in '${r1}'");
print("Example23 addTwo(1, 2) results in '${r2}'"); print("Example23 addTwo(1, 2) results in '${r2}'");
} }
@ -362,12 +382,13 @@ class Example24A {
} }
get value => _value; get value => _value;
} }
class Example24B extends Example24A { class Example24B extends Example24A {
Example24B({value: "someOtherValue"}) : super(value: value); Example24B({value: "someOtherValue"}) : super(value: value);
} }
example24() { example24() {
var o1 = new Example24B(), var o1 = new Example24B(), o2 = new Example24B(value: "evenMore");
o2 = new Example24B(value: "evenMore");
print("Example24 calling super during constructor '${o1.value}'"); print("Example24 calling super during constructor '${o1.value}'");
print("Example24 calling super during constructor '${o2.value}'"); print("Example24 calling super during constructor '${o2.value}'");
} }
@ -379,10 +400,11 @@ class Example25 {
var value, anotherValue; var value, anotherValue;
Example25({this.value, this.anotherValue}); Example25({this.value, this.anotherValue});
} }
example25() { example25() {
var o = new Example25(value: "a", anotherValue: "b"); var o = new Example25(value: "a", anotherValue: "b");
print("Example25 shortcut for constructor '${o.value}' and " print("Example25 shortcut for constructor '${o.value}' and "
"'${o.anotherValue}'"); "'${o.anotherValue}'");
} }
// Named parameters are available when declared between {}. // Named parameters are available when declared between {}.
@ -394,17 +416,19 @@ example26() {
_name = name; _name = name;
_surname = surname; _surname = surname;
} }
setConfig2(name, [surname, email]) { setConfig2(name, [surname, email]) {
_name = name; _name = name;
_surname = surname; _surname = surname;
_email = email; _email = email;
} }
setConfig1(surname: "Doe", name: "John"); setConfig1(surname: "Doe", name: "John");
print("Example26 name '${_name}', surname '${_surname}', " print("Example26 name '${_name}', surname '${_surname}', "
"email '${_email}'"); "email '${_email}'");
setConfig2("Mary", "Jane"); setConfig2("Mary", "Jane");
print("Example26 name '${_name}', surname '${_surname}', " print("Example26 name '${_name}', surname '${_surname}', "
"email '${_email}'"); "email '${_email}'");
} }
// Variables declared with final can only be set once. // Variables declared with final can only be set once.
@ -416,6 +440,7 @@ class Example27 {
// that follows the : // that follows the :
Example27({this.color1, color2}) : color2 = color2; Example27({this.color1, color2}) : color2 = color2;
} }
example27() { example27() {
final color = "orange", o = new Example27(color1: "lilac", color2: "white"); final color = "orange", o = new Example27(color1: "lilac", color2: "white");
print("Example27 color is '${color}'"); print("Example27 color is '${color}'");
@ -434,6 +459,7 @@ class Example28 extends IterableBase {
} }
get iterator => names.iterator; get iterator => names.iterator;
} }
example28() { example28() {
var o = new Example28(); var o = new Example28();
o.forEach((name) => print("Example28 '${name}'")); o.forEach((name) => print("Example28 '${name}'"));
@ -459,10 +485,12 @@ example29() {
callItForMe(fn()) { callItForMe(fn()) {
return fn(); return fn();
} }
rand() { rand() {
v = new DM.Random().nextInt(50); v = new DM.Random().nextInt(50);
return v; return v;
} }
while (true) { while (true) {
print("Example29 callItForMe(rand) '${callItForMe(rand)}'"); print("Example29 callItForMe(rand) '${callItForMe(rand)}'");
if (v != 30) { if (v != 30) {
@ -477,8 +505,12 @@ example29() {
// Parse int, convert double to int, or just keep int when dividing numbers // Parse int, convert double to int, or just keep int when dividing numbers
// by using the ~/ operation. Let's play a guess game too. // by using the ~/ operation. Let's play a guess game too.
example30() { example30() {
var gn, tooHigh = false, var gn,
n, n2 = (2.0).toInt(), top = int.parse("123") ~/ n2, bottom = 0; tooHigh = false,
n,
n2 = (2.0).toInt(),
top = int.parse("123") ~/ n2,
bottom = 0;
top = top ~/ 6; top = top ~/ 6;
gn = new DM.Random().nextInt(top + 1); // +1 because nextInt top is exclusive gn = new DM.Random().nextInt(top + 1); // +1 because nextInt top is exclusive
print("Example30 Guess a number between 0 and ${top}"); print("Example30 Guess a number between 0 and ${top}");
@ -488,10 +520,11 @@ example30() {
} else { } else {
tooHigh = n > gn; tooHigh = n > gn;
print("Example30 Number ${n} is too " print("Example30 Number ${n} is too "
"${tooHigh ? 'high' : 'low'}. Try again"); "${tooHigh ? 'high' : 'low'}. Try again");
} }
return n == gn; return n == gn;
} }
n = (top - bottom) ~/ 2; n = (top - bottom) ~/ 2;
while (!guessNumber(n)) { while (!guessNumber(n)) {
if (tooHigh) { if (tooHigh) {
@ -510,12 +543,15 @@ example30() {
// the program needs to startup with. // the program needs to startup with.
main() { main() {
print("Learn Dart in 15 minutes!"); print("Learn Dart in 15 minutes!");
[example1, example2, example3, example4, example5, example6, example7, [
example8, example9, example10, example11, example12, example13, example14, example1, example2, example3, example4, example5,
example15, example16, example17, example18, example19, example20, example6, example7, example8, example9, example10,
example21, example22, example23, example24, example25, example26, example11, example12, example13, example14, example15,
example27, example28, example29, example30 example16, example17, example18, example19, example20,
].forEach((ef) => ef()); example21, example22, example23, example24, example25,
example26, example27, example28, example29,
example30 // Adding this comment stops the dart formatter from putting all items on a new line
].forEach((ef) => ef());
} }
``` ```