Skip to content

Instantly share code, notes, and snippets.

@thsutton
thsutton / main.hs
Created August 8, 2014 05:52
Monad logger
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Control.Applicative
import Control.Monad.Except
import Control.Monad.IO.Class ()
import Control.Monad.Reader
@thsutton
thsutton / demo.hs
Last active August 29, 2015 14:07 — forked from anonymous/demo.hs
module Main where
import Control.Applicative
import Data.Char
import Data.List
import Options.Applicative
import Options.Applicative.Types
-- * Options parsers
@thsutton
thsutton / demo.hs
Last active August 29, 2015 14:07 — forked from anonymous/demo.hs
module Main where
import Control.Applicative
import Data.Char
import Data.List
import Options.Applicative
import Options.Applicative.Types
-- * Options parsers
@thsutton
thsutton / secret.sh
Last active August 29, 2015 14:08
Edit a GPG encrypted file, maintaining (and extending) multiple recipients
#!/bin/sh
#
# Access a shared secret file.
set -eu
error() {
echo $1
exit 1
}
@thsutton
thsutton / README.md
Created February 3, 2015 03:06
Running a testing slapd instance

Running a test slapd instance

I use this to run a slapd instance for testing schemata, searched, etc. during development.

Usage

mkdir -p schema
@thsutton
thsutton / test.hs
Created May 26, 2015 01:29
Invoke GHC to check that files compile in a cabal test suite
module Main where
import System.Exit
import System.Process
main :: IO ()
main = do
(c1, o1, e1) <- readProcessWithExitCode "ghc" ["-ilib", "-o", "/tmp/failure", "test/fail.hs"] ""
(c2, o2, e2) <- readProcessWithExitCode "ghc" ["-ilib", "-o", "/tmp/passure", "test/pass.hs"] ""
@thsutton
thsutton / Fails.hs
Created July 7, 2015 01:26
HLint can't parse type application in a list
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE MultiParamTypeClasses #-}
module Foo where
import GHC.TypeLits
data Header (sym :: Symbol) (val :: Symbol)
type Foo = '[Header "X-Forwarded-For" "Satan"]
@thsutton
thsutton / Validate.scala
Created October 14, 2015 04:34
Simple validation thing
sealed abstract class Validation[T] {
def must(p: (T => Boolean), error: String): Validation[T]
}
object Validation {
def apply[T](value: T): Validation[T] = Valid(value)
}
case class Invalid[T](value: T, errors: List[String]) extends Validation[T] {
def must(p: (T => Boolean), error: String): Validation[T] =
@thsutton
thsutton / LICENSE
Last active October 27, 2015 02:45
Count n-length paths through a graph by matrix multiplication
Copyright Thomas Sutton (c) 2015
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
@thsutton
thsutton / Set.hs
Created November 13, 2015 05:32
Compare sets by inclusion
module Set where
import Data.Set (Set)
import qualified Data.Set as S
-- | Compare two 'Set's by inclusion or, failing that, by size.
compareInclusion :: (Ord e) => Set e -> Set e -> Ordering
compareInclusion s1 s2 =
let lt = S.isSubsetOf s1 s2
gt = S.isSubsetOf s2 s1