Skip to content

Instantly share code, notes, and snippets.

View dmwit's full-sized avatar

Daniel Wagner dmwit

View GitHub Profile
{-# LANGUAGE QuantifiedConstraints #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE DataKinds #-}
import GHC.Exts
type family AllAllowed k xs where
AllAllowed k '[] = ()
AllAllowed k (x : xs) = (Allowed k x, AllAllowed k xs)
\xs -> sum $ map (sum . toDigits) xs
= { f $ x = f x }
\xs -> sum (map (sum . toDigits) xs)
= { function application associates to the left }
\xs -> sum ((map (sum . toDigits)) xs)
= { (f . g) x = f (g x), with f = sum, g = (map (sum . toDigits)) }
\xs -> (sum . (map (sum . toDigits))) xs
= { eta reduction }
(sum . (map (sum . toDigits)))
= { remove unnecessary clarifying parentheses }
{-# LANGUAGE TypeFamilies #-}
class Foo a where
type I a t
foo :: I a t -> t -> a
instance Foo () where
type I () t = ()
foo _ _ = ()
instance Foo b => Foo (a, b) where
@dmwit
dmwit / ghc-version
Created February 24, 2019 19:21
A shell script to switch which version of GHC the non-versioned names point to
#!/bin/zsh
set -e
setopt null_glob
case $# in
1) true;;
*)
echo Usage: ghc-version VERSION
ghc --version || true
{-# LANGUAGE FlexibleInstances #-}
import Data.Functor.Compose
import Control.Monad
joinEntry :: Maybe [Maybe [a]] -> Maybe [a]
joinEntry = fmap join . join . fmap sequenceA
instance Monad (Compose Maybe []) where
-- or use liberal applications of coerce to avoid the calls to Compose and getCompose
x >>= f = Compose . joinEntry . getCompose . fmap (getCompose . f) $ x
class Stream
def initialize(&more)
@sofar = []
@more = more
end
def [](i)
while i >= @sofar.length do
@sofar += @more.call(@sofar)
end
-- dmwit style
module Parser where
import Data.Time
data Transaction = Transaction
{ amount :: Double
, time :: UTCTime
, tags :: [String]
@dmwit
dmwit / test.c
Created January 23, 2019 18:01
how to masquerade as somebody else
#include <unistd.h>
int main() {
char *args[3] = {"notsleep", "10", NULL};
return execv("/usr/bin/sleep", args);
}
@dmwit
dmwit / wat.hs
Created December 18, 2018 04:01
testing tab settings
import Data.List
import System.Environment
data SizedTree a = Node Integer a [SizedTree a] deriving (Eq, Ord, Read, Show)
type Stream a = [SizedTree a]
indexT :: Integer -> SizedTree a -> a
indexT 0 (Node _ v _) = v
indexT n (Node _ _ vs) = indexS (n-1) vs
@dmwit
dmwit / whatevs.hs
Last active December 18, 2018 03:58
sublinear indexing with trees
import Data.List
import System.Environment
data SizedTree a = Node Integer a [SizedTree a] deriving (Eq, Ord, Read, Show)
type Stream a = [SizedTree a]
indexT :: Integer -> SizedTree a -> a
indexT 0 (Node _ v _) = v
indexT n (Node _ _ vs) = indexS (n-1) vs