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 / 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',
@trvswgnr
trvswgnr / partial-application.ts
Created November 27, 2024 00:32
partial application typescript
import type { Expect, Equal, IsAny, IsUnknown } from "type-testing";
import { Solution } from "./solution";
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
type AnyArray = any[];
type PartialApplicationHelper<T extends AnyArray, R> = <A extends Partial<T>>(
...args: A
) => A extends AnyArray
? A["length"] extends T["length"]
@trvswgnr
trvswgnr / distribute.ts
Created November 26, 2024 04:43
distribute workers based on ratios
interface Service {
ratio: number;
name: string;
}
function assignNumWorkers(services: Service[], maxTotalWorkers: number): Map<string, number> {
// sub 1 for the orchestrator
const availableWorkers = maxTotalWorkers - 1;
// calc total ratio sum