Skip to content

Instantly share code, notes, and snippets.

View matthewoestreich's full-sized avatar
♠️
👋

Matt Oestreich matthewoestreich

♠️
👋
View GitHub Profile
@matthewoestreich
matthewoestreich / optional.cpp
Created October 18, 2026 00:16
Optional Type in C++
/*
Implement an optional type.
Constraints:
- It either has a value or it doesn't
- The API allows you to check if it has a value
- It is implicitly convertible to `boolean` (`true` if a value exists)
- If `T` is large (>= 16 bytes) it should reside on the heap, otherwise on the stack
- This optional type should only be at most 8 bytes on a 64-bit system IF `T` is >= 16 bytes
@matthewoestreich
matthewoestreich / chrome-math-random.js
Last active October 8, 2026 04:11
Exact copy of Chrome Math.random implementation in JS
/**
* This script completely mimics how V8 implements Math.random in V8 versions >= 15.4.80.20
* https://source.chromium.org/chromium/_/chromium/v8/v8/+/03f0a9e97f21af68f1359f011f7d44ddbc2e7130:include/v8-version.h;bpv=1;bpt=0;drc=03f0a9e97f21af68f1359f011f7d44ddbc2e7130
*
* If you use `Chrome 154.0.8037.98 (Official Build) (x86_64)`, which runs V8 v15.4.80.20, you
* can set the seed that Math.random uses by calling Chrome with specific flags.
*
* This is how I did it on Mac
* `/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --js-flags="--random-seed=1337"`
*
@matthewoestreich
matthewoestreich / ThreadedDeque.rs
Last active November 10, 2025 17:36
Thread safe VecDeque
pub mod threadsafe {
use std::{
collections::VecDeque,
sync::{Arc, Mutex, MutexGuard},
};
// One-liner that allows us to easily lock a Mutex while handling possible poison.
fn safe_lock<T>(m: &Mutex<T>) -> MutexGuard<'_, T> {
match m.lock() {
Ok(guard) => guard,
alert("Wizer");
@matthewoestreich
matthewoestreich / sqlite.sql.zip
Created February 17, 2025 17:50
Uploaded via script @ Mon, 17 Feb 2025 17:50:39 GMT UTC
This file has been truncated, but you can view the full file.
@matthewoestreich
matthewoestreich / sqlite.sql.gz
Created February 16, 2025 23:59
Testing .gz as Gist.
This file has been truncated, but you can view the full file.
import React, { useReducer, useState } from 'react';
interface Message {
from: string;
message: string;
}
interface ChatState {
messages: Message[] | null;
}
@matthewoestreich
matthewoestreich / solution.js
Created November 4, 2024 00:59
Training on Warcraft Series I
function generateEmptyPathArray(pathLength) {
let path = ["M"];
for (let i = 1; i < pathLength-1; i++) {
path.push(".");
}
path.push("B");
return path;
}
function simulateMining(path, time) {
@matthewoestreich
matthewoestreich / slice_filter.go
Created August 13, 2024 02:58
Generic slice filter in Go with similar behavior to `[].filter((i, e) => bool)` in JS.
type filterFunc[T any] func(int, T) bool
func filter[T any](arr []T, fn filterFunc[T]) []T {
filtered := []T{}
for i, e := range arr {
if fn(i, e) {
filtered = append(filtered, e)
}
}
return filtered
const https = require("https");
const querystring = require("querystring");
const fs = require("fs");
const path = require("path");
(async () => {
// Root of json file should be an array..
// []
const LOG_FILE_PATH = path.resolve(__dirname, "./log.json");