Skip to content

Instantly share code, notes, and snippets.

View md2perpe's full-sized avatar

Per Persson md2perpe

View GitHub Profile
fizzbuzz :: Int -> String
fizzbuzz n = unlines $ map fizzbuzz' [1 .. n]
where
fizzbuzz' n = let p = n `mod` 3 == 0
q = n `mod` 5 == 0
in case (p, q) of
(p, q) | p && q -> "FizzBuzz"
| p -> "Fizz"
| q -> "Buzz"
| otherwise -> show n
@CHH
CHH / .gitignore
Created August 2, 2011 21:09
PHP Templating Engine with bindable $this support in 53 LOC
vendor/
composer.lock
@palladin
palladin / gist:1088010
Created July 17, 2011 20:14
Hughes's CPSFuncList
type FuncList<'a> = 'a list -> 'a list
type CPSFuncList<'a> = FuncList<'a> -> FuncList<'a>
// Monoid comprehension
type CPSFuncListBuilder() =
member self.Combine (first : CPSFuncList<'a>, second : CPSFuncList<'a>) : CPSFuncList<'a> =
(fun k -> second (fun tail -> first (fun tail' -> k tail') tail))
member self.Zero() : CPSFuncList<'a> = (fun k tail -> k tail)
member self.Yield (value : 'a) : CPSFuncList<'a> = (fun k tail -> k (value :: tail))
member self.YieldFrom (value : CPSFuncList<'a>) : CPSFuncList<'a> = value
@palladin
palladin / gist:1088009
Created July 17, 2011 20:13
Hughes's FuncList
// for more info http://www.cs.tufts.edu/~nr/cs257/archive/john-hughes/lists.pdf
type FuncList<'a> = 'a list -> 'a list
// Monoid comprehension
type FuncListBuilder() =
member self.Combine (first : FuncList<'a>, second : FuncList<'a>) : FuncList<'a> = (first << second)
member self.Zero() : FuncList<'a> = id
member self.Yield (value : 'a) : FuncList<'a> = fun tail -> value :: tail
member self.YieldFrom (value : FuncList<'a>) : FuncList<'a> = value
member self.Delay ( f : unit -> FuncList<'a>) : FuncList<'a> = (fun tail -> f () tail)
@isomorphism
isomorphism / RotateArgs.hs
Created July 16, 2011 18:25
generalized flip
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
module RotateArgs where
data Z = Z
data S n = S n
class RotateArgs n a where
modelIO :: IO a -> Model a
modelIO action = do
Model $ ReaderT (\_ -> action)
insertBook :: Book -> Model (LoadedCoreEntity Book)
insertBook bookSpec = do
bookGid <- (modelIO randomIO) :: Model UUID
bookRow <- head `fmap` query insertQuery [ toSql "Hello" ]
return $ bookFromRow bookRow
where insertQuery = unlines [ "INSERT INTO book (name)"
bookResource :: Controller ()
bookResource = do
gid <- ((fromString . unpack . fromJust) <$> getParam "gid") >>= (\gid' -> gid' `onNothing` "Invalid BBID")
maybeBook <- model $ getBook gid
case maybeBook of
Nothing -> generic404 "The request book could not be found"
Just book -> do
book <- model $ loadAuthorCredit book
book <- model $ set lBookAuthorCredit book <$> (loadForAuthorCredits $ bookAuthorCredit book)
editions <- model $ findBookEditions book
-- This changes bookAuthorCredit from AuthorCreditReference to AuthorCredit
loadAuthorCredit :: Book -> Model Book
-- This changes all PersonReference's in an AuthorCredit to just Person
loadForAuthorCredits :: AuthorCredit -> Model AuthorCredit
-- This gets the AuthorCredit for a Book
bookAuthorCredit :: Book -> AuthorCredit
-- This 'sets' the AuthorCredit for a Book (returning a new Book)
{
process := make(chan *feat.Feature)
buffer := make(chan *feat.Feature, 1000)
wg := new(sync.WaitGroup)
if *cores < 2 {
*cores = 2
}
for i := 0; i < *cores-1; i++ {
go func(){wg.Add(1); processServer(intervalTree, process, buffer); wg.Done()}
}
@axman6
axman6 / gist:1074170
Created July 10, 2011 02:17
Binary tree that allows lazy reading from disk (if used with bytestring-mmap)
data BTree a
= Null
| Leaf a
| Node a (BTree a) (BTree a)
deriving (Show)
-- makeTree :: [a] -> BTree a
-- makeTree xs =
instance Binary a => Binary (BTree a) where