Skip to content

Instantly share code, notes, and snippets.

View bakerface's full-sized avatar

Chris Baker bakerface

  • Test and Controls International
  • Taylorsville, Kentucky
View GitHub Profile
@bakerface
bakerface / example.ts
Last active December 29, 2022 14:39
TypeScript pattern matching in 2 lines of code
import * as Maybe from "./maybe";
import { caseOf } from "./one-of";
const square = (n: number) => n * n;
// serializable as plain js arrays
console.log(Maybe.of(42)); // [ 'Just', 42 ]
// supports exhaustive pattern matching
caseOf(Maybe.map(Maybe.of(3), square), {
@bakerface
bakerface / index.html
Created December 28, 2022 15:03
9x9 TicTacToe
<html>
<head>
<style>
body { width: 100%; }
table { margin: auto; border-spacing: 1rem; }
button { padding: 1rem; }
</style>
</head>
<body>
<table>
@bakerface
bakerface / mocha.h
Last active April 25, 2022 11:38
A single-file implementation of the mochajs test suite runner in pure C
/**
* Copyright (c) 2022 Chris Baker
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
@bakerface
bakerface / co.h
Created April 25, 2022 11:33
A single-file coroutine implementation in pure C
/**
* Copyright (c) 2022 Chris Baker
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
@bakerface
bakerface / crc16.h
Created April 25, 2022 11:21
A single-file CRC-16 CCITT implementation in pure C
/**
* Copyright (c) 2022 Chris Baker
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
@bakerface
bakerface / fluent-example.ts
Last active June 27, 2021 18:18
TypeScript helpers for fluent types.
import type { Fluent } from "./fluent";
export class User {
public id: string;
public username: string;
private constructor() {
this.id = "";
this.username = "";
}
@bakerface
bakerface / match.ts
Last active July 3, 2021 17:06
Basic pattern matching with TypeScript template literals
export type KeysOfParams<S extends string> =
S extends `${infer Before} :${infer Name} ${infer After}`
? KeysOfParams<Before> | Name | KeysOfParams<After>
: S extends `:${infer Name} ${infer After}`
? Name | KeysOfParams<After>
: S extends `${infer Before} :${infer Name}`
? KeysOfParams<Before> | Name
: S extends `:${infer Name}`
? Name
: never;
@bakerface
bakerface / statistics.ts
Last active April 19, 2021 12:48
Statistics in TypeScript
export class Statistics {
private static ensureNotEmpty(arr: readonly number[]): void {
if (arr.length === 0) {
throw new Error("The array must not be empty.");
}
}
static asc(a: number, b: number): number {
return a - b;
}
@bakerface
bakerface / job-queue-example.ts
Last active April 14, 2021 23:59
Job Queue in TypeScript
import { JobQueue } from "./job-queue";
export interface Device {
send(command: string): Promise<void>;
}
const sleep = (ms: number) => new Promise<void>((fn) => setTimeout(fn, ms));
export class AsyncDevice implements Device {
async send(command: string): Promise<void> {
@bakerface
bakerface / mixin.ts
Created April 3, 2021 16:58
TypeScript Mixin
export type Constructor<T> = new (...args: any[]) => T;
export interface Mixin<Features, Requirements = any> {
<T extends Constructor<Requirements>>(Base: T): T & Constructor<Features>;
}