-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimmutability_sample.dart
113 lines (89 loc) · 2.14 KB
/
immutability_sample.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// ignore_for_file: avoid_print
// ignore_for_file: constant_identifier_names
import 'dart:core';
/// Example 1.
final name = 'John';
//name = 'Doe';
class ImmutablePoint {
final double x, y;
const ImmutablePoint(this.x, this.y);
}
/// Example 4.
int x = 2;
int sum(int y) => x + y;
int sumOnWorkdays(int x, int y) {
var today = DateTime.now().weekday;
if (1 <= today && today <= 5) {
return x + y;
} else {
throw Exception('do not work on weekend');
}
}
bool canIWorkToday() {
var today = DateTime.now().weekday;
if (1 <= today && today <= 5) {
return true;
} else {
throw Exception('do not work on weekend');
}
}
int sumOnWorkdays2(int x, int y, Function canIWork) {
if (canIWork()) {
return x + y;
} else {
throw Exception('do not work on weekend');
}
}
/// Example 5 - Area of the circle
///
const PI = 3.14;
double areaOfCircle1(double radius) {
return PI * radius * radius;
}
//print(sumOnWorkdays2(1, 2, canIWorkToday));
void main4() {
List<double> inputs = [-1.0, 1.0, 3.0];
for (int i = 0; i < inputs.length; ++i) {
final radius = inputs[i];
if (radius > 0) {
final area = areaOfCircle(radius);
print(area);
}
}
}
/// Declaring variables as final
void main2() {
final String name = "John";
print(name); // Prints John
//name = "David"; // This will cause a compile-time error
const int age = 29;
print(age); // Output: 25
//age = 30; // This will cause a compile-time error
}
void main3() {
/// Example 2.
List<int> numbers = [1, 2, 3];
numbers.add(4); // [1, 2, 3, 4]
print(numbers);
/* List<double> inputs = [-1.0, 1.0, 3.0];
inputs
.where((input) => input > 0)
.map((radius) => areaOfCircle(radius, print));*/
}
double calculateArea2(double radius) {
return PI * radius * radius;
}
double areaOfCircle(double radius) {
return PI * radius * radius;
}
void main() {
List<double> inputs = [-1.0, 1.0, 3.0];
inputs
.skip(1)
.where((input) => input > 0)
.map((radius) => areaOfCircle(radius))
.map((area) => area.toStringAsFixed(2))
.toList()
.take(2)
.forEach((area) => print(area));
}