Skip to content

Commit 96fbbfa

Browse files
committed
fix: swap std.heap.c_allocator for page_allocator on old examples
1 parent 88bfd09 commit 96fbbfa

File tree

9 files changed

+98
-125
lines changed

9 files changed

+98
-125
lines changed

src/base64.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const std = @import("std");
22

33
pub fn main() !void {
4-
var arena = std.heap.ArenaAllocator.init(std.heap.c_allocator);
4+
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
55
defer arena.deinit();
66
var allocator = arena.allocator();
77

src/comp-partial.zig

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,39 +13,26 @@ fn div10(comptime T: type, v: T) T {
1313
return @divTrunc(v, 10);
1414
}
1515

16-
fn addX(comptime T: type, v: T, a: T) T {
17-
return v + a;
16+
fn addX(comptime T: type, comptime v: T, x: T) T {
17+
return v + x;
1818
}
1919

20-
fn divX(comptime T: type, d: T, v: T) T {
21-
return @divFloor(v, d);
20+
fn multX(comptime T: type, comptime v: T, x: T) T {
21+
return v * x;
2222
}
2323

24-
fn multX(v: anytype, m: anytype) @TypeOf(m) {
25-
return v * m;
24+
fn multXX(comptime T: type, comptime v: T, comptime u: T, x: T) T {
25+
return v * u * x;
2626
}
2727

2828
pub fn main() !void {
29-
const mult5 = f.partial(multX, 5);
30-
std.debug.print("\n{d}", .{ mult5(3) });
31-
32-
// const c_f32 = f.comp(f32, &.{
33-
// f.partial(f32, multX, 5),
34-
// f.partial(f32, addX, 10),
35-
// });
36-
// std.debug.print("\n\n(x * 5) + 10", .{});
37-
// for (1..10) |x| {
38-
// const result = c_f32(@floatCast(@as(f32, @floatFromInt(x))));
39-
// std.debug.print("\n{x:.2} → {d:>5.2}, {any}", .{ x, result, @TypeOf(result) });
40-
// }
41-
42-
// const c_i32 = f.comp(i32, &.{
43-
// f.partial(i32, multX, 10),
44-
// f.partial(i32, divX, 2),
45-
// });
46-
// std.debug.print("\n\n(x * 10) / 2", .{});
47-
// for (1..10) |x| {
48-
// const result = c_i32(@intCast(x));
49-
// std.debug.print("\n{d} → {d}, {any}", .{ x, result, @TypeOf(result) });
50-
// }
29+
const mult5 = f.partial(u8, multX, 5);
30+
const mult2and5 = f.partial2(u8, multXX, 2, 5);
31+
std.debug.print("\n{d}, {d}", .{mult5(3), mult2and5(3)});
32+
33+
const comp_fn = f.comp(u8, &.{
34+
f.partial(u8, multX, 5),
35+
f.partial(u8, addX, 10),
36+
});
37+
std.debug.print("\n\n(x * 5) + 10: {d}", .{ comp_fn(3) });
5138
}

src/csv-transduce.zig

Lines changed: 12 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -38,85 +38,24 @@ pub fn mul(comptime T: type, m: T, arg: T) T {
3838
return arg * m;
3939
}
4040

41-
pub fn wrapNoOpts(comptime outer: []const u8, comptime inner: []const u8) []const u8 {
42-
return "<" ++ outer ++ ">" ++ inner ++ "</" ++ outer ++ ">";
41+
pub fn wrapNoOpts(comptime _: @TypeOf([]const u8), alloc: std.mem.Allocator, outer: []const u8, inner: []const u8) []const u8 {
42+
return std.fmt.allocPrint(alloc, "<{s}>{s}</{s}>", .{ outer, inner, outer }) catch return "";
4343
}
4444

45-
pub fn wrap(comptime outer: []const u8, comptime options: []const u8, comptime inner: []const u8) []const u8 {
46-
return "<" ++ outer ++ " " ++ options ++ ">" ++ inner ++ "</" ++ outer ++ ">";
45+
pub fn wrap(comptime _: @TypeOf([]const u8), alloc: std.mem.Allocator, outer: []const u8, options: []const u8, inner: []const u8) []const u8 {
46+
return std.fmt.allocPrint(alloc, "<{s} {s}>{s}</{s}>", .{ outer, options, inner, outer }) catch return "";
4747
}
4848

4949

5050
pub fn main() !void {
51+
// const inner = "<circle cx=\"50\" cy=\"50\" r=\"40\" stroke=\"green\" stroke-width=\"4\" fill=\"yellow\" />";
52+
// _ = inner;
5153

52-
const inner = "<circle cx=\"50\" cy=\"50\" r=\"40\" stroke=\"green\" stroke-width=\"4\" fill=\"yellow\" />";
53-
_ = inner;
54-
55-
const svg = f.partial2Typed([]const u8, wrap, "svg", "viewBox=\"0 0 300 100\" xmlns=\"http://www.w3.org/2000/svg\"");
56-
const g = f.partialTyped([]const u8, wrapNoOpts, "g");
57-
const t = f.partial2Typed([]const u8, wrap, "text", "fill=\"black\" x=\"0\" y=\"0\"");
58-
59-
const output = svg(g(t("Test")));
60-
std.debug.print("\n{s}", .{ output });
61-
62-
63-
// const file_path = "out/test.svg";
64-
// const file = try fs.cwd().createFile(file_path, .{});
65-
// defer file.close();
66-
// _ = try file.write(output);
67-
68-
54+
const svg = f.partialGeneric([]const u8, wrap, .{ allocator, "sv", "viewBox=\"0 0 300 100\" xmlns=\"http://www.w3.org/2000/svg\""});
55+
const g = f.partialGeneric([]const u8, wrapNoOpts, .{ allocator, "g" });
56+
// const t = f.partial2([]const u8, wrap, "text", "fill=\"black\" x=\"0\" y=\"0\"");
6957

70-
// const input = [_]f32{9, 18, 27};
71-
// _ = input;
72-
// const result = try map([]f32, @as(f32, 10.0), f.partial(f32, mul, 9));
73-
// for (result) |r| {
74-
// std.debug.print("\n{d}", .{ r });
75-
// }
76-
77-
// const file_path = "data/dailyActivity_merged.csv";
78-
// const file = try fs.cwd().openFile(file_path, .{ .mode = .read_only });
79-
// defer file.close();
80-
// const reader = file.reader();
81-
// var contents = std.ArrayList(u8).init(allocator);
82-
// try reader.streamUntilDelimiter(contents.writer(), '\n', null);
83-
// std.debug.print("\n{s}\nCount: {d}", .{ contents.items, contents.items.len });
84-
85-
// const line : []const u8 = try allocator.dupe(u8, contents.items);
86-
87-
// var splitIt = std.mem.tokenize(u8, line, ",");
88-
// while (splitIt.next()) |entry| {
89-
// std.debug.print("\n{s}", .{ entry });
90-
// }
91-
92-
// const cols = [_][]const u8 { "TotalSteps", "TotalDistance" };
93-
// comptime var fields: [cols.len]std.builtin.Type.StructField = undefined;
94-
// comptime var i: usize = 0;
95-
// inline for (cols) |col| {
96-
// std.debug.print("\n\n{s}", .{ col });
97-
// fields[i] = .{
98-
// .name = col,
99-
// .type = u8,
100-
// .default_value = "",
101-
// .is_comptime = false,
102-
// .alignment = @alignOf(u8),
103-
// };
104-
// i += 1;
105-
// }
106-
107-
// std.debug.print("\n{d}", .{ fields.len });
108-
// inline for (fields) |field| {
109-
// std.debug.print("\n{s}, {any}", .{ field.name, field.type });
110-
// }
111-
// const decls: [0]std.builtin.Type.Declaration = [_]std.builtin.Type.Declaration{};
112-
// const TestType = @Type(.{
113-
// .Struct = .{
114-
// .layout = .Auto,
115-
// .is_tuple = false,
116-
// .fields = &fields,
117-
// .decls = &decls,
118-
// },
119-
// });
120-
// const inst: TestType = TestType{ .TotalSteps = 3 };
121-
// std.debug.print("\n\n{any}\n{any}", .{ TestType, inst });
58+
const output1 = svg(g("Test"));
59+
// const output = svg(output1);
60+
std.debug.print("\n{s}\n{s}", .{ output1, "" });
12261
}

src/format.zig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const std = @import("std");
2+
3+
pub fn main() !void {
4+
var x: usize = 0;
5+
for (0..32) |i| {
6+
std.debug.print("\n{d}", .{ i });
7+
std.debug.print("\n{d:0>6}", .{ x });
8+
x += 1;
9+
}
10+
}

src/game-programming-patters/command.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ const Command = struct {
5959
};
6060

6161
pub fn main() !void {
62-
var arena = std.heap.ArenaAllocator.init(std.heap.c_allocator);
62+
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
6363
defer arena.deinit();
6464

6565
// Open tty

src/json.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub fn main() !void {
3434
// print("\nParsed Person:\n{s}: {?}", .{ parsed_person.name, parsed_person.age });
3535

3636
// ------ 0.12.0 ------
37-
var arena = std.heap.ArenaAllocator.init(std.heap.c_allocator);
37+
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
3838
defer arena.deinit();
3939
const allocator = arena.allocator();
4040

src/lib/functional.zig

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,52 @@
1-
// pub fn comp(comptime fns: anytype) *const @TypeOf(fns[0]) {
2-
// return struct {
3-
// pub fn f(args: T) T {
4-
// var tmp: T = args;
5-
// inline for (fns) |func| {
6-
// tmp = @call(.auto, func, .{ T, tmp });
7-
// }
8-
// return tmp;
9-
// }
10-
// }.f;
11-
// }
1+
const std = @import("std");
122

13-
pub fn partial(comptime fun: anytype, comptime arg0: anytype) *const fn (comptime arg: @TypeOf(arg0)) @TypeOf(arg0) {
3+
pub fn comp(comptime T: type, comptime fns: anytype) fn(arg: T) T {
144
return struct {
15-
pub fn f(comptime arg: @TypeOf(arg0)) @TypeOf(arg) {
16-
return fun(arg0, arg);
5+
pub fn f(arg: T) T {
6+
var tmp: T = arg;
7+
inline for (fns) |func| {
8+
tmp = @call(.auto, func, .{ tmp });
9+
}
10+
return tmp;
1711
}
1812
}.f;
1913
}
2014

21-
pub fn partialTyped(comptime T: type, comptime fun: anytype, comptime arg0: T) *const fn (comptime arg: @TypeOf(arg0)) T {
15+
pub fn partial(comptime T: type, func: anytype, comptime arg0: T) *const fn (arg: T) T {
2216
return struct {
23-
pub fn f(comptime arg: @TypeOf(arg0)) @TypeOf(arg) {
24-
return fun(arg0, arg);
17+
pub fn f(arg: T) T {
18+
return @call(.auto, func, .{ T, arg0, arg });
2519
}
2620
}.f;
2721
}
2822

23+
pub fn partial2(comptime T: type, func: anytype, comptime arg0: anytype, comptime arg1: anytype) *const fn (arg: T) T {
24+
return struct {
25+
pub fn f(arg: T) T {
26+
return @call(.auto, func, .{ T, arg0, arg1, arg });
27+
}
28+
}.f;
29+
}
2930

30-
pub fn partial2Typed(comptime T: type, comptime fun: anytype, comptime arg0: T, comptime arg1: T) *const fn (comptime r0: @TypeOf(arg0)) @TypeOf(arg0) {
31+
pub fn partialGeneric(comptime T: type, func: anytype, comptime args: anytype) *const fn (arg: T) T {
32+
const field_info = @typeInfo(@TypeOf(args)).Struct.fields;
3133
return struct {
32-
pub fn f(comptime arg: @TypeOf(arg0)) @TypeOf(arg) {
33-
return fun(arg0, arg1, arg);
34+
pub fn f(arg: T) T {
35+
// @compileLog(());
36+
switch(field_info.len) {
37+
1 => return @call(.auto, func, .{ T, args[0], arg }),
38+
2 => return @call(.auto, func, .{ T, args[0], args[1], arg }),
39+
3 => return @call(.auto, func, .{ T, args[0], args[1], args[2], arg }),
40+
else => return @call(.auto, func, .{ T }),
41+
}
3442
}
3543
}.f;
3644
}
45+
46+
// pub fn partial2Typed(comptime T: type, comptime fun: anytype, comptime arg0: T, comptime arg1: T) *const fn (comptime r0: @TypeOf(arg0)) @TypeOf(arg0) {
47+
// return struct {
48+
// pub fn f(comptime arg: @TypeOf(arg0)) @TypeOf(arg) {
49+
// return fun(arg0, arg1, arg);
50+
// }
51+
// }.f;
52+
// }

src/meta.zig

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,23 @@
11
const std = @import("std");
2+
const fun = @import("./lib/functional.zig");
3+
4+
fn add(comptime arg0: anytype) @TypeOf(arg0) {
5+
return arg0 + 1;
6+
}
7+
8+
fn generic(comptime func: anytype, comptime args: anytype) void {
9+
const FuncType = @TypeOf(func);
10+
const args_info = @typeInfo(FuncType).Fn.params;
11+
inline for(0..args_info.len) |i| {
12+
std.debug.print("\n{any}", .{ args_info[i] });
13+
}
14+
15+
const ArgsType = @TypeOf(args);
16+
const fields_info = @typeInfo(ArgsType).Struct.fields;
17+
inline for(0..fields_info.len) |i| {
18+
std.debug.print("\n{any}: {any}", .{ fields_info[i], args[i] });
19+
}
20+
}
221

322
const DataStruct = struct {
423
seed: u16 = undefined,
@@ -40,4 +59,6 @@ const DataStruct = struct {
4059
pub fn main() !void {
4160
const ds = DataStruct.initRandom();
4261
std.debug.print("\n{any}", .{ ds });
62+
63+
generic(add, .{ 2, 4 });
4364
}

src/pointers.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const Node = struct {
1717

1818

1919
pub fn main() !void {
20-
var arena = std.heap.ArenaAllocator.init(std.heap.c_allocator);
20+
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
2121
defer arena.deinit();
2222
const allocator = arena.allocator();
2323

0 commit comments

Comments
 (0)