Skip to content

Instantly share code, notes, and snippets.

@Solonarv
Solonarv / monkeypatch.py
Last active April 3, 2024 23:34
Simple monkey patching framework.
# Released under Unlicense.
from inspect import getattr_static
import functools
class monkeypatch:
""" Decorator for all your evil monkeypatching needs.
Functions as a class decorator:
@monkeypatch(Foo)
@Solonarv
Solonarv / .asoundrc
Created May 31, 2015 15:43
The ALSA setup that lets me choose to record, hear or both each source separately
pcm.!default {
type asym
playback.pcm "LoopAndReal"
capture.pcm "plug:realrecds"
}
pcm.realrecds {
type dsnoop
ipc_key 1026
slave.pcm "realrec"
@Solonarv
Solonarv / mloeb.hs
Created December 11, 2020 19:03
Monadic Loeb fixed-point combinator
-- standard "loeb" combinator
-- https://github.com/quchen/articles/blob/master/loeb-moeb.md
loeb :: Functor f => f (f a -> a) -> f a
loeb x = go where go = fmap ($ go) x
-- "monadic loeb"?
loebM :: (Traversable t, Monad m) => t (t a -> m a) -> m (t a)
loebM = sequenceA . loeb . fmap (\f xs -> f =<< sequenceA xs)
@Solonarv
Solonarv / snecko-energy.hs
Created September 12, 2020 01:19
Number of playable cards in hand with Snecko Eye and energy relic (Slay the Spire).
import System.Random
import Data.Monoid
import Control.Applicative
import Control.Monad
randomRsIO range = randomRs range <$> newStdGen
numPlayables :: Int -> [Int] -> Int
numPlayables energy costs = zeros + nonzeros
where
@Solonarv
Solonarv / FastFibo.hs
Last active February 28, 2020 21:15
Overengineered fast fibonacci
{-# LANGUAGE UnboxedTuples #-}
{-# LANGUAGE MagicHash #-}
module FastFibo where
import GHC.Exts
type Mat2x2 = (# Int#, Int#, Int#, Int# #)
type Vec2 = (# Int#, Int# #)
@Solonarv
Solonarv / switchctx.py
Last active November 26, 2019 17:32
Augmented switch statements in python, using a hybrid context manager/decorator approach.
from abc import ABCMeta, abstractmethod
def do_nothing(f):
"""A decorator that swallows the function and does nothing with it."""
pass
def instantiate(callable):
"""A decorator that replaces a class definition with an instance of the class.
Useful for making opaque singleton objects.
@Solonarv
Solonarv / Objects.File.hs
Last active June 29, 2019 16:52
Object oriented haskell (in the message-passing sense). Each objects lives in a separate thread.
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
module Objects.File where
import Control.Monad
import System.IO
import Objects
:set prompt "\ESC[1;34m%s\n\ESC[0;34mλ> \ESC[m"
:set prompt-cont "\ESC[0;34mλ| \ESC[m"
@Solonarv
Solonarv / IxSTRef.hs
Created April 4, 2019 19:27
STRefs that carry an extra tag to enable hashing, comparison &c. Safe, I think.
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE RoleAnnotations #-}
module IxSTRef
( IxSTRef, ixSTRefId, ixSTRef
, IxCounter, withIxCounter, newIxSTRef
) where
import Control.Monad.ST
import Data.Function
import Data.STRef
@Solonarv
Solonarv / type-fizzbuzz.hs
Created March 29, 2019 17:03
Should be correct, but takes so long to compile that I have no idea if it actually works.
{-# OPTIONS_GHC -Wno-unticked-promoted-constructors -freduction-depth=0 #-}
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE ExplicitForAll #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}