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.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
@trvswgnr
trvswgnr / logger.ts
Created November 21, 2024 22:43
Syslog Protocol (RFC 5424) Log to Console in TypeScript
/**
* Syslog Facility codes as defined in RFC 5424
*/
enum SyslogFacility {
KERN = 0, // kernel messages
USER = 1, // user-level messages
MAIL = 2, // mail system
DAEMON = 3, // system daemons
AUTH = 4, // security/authorization messages
SYSLOG = 5, // messages generated internally by syslogd