Skip to content

Instantly share code, notes, and snippets.

@bmjames
bmjames / open-haddock
Created April 19, 2015 10:23
Open local haddocks for an installed package
#!/usr/bin/env sh
browser=chromium-browser
package=${1-base}
ghc-pkg describe $package | \
grep haddock-html | \
awk '{ print $2 "/index.html" }' | \
xargs $browser &> /dev/null
@bmjames
bmjames / gist:9e67f0c0f434658c0b4d
Last active June 15, 2023 22:26
Run a Warp server on a random available TCP port
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Control.Concurrent (forkIO, ThreadId)
import Data.ByteString.Lazy (ByteString)
import Data.Text.Lazy (pack)
import Data.Text.Lazy.Encoding (encodeUtf8)
import Network.HTTP.Types (status200)
import Network.HTTP.Types.Header (hContentType)
@bmjames
bmjames / prettify-json.hs
Created January 16, 2015 13:10
Utility which pretty-prints and highlights JSON in output from ngrep (with -W byline)
module Main where
import Data.Aeson hiding (Result)
import Data.Aeson.Encode.Pretty (encodePretty)
import Data.Attoparsec.ByteString
import Data.ByteString (ByteString)
import qualified Data.ByteString.Char8 as BS
import Data.ByteString.Lazy (toStrict)
import System.Console.ANSI
@bmjames
bmjames / gist:752da6635396501a6db1
Last active August 29, 2015 14:01
Console algebra
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module Main where
import Data.Functor
import Control.Monad
import Control.Monad.State
import Control.Monad.Writer
@bmjames
bmjames / gist:9669467
Created March 20, 2014 17:37
Comparing Scala's List#map to Haskell's map
-- http://hackage.haskell.org/package/base-4.6.0.1/docs/src/GHC-Base.html#map
map :: (a -> b) -> [a] -> [b]
map _ [] = []
map f (x:xs) = f x : map f xs
@bmjames
bmjames / gist:6201025
Created August 10, 2013 16:15
Gratuitously point-free JSON encoding
data Dog = Dog { name :: Text
, age :: Int
, likes :: Text
} deriving Show
instance ToJSON Dog where
toJSON = asObject [ name `as` "name"
, age `as` "age"
, likes `as` "likes"
]
@bmjames
bmjames / gist:5319410
Last active December 15, 2015 20:29
Stream implementation
object Chap5 extends App {
import Stream._
def ones: Stream[Int] = cons(1, ones)
def lin: Stream[Int] = cons(1, lin.map(_ + 1))
println(ones.flatMap(_ => empty[Int])) // Stack overflow!
}
sealed trait Stream[A] {
@bmjames
bmjames / gist:4585409
Last active December 11, 2015 10:18
foldMap
import scalaz.Scalaz._
/** The problem: given the following database query results, associate each parentId to
* a List of filenames.
*/
case class Row(parentId: Int, file: String)
val queryResult = Stream(Row(123, "foo.jpg"), Row(123, "bar.jpg"), Row(234, "foo.png"))
/** Using foldLeft is verbose because we have to specify:
* - how to create an empty Map to begin with
@bmjames
bmjames / gist:3789162
Created September 26, 2012 16:55
Balanced parentheses using StateT
object PragmaticParens {
import scalaz._, Scalaz._, typelevel._
private def f(c: Char): StateT[Option, Nat, Unit] =
StateT(nat => (c match {
case '(' => Some(nat.succ)
case ')' => nat.pred
case _ => Some(nat)
}) map (_ -> ()))
@bmjames
bmjames / gist:3378799
Created August 17, 2012 13:45
filterMN
import scalaz._, syntax.monad._
final def filterMN[A, M[_]:Monad, N[_]:Monad](as: List[A])(p: A => N[M[Boolean]]): N[M[List[A]]] =
as match {
case Nil => Monad[N].point(Monad[M].point(Nil))
case h :: t =>
for {
mb <- p(h)
mg <- filterMN(t)(p)
} yield {