Last active
September 16, 2019 22:34
-
-
Save nrdmn/ff5c7ede3d8de011cce8f7c0a926faa8 to your computer and use it in GitHub Desktop.
Pointer Arithmetic
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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