Skip to content

Instantly share code, notes, and snippets.

@shajra
shajra / mtl.scala
Created March 18, 2017 18:18
MTL can work in Scala (2.12, with SI-2712 fix)
def tryIt[F[_] : MonadError[String, ?[_]] : MonadState[Int, ?[_]]] = {
val attempt =
for {
_ <- 1.put[F]
_ <- "oh noes".raiseError[F, Unit]
} yield ()
attempt.handleError(_ => ().pure[F])
}
def tryItSE =
@shajra
shajra / config.nix
Last active February 26, 2017 02:18
example of how I manage packages with Nix
{
packageOverrides = super: rec {
/*
haskell = super.haskell // {
compiler = super.haskell.compiler // {
ghc801 = super.stdenv.lib.overrideDerivation super.haskell.compiler.ghc801 (oldAttrs: {
patches = oldAttrs.patches ++ [./ghc.801.patch];
});
};
};
@shajra
shajra / nix-haskell-override.sh
Last active February 9, 2017 08:07
Overrides not overriding for Haskell in Nix?
$ nix-build -E '
let n = import <nixpkgs> {}; hp = n.haskellPackages; l = n.haskell.lib;
in hp.aeson
' # MAKES HADDOCK
/nix/store/rxny4hw6m5hxs69pl27n48whyizx1f1y-aeson-0.11.3.0
$ nix-build -E '
let n = import <nixpkgs> {}; hp = n.haskellPackages; l = n.haskell.lib;
in l.dontHaddock (l.doHaddock hp.aeson)
' # MAKES HADDOCK
@shajra
shajra / builder.sh
Last active January 28, 2017 15:42
Nix work to replace strings in a binary
source "$stdenv/setup"
mkdir -p "$out/bin"
cp "$src/strings-replace" "$out/bin"
wrapProgram "$out/bin/strings-replace" \
--prefix PATH : "${binutils}/bin" \
--prefix PATH : "${coreutils}/bin" \
--prefix PATH : "${gnugrep}/bin"
@shajra
shajra / ClassyPrisms.hs
Last active December 11, 2016 00:03
How is this compiling?
{-# LANGUAGE TemplateHaskell #-}
module ClassyPrisms where
import Control.Lens.TH (makeClassyPrisms)
data Foo = FooA | FooB
@shajra
shajra / App.hs
Last active November 23, 2016 05:32
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE NoImplicitPrelude #-}
module C12E.Exceptions where
import Protolude hiding (catch, onException, uninterruptibleMask)
import Control.Monad.Except (MonadError(catchError, throwError))
import Control.Monad.Catch (MonadMask, uninterruptibleMask)
@shajra
shajra / readfile
Last active September 25, 2018 14:22
Real-world exception handling in Haskell that seems like overkill for this simple example
#!/usr/bin/env stack
-- stack --install-ghc runghc --package safe-exceptions --package lens --package mtl
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE ConstraintKinds #-}
{-# OPTIONS_GHC -Wall -Werror #-}
import Control.Exception (IOException)
@shajra
shajra / ReadV.hs
Created July 20, 2016 06:05
trying to see how haskell-lens can help; pointers welcome
{-# LANGUAGE
FlexibleInstances
, GeneralizedNewtypeDeriving
, NoImplicitPrelude
, OverloadedStrings
, RankNTypes
#-}
module C12E.Validation.ReadV where
trait Translate[A, B] {
def apply(a: A): B
}
trait TranslateSyntax {
implicit class TranslateOps[A](a: A) {
def translate[B](implicit tr: Translate[A, B]): B = tr(a)
}
}