Skip to content

Instantly share code, notes, and snippets.

@Lucus16
Lucus16 / fluid.zig
Last active June 24, 2024 14:26
A better fluid model for Factorio.
// Optimal flow speed is when pipes are at half pressure.
const Node = struct {
// All of these must be positive, I'm just using signed types to avoid casting.
height: i32,
width: i32,
contents: i32,
potential_inflow: i32,
potential_outflow: i32,
@Lucus16
Lucus16 / Futamura.hs
Last active May 8, 2024 16:21
The Futamura projections type-checked by Haskell.
{-# LANGUAGE ImpredicativeTypes #-}
module Futamura where
newtype C a = C {runC :: a}
newtype L a = L a
newtype Executable a = Executable {run :: a}
-- Given:
-- - A high level language L
@Lucus16
Lucus16 / Lifetime.hs
Created February 15, 2024 17:18
Haskell supports lifetimes!
{-# LANGUAGE RankNTypes #-}
module Lifetime where
import Data.Text (Text)
import Data.Text.IO qualified as Text
import System.IO (IOMode (..))
import System.IO qualified as Base
import Prelude hiding (Handle, withFile)
import Prelude qualified as Base
@Lucus16
Lucus16 / lineageos.md
Created December 25, 2023 22:29
Install LineageOS on a Nokia 6.1
@Lucus16
Lucus16 / splitmix.nix
Created October 30, 2023 16:21
Experiment to build haskell in pure nix
{ pkgs ? import <nixpkgs> { } }:
with builtins;
rec {
inherit (pkgs) ghc lib;
uniqueStrings = strings:
attrNames (listToAttrs (map (name: {
inherit name;
@Lucus16
Lucus16 / SafeIO.hs
Created August 16, 2023 11:56
Proof of concept for interruptible-checked IO
module System.SafeIO where
import "base" Control.Concurrent ( ThreadId )
import "base" Control.Concurrent qualified as Unsafe
import "base" Control.Exception ( AsyncException(..), Exception(..) )
import "base" Control.Exception qualified as Unsafe
import "base" Prelude ( Applicative(..), Functor(..), Monad(..), const, ($), (.) )
import "base" System.IO qualified as Unsafe
data Uninterruptible
newtype Pool r = Pool (TVar (Seq r))
newPool :: [r] -> IO (Pool r)
newPool = fmap Pool . newTVarIO . Seq.fromList
getResource :: Pool r -> IO r
getResource (Pool pool) = STM.atomically do
rs <- STM.readTVar pool
case rs of
Seq.Empty -> STM.retry
def caffeine(hours):
sip = hours * (1 - 0.5**(1 / 5))
gulp = 1 - 0.5**((24 - hours) / 5)
total = sip + gulp
print("If you want to maintain a consistent amount of caffeine in your")
print("blood for {} hours after you wake up, you'll need to drink {:.1%}"
.format(hours, gulp / total)
)
print("of the amount of caffeine you want to drink in a day when you")
print("wake up, and {:.1%} during the {} hours thereafter, which is about"
#include <stdint.h>
#include <string.h>
#define MASK26 0x03ffffff
// Load 128-bit value from 4 32-bit limbs into 5 26-bit limbs.
void load26(uint32_t dst[5], const uint32_t src[4]) {
dst[0] = src[0] & MASK26;
dst[1] = (src[0] >> 26 | src[1] << 6) & MASK26;
dst[2] = (src[1] >> 20 | src[2] << 12) & MASK26;
@Lucus16
Lucus16 / Fold.hs
Created December 5, 2022 23:16
Fold in both directions
-- Ever have trouble deciding whether to use a left or right fold?
-- Por que no los dos?
-- Fold left and right at the same time!
-- Use accumulated values from both directions for each step!
module Fold where
import Data.Set (Set)
import Data.Set qualified as Set