Skip to content

Instantly share code, notes, and snippets.

@Garciat
Garciat / expr.cpp
Last active January 24, 2026 21:48
#include <memory>
#include <print>
#include <sstream>
#include <string>
#include <variant>
template<class... Ts>
struct overloaded : Ts... { using Ts::operator()...; };
auto format(auto&& value) {
import Data.Ratio
rat f g r = f (numerator r) % g (denominator r)
num f = rat f id
den f = rat id f
-- fun way
wallis = (2 % 1) : zipWith ($) (cycle [den (+2), num (+2)]) wallis
@Garciat
Garciat / rex.c
Last active January 2, 2026 07:30
A regular expressions DSL for C, inspired by Haskell's Parsec library. Continued in https://github.com/Garciat/parsing.c
#include <assert.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// ==============================================================
// # Utilities
// ==============================================================
// === Library
interface FeatureKey {}
sealed interface LookupResult<S extends State<S, ?, ?>> {
record Found<S extends State<S, ?, ?>>(S state) implements LookupResult<S> {}
record NotFound<S extends State<S, ?, ?>>() implements LookupResult<S> {}
}
data Dial = Dial Int deriving (Show, Eq)
dialMod :: Num a => a
dialMod = 100
instance Num Dial where
(Dial x) + (Dial y) = Dial ((dialMod + x + y) `mod` dialMod)
negate (Dial x) = Dial ((dialMod - x) `mod` dialMod)
fromInteger x = Dial (fromInteger (x `mod` dialMod))
-- other Num methods skipped for brevity
package org.example;
import static java.lang.reflect.AccessFlag.PUBLIC;
import static java.lang.reflect.AccessFlag.STATIC;
import static java.util.Objects.requireNonNull;
import static java.util.function.Function.identity;
import static org.example.Functions.curry;
import static org.example.Functions.flip;
import static org.example.TyEq.refl;
import static org.example.TypeClass.Witness.Overlap.OVERLAPPABLE;
package org.example;
import static java.lang.reflect.AccessFlag.PUBLIC;
import static java.lang.reflect.AccessFlag.STATIC;
import static java.util.Objects.requireNonNull;
import static org.example.TypeClassSystem.witness;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
interface Refinement<T> {
boolean isValid(T value);
static <T, R1 extends Refinement<T>, R2 extends Refinement<T>> And<T, R1, R2> and(R1 r1, R2 r2) {
return new And<>(r1, r2);
}
record And<T, R1 extends Refinement<T>, R2 extends Refinement<T>>(R1 r1, R2 r2)
implements Refinement<T> {