Skip to content

Instantly share code, notes, and snippets.

@aavogt
aavogt / zipper.rs
Last active February 21, 2023 02:31
struct Zipper<'a, T> {
up : Vec<&'a mut T>,
focus : &'a mut T,
down : Vec<&'a mut T>,
}
impl<'a, T> Zipper<'a, T> {
fn focus_n(&'a mut self, n: i32) -> Option<&mut Self> {
let mut x = self.focus_top()?;
// n focus downs from the top
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeFamilies #-}
type family Remove v xs where
Remove x (x ': xs) = Remove x xs
Remove x (y ': xs) = y ': Remove x xs
Remove x '[] = '[]
@aavogt
aavogt / StaticVars.hs
Last active November 11, 2024 18:43
ImplicitParams emulating `static`
{-# LANGUAGE TemplateHaskell #-}
{- ORMOLU_DISABLE -}
{- |
Write this:
@
{-# LANGUAGE ImplicitParams, TemplateHaskell #-}
import Data.IORef
{-# LANGUAGE StandaloneDeriving, FlexibleContexts, UndecidableInstances #-}
-- zippers is probably a better answer
import Control.Lens
import Data.Maybe
-- with index
data WI m = WI (Index m) m
deriving instance (Show m, Show (Index m)) => Show (WI m)
mkwi' :: Ixed m => Index m -> m -> WI m
-- we can implement drawN_ in terms of drawN, but not the other way around
--
drawN_ :: MonadState s m => (forall b. [b] -> m [b]) -> m ()
drawN_ = drawN
drawN :: MonadState s m => ([Card] -> m [Card]) -> m ()
drawN shuffle = _
@aavogt
aavogt / overpay.hs
Last active December 12, 2018 00:19
import Control.Lens
{- | in monopoly deal you have to pay more than you owe,
if you don't have exact change
Ex. if you have $3, $2 $2 $2, $1 $1 bills, and owe 4:
>>> overpay 4 [(3,1),(2,3),(1,2)]
[([(1,2),(2,1)],[(2,2),(3,1)],0),
([(2,2)],[(1,2),(2,1),(3,1)],0),
@aavogt
aavogt / Main.hs
Created May 18, 2016 17:12
lexicographic sorting doesn't go faster when the sorting function sees more structure
module Main where
import Criterion.Main
import Criterion
import Data.List
import Data.Foldable (toList)
import Data.Random
import Data.Monoid ((<>))
import qualified Data.IntMap as IM
@aavogt
aavogt / HistogramProb.hs
Last active May 11, 2016 19:31
histograms are almost applicative
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE PartialTypeSignatures #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE TemplateHaskell #-}
module HistogramProb where
import qualified Data.Map as M
import Control.Lens
@aavogt
aavogt / xmonad.hs
Last active May 5, 2016 17:15 — forked from lierdakil/xmonad.hs
A toy implementation of fully monadic config
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE FunctionalDependencies #-}
@aavogt
aavogt / exceptions.hs
Created May 2, 2016 18:17
exceptional xmonad layout instances are rare
module Main where
import System.Process
import Data.Char
import Text.PrettyPrint
listModules :: String -> IO [String]
listModules pkgName =
-- remove empty lines (possible?) or the exposed-modules: heading