-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathREADME.example.zig
48 lines (37 loc) · 1.42 KB
/
README.example.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
const zignite = @import("src/zignite.zig");
const std = @import("std");
fn even(x: usize) bool {
return @mod(x, 2) == 0;
}
const RepeatTake = zignite.Repeat(usize).Take();
fn repeatTake(x: usize) RepeatTake {
return zignite.repeat(usize, x).take(x);
}
test "Example Code 1" {
const x = zignite
.range(usize, 0, 100) // { 0, 1, ..., 99 }
.filter(even) // { 0, 2, ..., 98 }
.flatMap(RepeatTake, repeatTake) // { 2, 2, 4, 4, 4, 4, ..., 98 }
.sum();
try std.testing.expect(x == 161700);
}
test "Example Code 2" {
const lazy = zignite
.range(usize, 1, 10) // { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }
.filter(even); // { 2, 4, 6, 8, 10 }
try std.testing.expect(lazy.sum() == 30);
try std.testing.expect(lazy.product() == 3840);
}
test "Example Code 3" {
const allocator = std.testing.allocator;
const slice = "Example Code 3";
// []const u8 -> BoundedArray(u8, 50)
const bounded_array = try zignite.fromSlice(u8, slice).toBoundedArray(50);
// BoundedArray(u8, 50) -> ArrayList(u8)
var array_list = try zignite.fromBoundedArray(u8, 50, &bounded_array).toArrayList(allocator);
defer array_list.deinit();
// ArrayList(u8) -> []const u8
var buffer: [50]u8 = undefined;
const slice2 = zignite.fromArrayList(u8, &array_list).toSlice(&buffer).?;
try std.testing.expect(std.mem.eql(u8, slice, slice2));
}