-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTest.hx
60 lines (51 loc) · 1.1 KB
/
Test.hx
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
using Test;
class Test {
static function main() {
trace("Haxe is great!");
trace("foo\nbar".lines().filter(s -> s != "foo").collect());
}
static function lines(s:String):Iterator<String> {
return s.split("\n").iterator(); // TODO: bad! should be a lazy iter!
}
static function filter<T>(i:Iterator<T>, fn:T->Bool):Iterator<T> {
return new FilterIter(i, fn);
}
static function collect<T>(iter:Iterator<T>):Array<T> {
return [for (i in iter) i];
}
}
class FilterIter<T> {
var inner:Iterator<T>;
var fn:T->Bool;
var _hasNext:Bool = false;
var _next:T = null;
public inline function new(i:Iterator<T>, fn:T->Bool) {
inner = i;
this.fn = fn;
while (inner.hasNext()) {
var n = inner.next();
if (fn(n)) {
_hasNext = true;
_next = n;
break;
}
}
}
public inline function hasNext():Bool {
return _hasNext;
}
public inline function next():Null<T> {
var ret = if (_hasNext) _next else null;
_hasNext = false;
_next = null;
while (inner.hasNext()) {
var n = inner.next();
if (fn(n)) {
_hasNext = true;
_next = n;
break;
}
}
return ret;
}
}