Skip to content

Instantly share code, notes, and snippets.

View abradley2's full-sized avatar
🌳
https://elm-lang.org/

Tony Bradley abradley2

🌳
https://elm-lang.org/
  • Washington, DC
View GitHub Profile
@abradley2
abradley2 / component.zig
Created April 21, 2025 11:07
Zig ECS Archetypes
const std = @import("std");
const Vector2 = @import("raylib").Vector2;
pub const Velocity = Component(0x00000001, "velocity", Vector2);
pub const Position = Component(0x00000002, "position", struct {
x_y: Vector2,
z: f32,
});
@abradley2
abradley2 / zig_writer_example.zig
Created April 6, 2025 14:47
Zig Writer Example
pub const Foo: type = struct {
alloc: std.mem.Allocator,
buff: std.ArrayListUnmanaged(u8),
pub fn init(alloc: std.mem.Allocator) Foo {
return Foo{
.alloc = alloc,
.buff = std.ArrayListUnmanaged(u8){},
};
}
@abradley2
abradley2 / ValidationExample.elm
Last active February 21, 2025 03:58
Elm Applicative Validation Example
module Main exposing (..)
import List.Nonempty exposing (..)
import State exposing (State(..))
type alias Error =
String
@abradley2
abradley2 / README.md
Last active February 17, 2025 13:08
Tony's Zig Patterns

Use std.BoundedArray alot

It is often worth it to use way more memory than you need to if that memory can remain on the stack. Hard to leak memory that you never allocate. Use std.BoundedArray and it's assumeCapacity methods alot. Operating systems have a max file path. If I'm able to put that many bytes on the stack I should never have to return OutOfMemory, unless it's an invalid value altogether

var source = std.BoundedArray(u8, std.fs.MAX_PATH_BYTES).init(0) catch unreachable;
try switch (source_json) {
@abradley2
abradley2 / main.zig
Last active February 4, 2025 03:42
You don't need interfaces in Zig
const std = @import("std");
fn Writer(comptime T: anytype) type {
return struct { Write: fn (t: *T, b: []const u8) anyerror!usize };
}
fn writeHello(comptime T: anytype, comptime writer: Writer(T), t: *T) anyerror!usize {
return writer.Write(t, "Hello there");
}
@abradley2
abradley2 / ECS.hs
Last active January 12, 2025 05:00
Simple ECS in Haskell
{-# LANGUAGE NoImplicitPrelude #-}
module ECS where
import RIO
import RIO.Set qualified as Set
import RIO.Vector qualified as Vector
import RIO.Writer (Writer)
import RIO.Writer qualified as Writer
@abradley2
abradley2 / aseprite.ts
Created December 30, 2024 16:37
AsepriteLoader
import { TaskEither } from "fp-ts/lib/TaskEither"
import { Assets, Spritesheet, SpritesheetData, SpritesheetFrameData } from "pixi.js"
import * as io from "io-ts"
import { HandledError } from "./errors"
import * as taskEither from "fp-ts/lib/TaskEither"
import { pipe } from "fp-ts/lib/function"
import * as errors from "./errors"
const frameDecoder = io.type({
@abradley2
abradley2 / main.rs
Created November 16, 2024 16:05
Integrating Tokio with synchronous code
use std::{thread, time::Duration};
use tokio::{runtime::Builder, sync::mpsc};
fn main() {
let start_instant = std::time::Instant::now();
let runtime = Builder::new_multi_thread()
.worker_threads(3)
.enable_all()
.build()
@abradley2
abradley2 / main.rs
Created November 15, 2024 04:30
Tokio Drift
use tokio::sync::mpsc;
#[tokio::main]
async fn main() {
let result = run();
assert_eq!(result, 2);
}
fn run() -> u32 {
let (tx, mut rx) = mpsc::channel(32);
@abradley2
abradley2 / main.rs
Created November 15, 2024 04:08
Regular Rust Async Example
use std::{sync::{mpsc::channel, Arc, Mutex}, thread, time::Duration};
fn main() {
let result = run();
assert_eq!(result, 3)
}
struct Counter {
value: u32,
}