Skip to content

Instantly share code, notes, and snippets.

View danidiaz's full-sized avatar

Daniel Díaz Carrete danidiaz

View GitHub Profile
@danidiaz
danidiaz / gist:7712739
Created November 29, 2013 22:22
Using StaticArrow to track the number of steps in a computation.
module Main where
import Data.Monoid
import Control.Monad
import Control.Applicative
import Control.Arrow
import Control.Arrow.Transformer
import Control.Arrow.Transformer.Static
type SteppedIO a b = StaticArrow ((,) (Sum Int)) (Kleisli IO) a b
@danidiaz
danidiaz / gist:7713166
Created November 29, 2013 23:07
Using applicatives to track the number of steps in a computation.
module Main where
import Data.Monoid
import Control.Applicative
import Data.Functor.Compose
type SteppedIO a = Compose ((,) (Sum Int)) IO a
step :: IO a -> SteppedIO a
step cmd = Compose (Sum 1, cmd)
@danidiaz
danidiaz / gist:7713554
Created November 29, 2013 23:57
Almost, but not quite. I'm afraid it is imposible to log progress using applicatives.
module Main where
import Data.Monoid
import Control.Applicative
import Data.Functor.Compose
import Control.Monad
import Control.Monad.Trans.State.Strict
type SteppedIO a = Compose ((,) (Sum Int)) (Compose IO (State Int)) a
@danidiaz
danidiaz / gist:7720246
Last active December 29, 2015 19:49
Using applicatives and pipes to track the number of steps in a computation, and show progress. http://stackoverflow.com/questions/20292694/how-to-write-a-monad-that-prints-step-i-of-n-when-executing-each-statement-in
module Main where
import Data.Monoid
import Control.Applicative
import Control.Monad.State
import Data.Functor.Compose
import Data.Functor.Compose
import Pipes
import Pipes.Lift
@danidiaz
danidiaz / gist:7778736
Created December 3, 2013 22:27
example of runghc invocation that requests a package
# http://www.haskell.org/ghc/docs/7.6.3/html/users_guide/ghci-invocation.html#idp32890000
# http://www.haskell.org/ghc/docs/7.0.3/html/users_guide/runghc.html
runghc -- -package --ghc-arg=shelly Script.hs
Aaaa $A \in \mathcal{C}$ bbb
$(F\downarrow U)$
$F(\star)=M$
$\bullet \longleftarrow \bullet \longrightarrow\bullet$
$\left(.\downarrow .\right): Cat^{\rightarrow\leftarrow} \longrightarrow Cat$
@danidiaz
danidiaz / gist:8529275
Created January 20, 2014 21:13
tasty in cabal file
test-suite Tests
hs-source-dirs: test
ghc-options: -Wall
main-is: Test.hs
Type: exitcode-stdio-1.0
default-language: Haskell2010
build-depends: base ==4.6.*, Cabal >= 1.16.0
, nananiero
, HUnit
, QuickCheck
@danidiaz
danidiaz / wrappedTH.hs
Created January 25, 2014 12:52
Deriving a Control.Lens.Wrapped instance using Template Haskell
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
import Control.Lens
data Foo a = Foo { unFoo :: (Int,a) } deriving Show
$(makeWrapped ''Foo)
@danidiaz
danidiaz / gist:8624735
Last active January 4, 2016 12:59
Experimenting with Control.Lens.At and "non"
ghci> at 1 ?~ "hello" $ M.empty
fromList [(1,"hello")]
ghci> at 1 .~ (Just "hello") $ M.empty
fromList [(1,"hello")]
ghci> at 1 .~ Nothing $ M.fromList [(1,"blah")]
fromList []
ghci> (%~) (at 2) (maybe (Just "b") (\x -> Just $ x ++ "bbb")) (M.fromList [(2,"a")])
fromList [(2,"abbb")]
ghci> Nothing ^. non 0
@danidiaz
danidiaz / hexceptions.hs
Created February 3, 2014 22:59
defining a custom exception
{-# LANGUAGE DeriveDataTypeable #-}
data HiddenException e = HiddenException e
deriving (Show, Typeable)
instance (Show e, Typeable e) => Exception (HiddenException e)
elideError :: (Show e, Typeable e) => ErrorT e IO a -> IO a
elideError action = do
runErrorT action >>= either (throwIO.HiddenException) return