Skip to content

Instantly share code, notes, and snippets.

View softprops's full-sized avatar
®️
Rustling

Doug Tangren softprops

®️
Rustling
View GitHub Profile
@softprops
softprops / main.zig
Created October 20, 2024 22:17
zig server
const std = @import("std");
pub fn main() !void {
try serve(8008, handle);
}
fn serve(port: u16, handler: fn (*std.http.Server.Request) void) !void {
var addr = try std.net.Address.parseIp("127.0.0.1", port);
var svr = try addr.listen(.{});
defer svr.deinit();
@softprops
softprops / if.zig
Last active July 30, 2024 20:43
what multi object if captures could look like if they worked the same way as multi capture for expressions
const std = @import("std");
pub fn main() void {
const maybeA: ?u8 = 1;
const maybeB: ?u8 = 2;
const maybeC: ?u8 = 3;
if (maybeA, maybeB, maybeC) |a, b, c| {
std.debug.print("{d} {d} {d}\n", .{ a, b, c});
}
}
const std = @import("std");
const meta = std.meta;
const Example = struct {
foo: []const u8,
bar: i32,
const docs = std.EnumArray(
meta.FieldEnum(Example),
// 👇 type of value of metadata checked at comptime, can be any type declared
@softprops
softprops / appendmetadata.zig
Created May 12, 2024 00:14
in duckdb v0.10.* extensions now require a metadata payload of bytes appended to their binary. if you aren't building with cmake here's a zig program that will do the equiv of https://github.com/duckdb/duckdb/blob/main/scripts/append_metadata.cmake
const std = @import("std");
pub fn main() !void {
var args = std.process.args();
_ = args.next();
const path = args.next() orelse {
bail("expected path argument. this is the path to the file we're appending metadata to");
};
const platform = args.next() orelse {
bail("expected duckdb platform argument. see https://duckdb.org/docs/extensions/working_with_extensions#platforms");
// remix of https://discordapp.com/channels/605571803288698900/1219282855679496243/1219706356718632981
const std = @import("std");
const Timestamp = struct {
seconds: i64,
pub fn format(
self: @This(),
comptime _: []const u8,
_: std.fmt.FormatOptions,
@softprops
softprops / coderpad.go
Last active March 7, 2024 19:03
coder pad go template
// To execute Go code, please declare a func main() in a package "main"
package main
import (
"log"
"reflect"
"time"
)
@softprops
softprops / CoderPad.java
Last active March 7, 2024 19:03
coder pad java template
import java.io.*;
import java.util.*;
import static org.junit.Assert.*;
// fmt: shift + cmd + f
// imports : shift + cmd + o
class Solution {
// impl
// 👇
[email protected]
exports java.io
exports java.lang
exports java.lang.annotation
exports java.lang.invoke
exports java.lang.module
exports java.lang.ref
exports java.lang.reflect
exports java.math
exports java.net
@softprops
softprops / NonRepeatingStream.java
Created June 16, 2021 18:50
NonRepeatingStream.java
import java.util.stream.*;
import java.util.concurrent.*;
import java.util.*;
public <T> Stream<T> nonRepeatingStream(List<T> src) {
IntSupplier index = () -> ThreadLocalRandom.current().nextInt(0, src.size());
return Stream.iterate(
index.getAsInt(),
(prev) -> {
int next = index.getAsInt();
use rand; // 0.8.3
use rand::Rng;
use std::iter::successors;
fn non_repeating<T: std::cmp::PartialEq + Clone>(src: &[T]) -> impl Iterator<Item = &T> + '_ {
let mut rng = rand::thread_rng();
successors(Some(&src[rng.gen_range(0..src.len())]), move |n| {
let mut next = &src[rng.gen_range(0..src.len())];
while *next == **n {
next = &src[rng.gen_range(0..src.len())];