-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfbz_psycho_7.dart
116 lines (97 loc) · 2.36 KB
/
fbz_psycho_7.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
114
115
116
import 'dart:async';
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
/// Using a StreamController
Future<void> main(List<String> args) async {
final controller = StreamController<Object?>(sync: true);
final fbOutput = FizzBuzzOutput(
controller: controller,
fizzBuzzTrans: FizzBuzzTrans(),
fizzTrans: FizzTrans(),
buzzTrans: BuzzTrans(),
)..init();
for (var i = 0; i < 31; i++) {
fbOutput.outputFor(number: i);
}
await controller.close();
}
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
class FizzBuzzOutput {
const FizzBuzzOutput({
required this.controller,
required this.fizzBuzzTrans,
required this.fizzTrans,
required this.buzzTrans,
});
final StreamController controller;
final FBTrans fizzBuzzTrans;
final FBTrans fizzTrans;
final FBTrans buzzTrans;
void init() {
controller.stream
.transform(
StreamTransformer.fromHandlers(
handleData: (data, sink) => fizzBuzzTrans.handle(
data: data,
sink: sink,
),
),
)
.transform(
StreamTransformer.fromHandlers(
handleData: (data, sink) => fizzTrans.handle(
data: data,
sink: sink,
),
),
)
.transform(
StreamTransformer.fromHandlers(
handleData: (data, sink) => buzzTrans.handle(
data: data,
sink: sink,
),
),
)
.listen((e) => print(e));
}
void outputFor({required int number}) {
controller.sink.add(number);
}
}
abstract class FBTrans {
const FBTrans();
String get message;
int get divisor;
void handle({required Object? data, required EventSink<Object?> sink}) {
if (data is String) {
sink.add(data);
return;
}
if ((data as int) % divisor == 0) {
sink.add(message);
return;
}
sink.add(data);
}
}
class FizzBuzzTrans extends FBTrans {
const FizzBuzzTrans();
@override
int get divisor => 15;
@override
String get message => 'FizzBuzz,';
}
class FizzTrans extends FBTrans {
const FizzTrans();
@override
int get divisor => 3;
@override
String get message => 'Fizz,';
}
class BuzzTrans extends FBTrans {
const BuzzTrans();
@override
int get divisor => 5;
@override
String get message => 'Buzz,';
}