-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathiterator.zig
49 lines (39 loc) · 987 Bytes
/
iterator.zig
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
const std = @import("std");
const expect = std.testing.expect;
fn Numbers() type {
return struct {
numbers: []u8,
const Self = @This();
fn init(ns: []u8) Self {
return Self{
.numbers = ns,
};
}
fn iterator(self: *Self) Iterator {
return Iterator{
.numbers = &self.numbers,
};
}
};
}
const Iterator = struct {
numbers: *[]u8,
pos: usize = 0,
const Self = @This();
pub fn next(self: *Self) ?u8 {
const index = self.pos;
return if (index < self.numbers.len) {
const temp = self.numbers.*[index];
self.pos += 1;
return temp;
} else null;
}
};
pub fn main() !void {
var ar = [_]u8{ 1, 2, 3, 4 };
var outer = Numbers().init(ar[0..]);
var iterator = outer.iterator();
while (iterator.next()) |item| {
std.debug.print("{d}\n", .{item});
}
}