Skip to content

Instantly share code, notes, and snippets.

@5outh
5outh / events.hs
Created May 3, 2016 22:14
Event System in Haskell
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Control.Monad.State
import Data.Text
import Data.Monoid
import qualified Data.Map as M
import Control.Monad
-- For example
type JSON = Text
-- An instance of RenderResponse knows how to turn itself into JSON
class RenderResponse a where
render :: a -> JSON
data Error = Error Text
data RegularResponse = RegularResponse Text -- For example
data ErrorResponse a = ErrorResponse a [Error] -- Parameterize over Response type
# Note: This is after the lines restricting php 7 have been commented out in `configure`. My PHP Version output:
$ php --version
PHP 7.0.5 (cli) (built: Mar 31 2016 06:38:23) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
with Xdebug v2.4.0, Copyright (c) 2002-2016, by Derick Rethans
$ export LIBFFI_PATH=$(brew --prefix libffi)
$ LDFLAGS=" -L${LIBFFI_PATH}/lib" PKG_CONFIG_PATH="$PKG_CONFIG_PATH:${LIBFFI_PATH}/lib/pkgconfig" ./configure
./configure: line 624: test: /Applications/Sublime: binary operator expected
@5outh
5outh / docgen.sh
Last active August 29, 2015 14:04
Manual hackage documentation upload
# Usage (from project root): ./docgen.sh <Repo Name> <Version> <Hackage Username> <Hackage Password>
# e.g. ./docgen.sh Bang 0.1.1.0 5outh F4kePa55word
#!/bin/bash
cabal configure && cabal build && cabal haddock --hyperlink-source \
--html-location='http://hackage.haskell.org/package/$pkg/docs' \
--contents-location='http://hackage.haskell.org/package/$pkg'
S=$?
if [ "${S}" -eq "0" ]; then
cd "dist/doc/html"
DDIR="${1}-${2}-docs"
@5outh
5outh / Bots.hs
Last active August 17, 2018 12:49
Bots2 Clone
import Pipes
import qualified Pipes.Prelude as P
import qualified System.Random as R
import Lens.Family2
import Lens.Family2.Stock
import Lens.Family2.State.Lazy
import Control.Monad.Trans.State
import Control.Monad
import Control.Concurrent(threadDelay)
main = mapM_ printNotExpr . lines =<< readFile "inputs.txt"
where printNotExpr e = case parseExpr e of
Right x -> print $ not x
Left e -> error $ show e
instance Show Expr where
show (Not e) = "NOT " ++ show e
show (And e1 e2) = show e1 ++ " AND " ++ show e2
show (Or e1 e2) = show e1 ++ " OR " ++ show e2
show (Var c) = [c]
show (SubExpr e) = "(" ++ show e ++ ")"
parseExpr :: String -> Either ParseError Expr
parseExpr = parse expr ""
where expr = buildExpressionParser operators term <?> "compound expression"
term = parens expr <|> variable <?> "full expression"
operators = [ [Prefix (string "NOT" *> spaces *> pure Not)]
, [binary "AND" And]
, [binary "OR" Or] ]
where binary n c = Infix (string n *> spaces *> pure c) AssocLeft
variable = Var <$> (letter <* spaces) <?> "variable"
parens p = SubExpr <$> (char '(' *> spaces *> p <* char ')' <* spaces) <?> "parens"
@5outh
5outh / not.hs
Created November 13, 2013 20:58
not :: Expr -> Expr
not (Not e) = e
not (And e1 e2) = Or (not e1) (not e2)
not (Or e1 e2) = And (not e1) (not e2)
not (SubExpr e) = not e
not (Var c) = Not (Var c)
@5outh
5outh / BoolExpr.hs
Last active December 28, 2015 06:19
data Expr = Not Expr
| And Expr Expr
| Or Expr Expr
| SubExpr Expr
| Var Char
deriving Eq