Skip to content

Instantly share code, notes, and snippets.

@thsutton
thsutton / Comb.scala
Last active August 1, 2017 06:24
Some generators for choices, permutations, shuffling, etc.
import scalaprops._
/** Generate a permutation of the given values.
*
* > permute("ABC").sample
* Vector(A,C,B)
* > permute("ABC").sample
* Vector(B,A,C)
*/
def permute[A](values: IndexedSeq[A]): Gen[IndexedSeq[A]] =
@thsutton
thsutton / StrictSubtype.scala
Created July 27, 2017 23:10
Bound a type parameter to be strictly a subtype.
object Test extends App {
// From https://stackoverflow.com/a/6944070
trait =!=[A, B]
implicit def neq[A, B] : A =!= B = null
// This pair excludes the A =:= B case
implicit def neqAmbig1[A] : A =!= A = null
TARGET=.
REPORT=/tmp/foo.sha
find $TARGET -type f -print0 | sort -z | xargs -0 sha1sum | tee >(sha1sum >> $REPORT) > $REPORT
@thsutton
thsutton / e.idr
Created December 23, 2015 03:54
Half working Idris code for the E language from PFPL
module Main
data Nt : Type where
Zn : Nt
Sn : Nt -> Nt
mutual
data Even : Nt -> Type where
EvenZn : Even Zn
EvenSn : Odd n -> Even (Sn n)
@thsutton
thsutton / lol.sql
Created December 1, 2015 09:34
SQLite derp
sqlite> create table foo ( id int not null );
sqlite> insert into foo values (1);
sqlite> insert into foo values (2);
sqlite> insert into foo values ("hello");
sqlite> select * from foo;
1
2
hello
@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
@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 / 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 / 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 / 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"] ""