Skip to content

Instantly share code, notes, and snippets.

@travisstaloch
travisstaloch / summarize.js
Created March 12, 2018 06:16
For use with zenbot's genetic backtester: darwin. Displays the top N results from sim files for a given generation. Results sorted by --prop, grouped by strategy and in total.
// Used with https://github.com/DeviaVir/zenbot
// I store this file at zenbot\scripts\genetic_backtester\summarize.js
// Example usage:
// node scripts\genetic_backtester\summarize.js --population_data simulations/popDataFolder --count 5 --prop endBalance --gen 1
const fs = require('fs'),
KEY_POP_DATA = 'population_data',
KEY_BT_GEN = 'backtester_generation',
KEY_STRAT = 'strategy',
KEY_FITNESS = 'fitness',
@travisstaloch
travisstaloch / peg.rs
Last active November 14, 2018 17:37 — forked from eyelash/peg.rs
#![feature(ptr_offset_from)]
type ParseResult<'a> = Option<&'a str>;
trait Peg<'a> {
fn p(&self, s: &mut &'a str) -> ParseResult<'a>;
}
struct Wrap<T>(T);
@travisstaloch
travisstaloch / tagged_union_test.zig
Last active September 11, 2022 16:15
Attempt to create tagged union instance from runtime known tag.
const std = @import("std");
const Tag = enum {
A,
B,
C,
};
const Tagged = union(Tag) {
A: bool,
B: i32,
@travisstaloch
travisstaloch / fields.zig
Last active May 12, 2024 23:19
zig iterate over struct fields and print
const std = @import("std");
test "fields" {
const U1s = packed struct {
a: u1,
b: u1,
c: u1,
};
const x = U1s{ .a = 1, .b = 0, .c = 0 };
@travisstaloch
travisstaloch / view_asm.zig
Created December 14, 2019 03:39
stripped down template for viewing assembly produced by zig
// zig build-obj --emit asm --strip --release-small view_asm.zig && cat view_asm.s
const std = @import("std");
pub fn panic(msg: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
while (true) {}
}
export nakedcc fn _start() noreturn {
// code here will appear in _start section
@travisstaloch
travisstaloch / nbody.zig
Last active January 2, 2020 00:27
benchmarksgame nbody problem in zig
const std = @import("std");
const pi: f64 = 3.141592653589793;
const solar_mass: f64 = 4.0 * pi * pi;
const year: f64 = 365.24;
const n_bodies: usize = 5;
const Body = struct {
x: @Vector(3, f64),
v: @Vector(3, f64),
// $ zig build-exe --release-fast spectralnorm.zig && time ./spectralnorm 5500
const std = @import("std");
const warn = std.debug.warn;
const V2f64 = @Vector(2, f64);
inline fn A(_i: usize, _j: usize) f64 {
var i: f64 = @intToFloat(f64, _i);
// The Computer Language Benchmarks Game
// https://salsa.debian.org/benchmarksgame-team/benchmarksgame/
//
// Contributed by Mark C. Lewis.
// Modified slightly by Chad Whipkey.
// Converted from Java to C++ and added SSE support by Branimir Maksimovic.
// Converted from C++ to C by Alexey Medvedchikov.
// Modified by Jeremy Zerfas.
// Converted to zig by Travis Staloch
const std = @import("std");
pub fn RingBuffer(comptime T: type) type {
return struct {
data: []T,
current: [*]T,
count: usize,
dtor: ?fn (T) void,
const Self = @This();
@travisstaloch
travisstaloch / parse_psf.zig
Last active January 25, 2020 21:01
parse psf font file, getting the header at comptime
const std = @import("std");
const warn = std.debug.warn;
test "parse psf font file" {
// https://github.com/monarrk/trOS/raw/master/src/vga/font.psf
// const font_raw = @embedFile("font.psf");
// https://github.com/powerline/fonts/blob/master/Terminus/PSF/ter-powerline-v16b.psf.gz
const font_raw = @embedFile("ter-powerline-v16b.psf");
const PSFHeader = extern struct {