I've code some code for sorting working with the Context structs and the lessThan functions declared in full. But I want to gain a better understanding of which, if any, I can use literals or anonymous structs (or functions) instead?
// two context structs
const SortContext1 = struct {};
const SortContext2 = struct {
hm: *const std.StringHashMap(u32),
};
// a lessThan function for each
fn lessThanFn1(context: SortContext1, lhs: []const u8, rhs: []const u8) bool {
return std.mem.order(u8, lhs, rhs) == .lt;
}
fn lessThanFn2(context: SortContext2, lhs: []const u8, rhs: []const u8) bool {
return context.hm.get(lhs).? > context.hm.get(rhs).?;
}
// examples of using each with `sort()`
std.mem.sort([]const u8, keez.items, SortContext1{}, lessThanFn1);
std.mem.sort([]const u8, keez.items, SortContext2{ .hm = stats }, lessThanFn2);
This is to help my understanding and not for best practices, so I want to know if both context structs and both lessThan functions need to be fully declared separately like this, or can I combine one or more of them into other lines as an alternative? (Even if ugly, even if there's some duplication. I just want to grok more fully)