Skip to content

Instantly share code, notes, and snippets.

@h-hirai
h-hirai / httpserv.rb
Created August 28, 2009 05:51
Starting Webrick
#! /usr/bin/ruby
require 'webrick'
mimetypes = WEBrick::HTTPUtils::DefaultMimeTypes
mimetypes["js"] = "application/x-javascript"
mimetypes["svg"] = "image/svg+xml"
s = WEBrick::HTTPServer.new(:Port => 8000,
:DocumentRoot => Dir::pwd,
@h-hirai
h-hirai / .emacs.el
Last active September 4, 2018 04:17
My .emacs
;; ~/.emacs.d/init.el
;; basic editing
(defun scroll-n-lines-ahead (&optional n)
"Scroll ahead N lines (1 by default)."
(interactive "P")
(scroll-up (prefix-numeric-value n)))
(defun scroll-n-lines-behind (&optional n)
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 ++
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
@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
@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: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: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..]
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 / 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