Video: Programming without pointers

2 Likes

Unreleased! It’s nearly ready for prime time but not quite!

I didn’t intend that repo to be public just yet. I don’t mind that you found it, that would be weird in several ways, and I’m not going to set it private because it’s quite close to done.

All I’m saying is for now, y’all are on your own, please don’t file issues or ask questions. Some things might move around which I would otherwise be reluctant to change, or even disappear. That kind of thing.

Hi, does anyone have the code from the presentation? if not I could type it and send it here

Edit: here is example1-original

const State = struct {
    clowns: StringHashMap(Clown) = .empty,

    const Clown = struct {
        scariness: f32,
        funniness: f32,
    };

    fn deinit(state: *State, gpa: Allocator) void {
        var it = state.clowns.iterator();
        while (it.next()) |entry| {
            gpa.free(entry.key_ptr.*);
        }
        state.clowns.deinit(gpa);
    }
};

test {
    const gpa = std.testing.allocator;

    var state: State = .{};
    defer state.deinit(gpa);

    {
        const dimples = try gpa.dupe(u8, "Dimples");
        errdefer gpa.free(dimples);
        try state.clowns.put(gpa, dimples, .{
            .scariness = 0.50,
            .funniness = 0.10,
        });
    }
    {
        const humpty = try gpa.dupe(u8, "Humpty");
        errdefer gpa.free(humpty);
        try state.clowns.put(gpa, humpty, .{
            .scariness = 0.99,
            .funniness = 0.99,
        });
    }
}

const std = @import("std");
const mem = std.mem;
const assert = std.debug.assert;
const ArrayList = std.ArrayListUnmananged;
const StringHashMap = std.StringHashMapUnmanaged;
const Allocator = std.mem.Allocator;
const HashMap = std.HashMapUnamanged;
const max_load_percent = std.hash_map.default_max_load_percentage;

example1-pwd

const State = struct {
    clowns: AutoHashMap(String, Clown) = .empty,

    string_bytes: ArrayList(u8) = .empty,
    string_table: String.Table  = .empty,

    const Clown = struct {
        scariness: f32,
        funniness: f32,
    };

    fn deinit(state: *State, gpa: Allocator) void {
        state.string_bytes.deinit(gpa);
        state.string_table.deinit(gpa);
        state.clowns.deinit(gpa);
    }

    pub const String = enum(u32) {
        _,

        const Table = HashMap(String, void, TableContext, max_load_percent);

        const TableContext = struct {
            bytes: []const u8,

            pub fn eql(_: @This(), a: String, b: String) bool {
                return a == b;
            }

            pub fn hash(ctx: @This(), key: String) u64 {
                return std.hash_map.hashString(mem.sliceTo(ctx.bytes[@intFromEnum(key)..], 0));
            }
        };

        const TableIndexAdapter = struct {
            bytes: []const u8,

            pub fn eql(ctx: @This(), a: []const u8, b: String) bool {
                return mem.eql(u8, a, mem.sliceTo(ctx.bytes[@intFromEnum(b)..],0));
            }

            pub fn hash(_: @This(), adapted_key: []const u8) u64 {
                assert(mem.indexOfScalar(u8, adapted_key, 0) == null);
                return std.hash_map.hashString(adapted_key);
            }
        };

        pub fn slice(index: String, state: *const State) [:0]const u8 {
            const start_slice = state.stirng_bytes.items[@intFromEnum(index)..];
            return start_slice[0..mem.indexOfScalar(u8, start_slice, 0).? :0];
        }
    };

    pub fn internString(state: *State, gpa: Allocator, bytes: []const u8) !String {
        const gop = try state.string_table.getOrPutContextAdapted(
            gpa,
            @as([]const u8, bytes),
            @as(String.TableIndexAdapter, .{ .bytes = state.string_bytes.items }),
            @as(String.TableContext, .{ .bytes = state.string_bytes.items}),
        );
        if (gop.found_existing) return gop.key_ptr.*;

        try state.string_bytes.ensureUnusedCapacity(gpa, bytes.len + 1);
        const new_off: String = @enumFromInt(state.string_bytes.items.len);

        state.string_bytes.appendSliceAssumeCapacity(bytes);
        state.string_bytes.appendAssumeCapacity(0);

        gop.key_ptr.* = new_off;

        return new_off;
    }
};

test {
    const gpa = std.testing.allocator;

    var state: State = .{};
    defer state.deinit(gpa);

    try state.clowns.put(gpa, try state.internString(gpa, "Dimples"), .{
        .scariness = 0.50,
        .funniness = 0.10,
    });

    try state.clowns.put(gpa, try state.internString(gpa, "Humpty"), .{
        .scariness = 0.99,
        .funniness = 0.10,
    });
}

const std = @import("std");
const mem = std.mem;
const assert = std.debug.assert;
const ArrayList = std.ArrayListUnmanaged;
const StringHashMap = std.StringHashMapUnmanaged;
const Allocator = std.mem.Allocator;
const HashMap = std.HashMapUnmanaged;
const AutoHashMap = std.AutoHashMapUnmanaged;
const max_load_percent = std.hash_map.default_max_load_percentage;

example2-original

const State = struct {
    diseases: ArrayList(Disease) = .empty,

    const Disease = struct {
        occurrences: ArrayList(Location),
    };

    const Location = struct {
        lattitude: f32,
        longitude: f32,
    };

    fn deinit(state: *State, gpa: Allocator) void {
        for (state.diseases.items) |*disease| {
            disease.occurrences.deinit(gpa);
        }
        state.diseases.deinit(gpa);
    }
};

test {
    const gpa = std.testing.allocator;

    var state: State = .{};
    defer state.deinit(gpa);

    {
        const disease = try state.diseases.addOne(gpa);
        disease.* = .{ .occurrences = .empty };
        try disease.occurrences.appendSlice(gpa, &.{
            .{ .lattitude = 31.77635, .longitude = 35.23559 },
            .{ .lattitude = 30.04443, .longitude = 31.23575 },
        });
    }
    {
        const disease = try state.diseases.addOne(gpa);
        disease.* = .{ .occurrences = .empty };
        try disease.occurrences.appendSlice(gpa, &.{
            .{ .lattitude = 31.50504, .longitude = 34.46107 },
        });
    }
}


const std = @import("std");
const mem = std.mem;
const assert = std.debug.assert;
const ArrayList = std.ArrayListUnmanaged;
const StringHashMap = std.StringHashMapUnmanaged;
const Allocator = std.mem.Allocator;
const HashMap = std.HashMapUnmanaged;
const AutoHashMap = std.AutoHashMapUnmanaged;
const max_load_percent = std.hash_map.default_max_load_percentage;

and example2-pwd

const State = struct {
    diseases: ArrayList(Disease) = .empty,
    locations: ArrayList(Location) = .empty,

    const Disease = struct {
        occurrences: Location.Slice,
    };

    const Location = struct {
        lattitude: f32,
        longitude: f32,

        const Slice = struct {
            off: u32,
            len: u32,
        };
    };

    fn deinit(state: *State, gpa: Allocator) void {
        state.locations.deinit(gpa);
        state.diseases.deinit(gpa);
    }

    fn addLocations(state: *State, gpa: Allocator, locs: []const Location) !Location.Slice {
        try state.locations.appendSlice(gpa, locs);
        return .{
            .off = @intCast(state.locations.items.len - locs.len),
            .len = @intCast(locs.len),
        };
    }
};

test {
    const gpa = std.testing.allocator;

    var state: State = .{};
    defer state.deinit(gpa);

    try state.diseases.append(gpa, .{
        .occurrences = try state.addLocations(gpa, &.{
            .{ .lattitude = 31.77635, .longitude = 35.23559 },
            .{ .lattitude = 30.04443, .longitude = 31.23575 },
        }),
    });

    try state.diseases.append(gpa, .{
        .occurrences = try state.addLocations(gpa, &.{
            .{ .lattitude = 31.50504, .longitude = 34.46107 },
        }),
    });
}

const std = @import("std");
const mem = std.mem;
const assert = std.debug.assert;
const ArrayList = std.ArrayListUnmanaged;
const StringHashMap = std.StringHashMapUnmanaged;
const Allocator = std.mem.Allocator;
const HashMap = std.HashMapUnmanaged;
const AutoHashMap = std.AutoHashMapUnmanaged;
const max_load_percent = std.hash_map.default_max_load_percentage;

hope it helps someone else

2 Likes

So true. God that takes me back.

Never noticed the ā€œadaptedā€ api on hashmaps! Super powerful

I hope you didn’t type this out by hand. You know AI can do this super easily? You can send screenshots to an AI and it will type out the code for you.

Absolutely do NOT use LLMs to write Zig code. They all stink at it.

Edit: my bad for not reading the context. Absolutely DO use AI to extract text (and code) from screenshots and videos.

1 Like

I believe they were referring to using an LLM to extract the code displayed in a video and put it into text form. Just basic text-recognition stuff, doesn’t have to be trained on Zig or even a code LLM.

Essentially just a screenshot of text and let an LLM transcript it to actual text.

2 Likes

i think they are saying to use ai to extract text from an image, which is usually fairly accurate

1 Like

I don’t see the problem with typing it out by hand, it’s a good way to start internalize the patterns.

2 Likes