Skip to content

Instantly share code, notes, and snippets.

@nrdmn
Last active September 16, 2019 22:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nrdmn/ff5c7ede3d8de011cce8f7c0a926faa8 to your computer and use it in GitHub Desktop.
Save nrdmn/ff5c7ede3d8de011cce8f7c0a926faa8 to your computer and use it in GitHub Desktop.
Pointer Arithmetic
const std = @import("std");
fn Add(comptime A: type, comptime B: type) type {
if (A == type) {
return B;
} else {
return Add(@typeInfo(A).Pointer.child, *B);
}
}
fn Fib(comptime N: type) type {
return switch (N) {
type => type,
*type => *type,
else => Add(Fib(@typeInfo(N).Pointer.child), Fib(@typeInfo(@typeInfo(N).Pointer.child).Pointer.child)),
};
}
fn Seq(comptime N: type) [toInt(N)]type {
if (N == type) {
return [_]type{};
} else {
return Seq(@typeInfo(N).Pointer.child) ++ [_]type{N};
}
}
fn toInt(comptime N: type) comptime_int {
if (N == type) {
return 0;
} else {
return 1 + toInt(@typeInfo(N).Pointer.child);
}
}
pub fn main() !void {
var buf: [4]u8 = undefined;
const stdout = try std.io.getStdOut();
inline for (Seq(*************type)) |N| {
try stdout.write(try std.fmt.bufPrint(buf[0..], "{}\n", u8(toInt(Fib(N)))));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment