Skip to content

Instantly share code, notes, and snippets.

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

Travis Wagner trvswgnr

:octocat:
hardly workin'
View GitHub Profile
@trvswgnr
trvswgnr / hkt.ts
Last active May 30, 2026 23:27
variable param ts hkt
export const ARGS = Symbol("args");
export type ARGS = typeof ARGS;
export const TYPE = Symbol("type");
export type TYPE = typeof TYPE;
export const _F = Symbol("F");
export type _F = typeof _F;
interface Parameterized<Args extends readonly unknown[]> {
@trvswgnr
trvswgnr / pipe.ts
Created May 27, 2026 14:37
ts simplest lazy pipe
const pipe = <T>(value: T) => {
let fns: Array<(x: any) => unknown> = [];
let cached: { v: T } | undefined = undefined;
const self = {
to: <U>(f: (v: T) => U) => {
fns.push((x: T) => f(x));
return self as unknown as ReturnType<typeof pipe<U>>;
},
run: (): T => {
if (cached) return cached.v;
@trvswgnr
trvswgnr / never.ts
Created February 7, 2026 02:29
typescript `never` on objects weird behavior
// --- Type-level Utilities for Testing --- //
type Expect<T extends true> = T;
type Equal<X, Y> =
(<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2 ? true : false;
const discard = <T>(x: T): void => void x;
const getRandomValue = (): string | number | bigint | boolean | symbol | undefined => {
const choices = ["foo", 1, 2n, true, Symbol("bar"), undefined];
const index = Math.floor(Math.random() * choices.length);
@trvswgnr
trvswgnr / hkt.rs
Last active April 24, 2025 13:00
higher kinded types in rust with functor, applicative, and monad traits + implementation for custom maybe type
//! Higher-kinded types in Rust
//!
//! Functor, Applicative, and Monad traits
//!
//! Example implementations for custom Maybe type
// -- Trait Definitions -- //
/// A constructor for a type of a higher kind with one type parameter
pub trait TypeClass {
@trvswgnr
trvswgnr / main.cpp
Created April 12, 2025 13:49
rust's result and println implemented in c++
#include <iostream>
#include <string>
#include <string_view>
#include <tuple>
#include <typeinfo>
#include <cstdio>
#include <vector>
std::vector<size_t> find_placeholders(const std::string_view& fmt) {
std::vector<size_t> positions;
@trvswgnr
trvswgnr / pipe.rs
Created March 25, 2025 17:05
pipe in rust
#[derive(Debug)]
struct Pipe<T>(T);
impl<T> std::ops::Deref for Pipe<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
@trvswgnr
trvswgnr / enum.ts
Last active February 9, 2025 06:06
typescript enums that are compatible with the --experimental-strip-types flag introduced in node v22.6.0
// --- examples --- //
// happy path
const Priority = Enum("Low", "Normal", "High");
// ^?
type Priority = Enum<typeof Priority>;
// ^?
// invalid key starting with number
const InvalidPriority1 = Enum("1Low", "Normal", "High");
@trvswgnr
trvswgnr / grpo_demo.py
Created February 2, 2025 06:58 — forked from willccbb/grpo_demo.py
GRPO Llama-1B
# train_grpo.py
import re
import torch
from datasets import load_dataset, Dataset
from transformers import AutoTokenizer, AutoModelForCausalLM
from peft import LoraConfig
from trl import GRPOConfig, GRPOTrainer
# Load and prep dataset
@trvswgnr
trvswgnr / functor.rs
Created December 5, 2024 07:43
functor in rust
/*
[dependencies]
*/
mod sealed {
pub trait IsType<T: ?Sized> {}
impl<T: ?Sized> IsType<T> for T {}
}
@trvswgnr
trvswgnr / index.ts
Last active November 28, 2024 05:25
limit to bitwise and evaluate strings
type Op = '<<' | '&' | '|';
const Gift = {
Coal: '0',
Train: '1 << 0',
Bicycle: '1 << 1',
SuccessorToTheNintendoSwitch: '1 << 2',
TikTokPremium: '1 << 3',
Vape: '1 << 4',
Traditional: 'Train | Bicycle',