Skip to content

Instantly share code, notes, and snippets.

@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: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: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 / 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: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 / 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