Skip to content

Instantly share code, notes, and snippets.

View trvswgnr's full-sized avatar
:octocat:
hardly workin'

Travis A. Wagner trvswgnr

:octocat:
hardly workin'
View GitHub Profile
@trvswgnr
trvswgnr / djb2.c
Last active July 24, 2024 04:00
various hash algorithms in c
#include <stdint.h>
uint32_t hash_djb2(unsigned char *str) {
uint32_t hash = 5381;
uint32_t c;
while ((c = *str++))
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
return hash;
@trvswgnr
trvswgnr / fetchJson.test.ts
Last active July 24, 2024 11:18
nice lil typescript fetch wrapper with errors as values
import { describe, it, expect, spyOn } from "bun:test";
import { fetchJson } from "./fetchJson";
class MockResponse {
static instanceCount = 0;
constructor(
public readonly ok: boolean,
private jsonSuccess: boolean | "bad parse",
) {
MockResponse.instanceCount++;
@trvswgnr
trvswgnr / fileContains.test.ts
Last active July 25, 2024 23:18
check if a file contains a specified phrase, without loading the entire file into memory
import { describe, it, expect, beforeAll, afterAll } from "bun:test";
import { fileContains, BoyerMooreSearcher } from "./fileContains";
import fs from "fs";
import path from "path";
describe("fileContains", () => {
const testDir = path.join(__dirname, "test-files");
beforeAll(() => {
if (!fs.existsSync(testDir)) {
@trvswgnr
trvswgnr / tramp.ts
Last active July 11, 2024 18:59
trampoline typescript
/**
* Transforms a function that returns either a direct value or a thunk (a
* no-argument function that returns a value) into a function that only returns
* a direct value. It does this by repeatedly evaluating the function if it
* returns a thunk, until a direct value is obtained.
*
* @template T The type of the value to be returned by the trampoline function.
* @template A The type tuple representing the argument types accepted by the
* function f.
* @param f A function that takes arguments of type A and returns either a
@trvswgnr
trvswgnr / Timeout.test.ts
Created June 27, 2024 20:39
custom setTimeout implementation in TypeScript
import { describe, it, expect, mock } from "bun:test";
import { Timeout } from "./shared";
describe("Timeout", () => {
it("should call the callback after a specific time", async () => {
expect.hasAssertions();
return await new Promise<void>((resolve) => {
const start = Date.now();
Timeout.set(() => {
expect(Date.now() - start).toBeGreaterThanOrEqual(100);
@trvswgnr
trvswgnr / map.ts
Last active June 26, 2024 11:45
custom ts map with eq and cmp
export enum Ordering {
Less = -1,
Equal = 0,
Greater = 1,
}
export interface Eq<T> {
eq(other: T): boolean;
}
@trvswgnr
trvswgnr / partial-application.ts
Last active June 19, 2024 22:20
partial application in typescript
export default function createPartialApplication<T extends any[], R>(
fn: (...args: T) => R,
): PartialApplication<T, R> {
return function accumulator(...args1: any[]): any {
return args1.length >= fn.length
? fn(...(args1 as T))
: (...args2: any[]) => accumulator(...args1.concat(args2));
};
}
@trvswgnr
trvswgnr / lc.rs
Last active June 19, 2024 01:16
λ-calc interpreter cramb
use std::collections::HashSet;
use std::iter::Peekable;
use std::str::Chars;
#[derive(Debug, PartialEq, Clone)]
enum Token {
Lambda,
Dot,
Variable(String),
LParen,
@trvswgnr
trvswgnr / lc.rs
Last active June 5, 2024 16:16
λ-calc interpreter in crablang
use std::collections::HashSet;
use std::iter::Peekable;
use std::str::Chars;
#[derive(Debug, PartialEq, Clone)]
enum Token {
Lambda,
Dot,
Variable(String),
LParen,
@trvswgnr
trvswgnr / sub-add.c
Created April 19, 2024 06:48
sub and add repr in c
#include <stdio.h>
#include <stdint.h>
uint8_t da(uint8_t value) {
uint8_t lo = value & 0x0F;
uint8_t hi = value >> 4;
if (lo > 9) lo += 6;
if (hi > 9) hi += 6;
return (hi << 4) | lo;
}