Skip to content

Instantly share code, notes, and snippets.

abstract class MyMaybe[T] {
def flatMap[U](f:T=>MyMaybe[U]):MyMaybe[U]
def map[U](f:T=>U):MyMaybe[U]
}
case class MyNothing[T]() extends MyMaybe[T] {
def flatMap[U](f:T=>MyMaybe[U]):MyMaybe[U] = MyNothing()
def map[U](f:T=>U):MyMaybe[U] = MyNothing()
}
case class MyJust[T](elm:T) extends MyMaybe[T] {
def flatMap[U](f:T=>MyMaybe[U]):MyMaybe[U] = f(elm)
import Test.QuickCheck
listLength :: Float -> Float -> Float -> Int
listLength start end step = 1 + floor ((end - start) / step)
iota :: Float -> Float -> Float -> [Float]
iota start end step = [start,start+step..end]
prop_ListLength :: Float -> Float -> Float -> Property
prop_ListLength start end step =
@h-hirai
h-hirai / bmp-trial.hs
Created April 3, 2012 17:48
tried to use Codec.BMP
import Codec.BMP
import Data.Word (Word8)
import Data.ByteString (pack)
pixcelToFlatByteString height width px = pack $ rep height $ rep width px
where rep n = concat . replicate n
pixcelToPackedRGBA height width px =
packRGBA32ToBMP width height $ pixcelToFlatByteString height width px
import Control.Applicative ((<$>), (*>), (<*))
import Text.Parsec (many1, option, oneOf, spaces, upper, char, digit,
noneOf, parseTest, (<|>))
import Text.Parsec.String (Parser, parseFromFile)
import Data.Char (ord)
import Prelude hiding (sequence, seq)
-- data Collection = Collection [GameTree] deriving Show
data GameTree = GameTree { seq::Sequence, subtrees::[GameTree] } deriving Show
@h-hirai
h-hirai / gist:1175236
Created August 27, 2011 10:43
FizzBuzz
-- http://d.hatena.ne.jp/fumokmm/20110815/1313405510
import Data.List (find)
makeAlist :: [String] -> [(Int, String)]
makeAlist [] = []
makeAlist (a:b:rest) = (read a, b):makeAlist rest
fizzbuzz :: [(Int, String)] -> [String]
fizzbuzz alist = map f [1..]
@h-hirai
h-hirai / gist:1175194
Created August 27, 2011 09:39
practice of using enumerator
{-# LANGUAGE OverloadedStrings #-}
-- http://d.hatena.ne.jp/kazu-yamamoto/20110216/1297845048
import Control.Monad.IO.Class (liftIO)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as B -- for OverloadedString
import Data.Enumerator (($$), (<==<), ($=), (=$))
import qualified Data.Enumerator as E
import Data.Enumerator.Binary as EB
@h-hirai
h-hirai / gist:914825
Created April 12, 2011 02:43
Iteratee implementation example by @tanakh http://d.hatena.ne.jp/tanakh/20100824
-- http://d.hatena.ne.jp/tanakh/20100824
module Iteratee where
import Control.Applicative (Applicative, pure, (<*>))
import Data.Maybe (catMaybes)
import System.IO (Handle, hIsEOF, hGetChar, openFile, hClose)
import System.IO (IOMode (ReadMode))
import Control.Exception (bracket)
import Control.Monad ((>=>))
import Control.Monad.Trans (MonadTrans, lift)
@h-hirai
h-hirai / gist:803247
Created January 30, 2011 21:02
generate RIFF WAVE format data, 440 Hz sin curve to stdout
module Main (main) where
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as C
import Data.Word
import Data.Int
import Data.Bits
class Packable a where
module GCJ (runSolver) where
runSolver :: (Int -> [String] -> [a]) ->
(a -> String) ->
String -> String
runSolver splitter solver s =
let ls = lines s
n = read $ head ls
ins = splitter n $ tail ls
in
data XMLItem = XMLElem String [(String, String)] [XMLItem]
| Text String
attrsShow as = concat [" " ++ k ++ "=\"" ++ v ++ "\"" | (k, v) <- as]
instance Show XMLItem where
show (Text s) = s
show (XMLElem tag as []) = "<" ++ tag ++ attrsShow as ++ " />"
show (XMLElem tag as cs) = "<" ++ tag ++ attrsShow as ++ ">" ++
concatMap show cs ++