Skip to content

Instantly share code, notes, and snippets.

View evanrelf's full-sized avatar

Evan Relf evanrelf

View GitHub Profile
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DerivingStrategies #-}
import Data.Bits (toIntegralSized)
import Data.Function ((&))
import Data.Kind (Type)
import Data.Monoid (Sum (..))
import Data.Proxy (Proxy (..))
import Data.Sequence (Seq (..))
import Data.Sequence qualified as Seq
<script type="module" async>
// TODO: Fix error using `@codemirror/lang-markdown`:
// ```
// Uncaught TypeError: Cannot read properties of undefined (reading 'deserialize')
// at h (index.js:1585:48)
// at new l (index.js:1600:29)
// at l.deserialize (index.js:1841:16)
// at index.js:252:25
// ```
use std::fmt::{self, Display};
// TODO: Use tuples instead of arrays for heterogeneous lists
// TODO: Escaping
#[derive(Debug, PartialEq)]
pub enum Html {
Node(Node),
Text(String),
}
#!/usr/bin/env nix-shell
#!nix-shell -i ghcid -p ghcid "ghc.withPackages (p: with p; [ linear-base ])"
-- Rough, imperfect re-implementation of this article on using Swift's
-- "typestate" pattern. Haskell fails to replicate some aspects, and exceeds
-- Swift's capabilities in other areas.
--
-- https://swiftology.io/articles/typestate/
{-# LANGUAGE DataKinds #-}
#!/usr/bin/env nix-shell
#!nix-shell -i runghc -p "ghc.withPackages (p: with p; [ safe-exceptions ])"
{-# LANGUAGE BlockArguments #-}
module Main (main) where
import Control.Exception.Safe (Exception, MonadCatch, catch, throw)
import Control.Monad (when)
import Data.Dynamic (Typeable)
use futures_lite::future::FutureExt as _;
use std::future::{pending, ready};
enum Output {
One(usize),
Two(usize),
Three(usize),
}
let one = async { Output::One(pending().await) };
use std::{
ops::{Deref, DerefMut},
sync::{
atomic::{AtomicBool, Ordering},
Arc,
},
};
/// An `Option<T>` that behaves like a `T`. Avoid paying the cost of `Option` - no unwrapping or
/// matching needed to access the value - when your value is present 99% of the time. For the rare
data HList :: [Type] -> Type where
HNil :: HList '[]
HCons :: x -> HList xs -> HList (x : xs)
infixr 5 `HCons`
type (<>) :: [k] -> [k] -> [k]
type family (<>) xs ys where
(<>) '[] '[] = '[]
(<>) (x : xs) '[] = x : xs
@evanrelf
evanrelf / aoc-download
Last active July 15, 2025 21:40
Offline Advent of Code
#!/usr/bin/env bash
set -Eeuo pipefail
IFS=$'\n\t'
if [ "$#" -ne 1 ]; then
echo "usage: ./aoc-download <year>" >&2
exit 1
fi
macro_rules! key_modifiers {
() => {
::crossterm::event::KeyModifiers::NONE
};
($m:ident) => {
::crossterm::event::KeyModifiers::$m
};
($m:ident | $($ms:ident)|+) => {
::crossterm::event::KeyModifiers::$m | crate::macros::key_modifiers!($($ms)|+)
};