Skip to content

Instantly share code, notes, and snippets.

@lukebayes
lukebayes / pointers.zig
Created June 18, 2021 15:36
Example code to help me understand when Zig works with pointers vs copies of structs, especially with Arrays and slices
const std = @import("std");
const expectEqual = std.testing.expectEqual;
const print = std.debug.print;
///////////////////////////////////////////////////////////////////////////////
// Examples for working with Structs and Arrays of Structs
//
// This is intended to help me understand when Zig works with an entity as a
// pointer, and when it makes a copy.
@lukebayes
lukebayes / set_get_tuples.zig
Last active June 17, 2021 16:00
How to work with heterogenous Tuple types in Zig?
const std = @import("std");
const print = std.debug.print;
// The dreaded, "unable to evaluate constant expression"
//
// I'm having a difficult time understanding when the zig compiler transitions
// my code execution from comptime to runtime and I'm pretty sure this is the
// root cause of my struggles here.
//
@lukebayes
lukebayes / when-changed.py
Created June 15, 2021 14:05
when-changed.py (with extra whitespace, de-duped files list and message for what file caused build)
#!/usr/bin/env python
"""
when-changed - run a command when a file is changed
Usage: when-changed FILE COMMAND...
when-changed FILE [FILE ...] -c COMMAND
Copyright (c) 2011, Johannes H. Jensen.
License: BSD, see LICENSE for more details.
"""
@lukebayes
lukebayes / poly.zig
Last active May 30, 2021 01:11
Zig Polymorphism / Delegation
const std = @import("std");
const print = std.debug.print;
pub fn Abstract(comptime T: type, comptime R: type) type {
return struct {
const Self = @This();
impl: *T = T{},
#! /usr/bin/env bash
set -eo pipefail
##########################################
# Everytime I need to remove a git submdoule, I wind up on stackoverflow trying to find the incantation.
# It seems the latest versions have made it much less painful but anyhow, here's a script that I drop into
# my ~/bin folder, chmod to 755 and use for easy, safe-ish git submodule removal.
##########################################
RED='\033[0;31m'
###########################################################
# Build script
###########################################################
PROJECTNAME=aspen
ROOTDIR=$(shell git rev-parse --show-toplevel)
# make dev-install ARCH=armv7l to get the right version of nodejs
# Ubuntu desktop is "x86_64", Raspberry Pi is "armv7l", Nodejs
# requires "x86_64" to be "x64"
package main
import (
"fmt"
)
type Displayable interface {
AddChild(c Displayable)
ChildAt(index int) Displayable
SetParent(p Displayable)
@lukebayes
lukebayes / demo.rkt
Created November 8, 2016 06:59
Demo of a Racket server that does not work...
#lang racket
(require web-server/http
web-server/servlet
web-server/servlet-env)
;; Sign in GET handler
(define (sign-in-get req)
(response/xexpr
`(html (head (title "GET"))
@lukebayes
lukebayes / find-replace.sh
Created March 10, 2013 23:18
Simple Bash/Posix find and replace shell script. This script is dangerous and will perform a recursive in-place modification of every file forward of the provided directory. Binary files, hidden files, etc. Use with caution.
#!/bin/bash
die () {
echo >&2 "$@"
exit 1
}
[ "$#" -eq 3 ] || die "3 arguments required ('find', 'replace', dir), $# provided"
MATCH=$1
@lukebayes
lukebayes / each_file_matching.js
Created February 7, 2011 06:20
NodeJS (or CommonJS?) code to recursively traverse directories triggering the provided callback each time a regex matches a file name.
var fs = require("fs");
/**
* Call fileHandler with the file name and file Stat for each file found inside
* of the provided directory.
*
* Call the optionally provided completeHandler with an array of files (mingled
* with directories) and an array of Stat objects (one for each of the found
* files.
*