Skip to content

Instantly share code, notes, and snippets.

View seanparsons's full-sized avatar
💭
Setting A Status Message

Sean Parsons seanparsons

💭
Setting A Status Message
View GitHub Profile
@seanparsons
seanparsons / servant-cookie.hs
Last active October 29, 2016 08:07
Servant, powered by a free monad with cookies handled in a cross cutting way.
let
pkgs = import <nixpkgs> {};
name = "test";
in pkgs.myEnvFun {
buildInputs = [
pkgs.haskell.packages.ghcjs.ghcWithPackages(p: with p; [ ghcjs-base ghcjs-dom lens text ])
pkgs.nodejs
];
inherit name;
}
@seanparsons
seanparsons / gist:4412803
Created December 30, 2012 13:18
Simple gist for timing code.
def time[T](expression: => T): T = {
val start = System.nanoTime
val result: T = expression
val timeTaken = System.nanoTime - start
if (timeTaken >= 0) println("Time taken: %s nanoseconds".format(timeTaken))
result
}
unstable-shell () { nix-shell -I nixpkgs=https://github.com/NixOS/nixpkgs-channels/archive/nixos-unstable.tar.gz -j 8 $* }
ghci-with-deps () { unstable-shell -p "haskell.packages.ghc7103.ghcWithPackages (pkgs: with pkgs; [ghc $*])" --command ghci }
@seanparsons
seanparsons / gist:7005543
Created October 16, 2013 10:08
Stacking ReaderT and OptionT.
import scalaz._,Scalaz._,scalaz.effect._,OptionT._,Kleisli._
type OptionTIO[+X] = OptionT[IO, X]
val reader1: ReaderT[Option, Int, String] = kleisli[Option, Int, String](n => n.toString.some)
val reader2: ReaderT[IO, Int, String] = kleisli[IO, Int, String](n => IO((n * 2).toString))
val convertedReader1: ReaderT[OptionTIO, Int, String] = reader1.mapK[OptionTIO, String](opt => optionT(IO(opt)))
val convertedReader2: ReaderT[OptionTIO, Int, String] = reader2.mapK[OptionTIO, String](io => io.liftM[OptionT])
@seanparsons
seanparsons / gist:6429556
Last active December 22, 2015 06:18
Possibly parallel and memoised fibonacci.
import Control.Parallel
nfib :: Int -> Int
nfib = (map fib [0 ..] !!)
where fib 0 = 0
fib 1 = 1
fib n = par n1 (pseq n2 (n1 + n2))
where n2 = nfib (n-2)
n1 = nfib (n-1)
@seanparsons
seanparsons / gist:6141023
Created August 2, 2013 15:54
Prettier ReaderT handling code.
def doThing[M[+_]: Monad, T, U, V]
(actionOne: () => ReaderT[M, T, Unit],
actionTwo: () => ReaderT[M, T, Unit],
actionThree: () => ReaderT[M, T, Unit],
actionFour: () => ReaderT[M, (T, U), Unit],
actionFive: () => ReaderT[M, T, Unit,
actionSix: () => ReaderT[M, V, Unit])
(): ReaderT[M, (T, U, V), Unit] = {
for {
_ <- actionOne().localFrom[(T, U, V)]
[error] (myproject/compile:compile) scala.reflect.internal.FatalError: class Universe does not have a member Quasiquote
[error] (myproject/compile:compile) scala.reflect.internal.FatalError: class Universe does not have a member Quasiquote
import org.apache.commons.codec.binary.Base64
import java.nio.file._
import java.nio.file.attribute._
import scala.collection.mutable.ListBuffer
val htmlTemplate = """
<html>
<head>
<title>Encoded Images!</title>
</head>