Skip to content

Instantly share code, notes, and snippets.

View edofic's full-sized avatar

Andraž Bajt edofic

View GitHub Profile
@edofic
edofic / folds.hs
Created February 15, 2014 12:26
composable folds. tried to implement something like foldl or folds from hackage just to see how hard it is.
{-# LANGUAGE ExistentialQuantification #-}
import Control.Applicative as A
import Data.Foldable as F
data Fold a b = forall x . Fold (x -> a -> x) x (x -> b)
instance Functor (Fold a) where
f `fmap` Fold step zero map = Fold step zero (f . map)
@edofic
edofic / gast.hs
Last active August 29, 2015 13:56
prelude session of generalized abstract syntax tree
Prelude> data GAST a = Literal String | If a a a deriving (Eq, Show)
Prelude> newtype AST = AST (GAST AST) deriving (Eq, Show)
Prelude> data Annotated a = Annotated a deriving (Eq, Show)
Prelude> newtype AAST = AAST (Annotated (GAST AAST)) deriving (Eq, Show)
Prelude> let x = AST $ If (AST $ Literal "foo") (AST $ Literal "bar") (AST $ Literal "baz")
Prelude> let y = AAST $ Correct $ If (AAST $ Wrong $ Literal "foo") (AAST $ Correct $ Literal "bar") (AAST $ Wrong $ Literal "baz")
Prelude> :t x
x :: AST
Prelude> :t y
y :: AAST
@edofic
edofic / echo_repl.hs
Last active August 29, 2015 13:56
echo repl with haskeline library
import System.Console.Haskeline
import Control.Monad
import Control.Monad.Trans.Class
import Control.Monad.Trans.Maybe
main :: IO ()
main = runInputT defaultSettings repl
repl :: InputT IO ()
repl = fmap (const ()) $ runMaybeT $ forever $ do
@edofic
edofic / 1_fact_stateT.hs
Last active August 29, 2015 13:57
stateful loops in haskell
-- This is an exercise solution from the book Beginning Haskell.
-- with -O3 running time for 100000 is 5.4 seconds
{-# LANGUAGE PackageImports #-}
import "mtl" Control.Monad.State
factState :: StateT Integer (State Integer) ()
factState = do
n <- get
when (n > 1) $ do
@edofic
edofic / reader.js
Created April 23, 2014 07:16
Reader monad in Javascript. Mimicking Scala OO style. Just a proof of concept implementation.
var readerLift = function(f) {
var m = function(r){
return f(r)
};
m.map = function(g){
return readerLift(function(r){
return g(f(r))
});
};
m.flatMap = function(g){
module Percentage
( Percentage -- abstract
, mkPercentage
, getPercentage
, discount
) where
import Data.Monoid
newtype Percentage a = Percentage { runPercentage :: a }
@edofic
edofic / rows.hs
Created May 5, 2014 21:06
fake row polymorphism in haskell using contexts and existentials
class HasName a where
getName :: a -> String
class HasAge a where
getAge :: a -> Int
data Person = Person String Int deriving (Eq, Show)
instance HasName Person where
getName (Person name _) = name
@edofic
edofic / algebra.hs
Created May 8, 2014 10:49
Real world f-algebras and catamorphisms
newtype Fix f = Fix { unFix :: f (Fix f) }
type Algebra f a = f a -> a
type InitialAlgebra f = Algebra f (Fix f)
cata :: Functor f => Algebra f a -> Fix f -> a
cata alg = go where go = alg . fmap go . unFix
--------------------------
@edofic
edofic / iorefCast.hs
Created May 13, 2014 20:38
unsafePerformIO cast in haskell
import Data.IORef
import System.IO.Unsafe
unsafeCast :: a -> b
unsafeCast a = unsafePerformIO $ do
let ref = unsafePerformIO $ newIORef undefined
writeIORef ref a
readIORef ref
@edofic
edofic / 1_beer.hs
Last active August 29, 2015 14:01
today i saw a shirt with `while (standing) drink(BEER);` on it
-- direct port, with implementation details, not so nice
import Control.Monad
import Control.Monad.Trans
import Control.Monad.Trans.Maybe
import Data.IORef
data Alcohol = BEER deriving Show
main = do