Last active
March 26, 2020 02:37
-
-
Save andrewrk/c5a84c3752805b39b5d36ddab39d31d7 to your computer and use it in GitHub Desktop.
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"); | |
const net = std.net; | |
const fs = std.fs; | |
const os = std.os; | |
pub const io_mode = .evented; | |
pub fn main() anyerror!void { | |
const allocator = std.heap.page_allocator; // TODO use a more appropriate allocator | |
var server = net.StreamServer.init(.{}); | |
defer server.deinit(); | |
var room = Room{ .clients = std.AutoHashMap(*Client, void).init(allocator) }; | |
try server.listen(net.Address.parseIp("127.0.0.1", 0) catch unreachable); | |
std.debug.warn("listening at {}\n", .{server.listen_address}); | |
while (true) { | |
const client = try allocator.create(Client); | |
client.* = Client{ | |
.conn = try server.accept(), | |
.handle_frame = async client.handle(&room), | |
}; | |
try room.clients.putNoClobber(client, {}); | |
} | |
} | |
const Client = struct { | |
conn: net.StreamServer.Connection, | |
handle_frame: @Frame(handle), | |
fn handle(self: *Client, room: *Room) !void { | |
try self.conn.file.writeAll("server: welcome to teh chat server\n"); | |
while (true) { | |
var buf: [100]u8 = undefined; | |
const amt = try self.conn.file.read(&buf); | |
const msg = buf[0..amt]; | |
room.broadcast(msg, self); | |
} | |
} | |
}; | |
const Room = struct { | |
clients: std.AutoHashMap(*Client, void), | |
fn broadcast(room: *Room, msg: []const u8, sender: *Client) void { | |
var it = room.clients.iterator(); | |
while (it.next()) |entry| { | |
const client = entry.key; | |
if (client == sender) continue; | |
client.conn.file.writeAll(msg) catch |e| std.debug.warn("unable to send: {}\n", .{e}); | |
} | |
} | |
}; |
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"); | |
pub fn main() anyerror!void { | |
const program = "+ + * 💩\xff - /"; | |
var accumulator: usize = 0; | |
var it = std.unicode.Utf8View.initComptime(program).iterator(); | |
while (it.nextCodepoint()) |token| switch (token) { | |
'+' => accumulator += 1, | |
'-' => accumulator -= 1, | |
'*' => accumulator *= 2, | |
'/' => accumulator /= 2, | |
'💩' => accumulator *= accumulator, | |
else => continue, // Ignore everything else | |
}; | |
std.debug.warn("The program \"{}\" calculates the value {}\n", program, accumulator); | |
} |
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"); | |
pub fn main() anyerror!void { | |
const fns = struct { | |
const x = 0; | |
const y = 1; | |
const z = 2; | |
fn cross_2d(a: var, b: @typeOf(a)) @typeInfo(@typeOf(a)).Array.child { | |
return a[x] * b[y] - a[y] * b[x]; | |
} | |
fn cross_3d(a: var, b: @typeOf(a)) @typeOf(a) { | |
return .{ | |
a[y] * b[z] - a[z] * b[y], | |
a[z] * b[x] - a[x] * b[z], | |
a[x] * b[y] - a[y] * b[x], | |
}; | |
} | |
}; | |
const a = [2]i32{ 1, 2 }; | |
const b = [2]i32{ 5, -3 }; | |
std.debug.warn("{}\n", fns.cross_2d(a, b)); | |
const x = [3]f32{ 1, 4, 9 }; | |
const y = [3]f32{ -5, 0, 3 }; | |
std.debug.warn("{}\n", fns.cross_3d(x, y)); | |
// Failure case | |
//const i = [2]bool{true, false}; | |
//const j = [2]bool{false, true}; | |
//std.debug.warn("{}", fns.cross_2d(i, j)); | |
// Use u1 if you want numerical bools | |
const i = [2]u1{ 1, 0 }; | |
const j = [2]u1{ 0, 1 }; | |
std.debug.warn("{}\n", fns.cross_2d(i, j)); | |
const cross = struct { | |
fn CrossReturnType(comptime T: type) type { | |
const array_info = @typeInfo(T).Array; | |
return switch (array_info.len) { | |
2 => array_info.child, | |
3 => T, | |
else => @compileError("expected [2]T or [3]T"), | |
}; | |
} | |
fn cross(arg0: var, arg1: var) CrossReturnType(@typeOf(arg0)) { | |
return switch (@typeInfo(@typeOf(arg0)).Array.len) { | |
2 => fns.cross_2d(arg0, arg1), | |
3 => fns.cross_3d(arg0, arg1), | |
else => @compileError("unreachable"), | |
}; | |
} | |
}.cross; | |
std.debug.warn("{}\n", cross(a, b)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment