Skip to content

Instantly share code, notes, and snippets.

@kei-q
kei-q / roman.rb
Created April 25, 2012 14:44
shinjuku.rbでもくもくと書いてたコード 負の数を入れたら挙動おかしいね
# digit : 一桁の数字
# roman : その桁の1, 5, 10を表す文字三文字 (ex: 'IVX')
def digit2roman(digit, roman)
a, b, c = roman.split(//)
case digit
when 0 then ''
when 1..3 then a * digit
when 4 then a + b
when 5 then b
@kei-q
kei-q / foldl_with_io.lhs
Created May 2, 2012 09:10
foldl (\io_x _ -> do {x <- io_x; putStrLn x; io_x}) (return "go") [1,2,3] でputStrLnが7回実行されるわけ
twitterにあったワンライナーを展開すると以下のようになる
> f = foldl io (return "go") [1,2,3]
> io io_x _ = do
> x <- io_x
> putStrLn x
> io_x
foldlの定義は以下の通り
@kei-q
kei-q / tableview.rb
Created June 9, 2012 09:29
macruby tableview
class AppDelegate
attr_accessor :window
def initialize
@contents = [
{'name' => 'hoge', 'age' => '123'},
{'name' => 'fuga', 'age' => '456'}
]
end
@kei-q
kei-q / README.mkd
Created June 9, 2012 15:57
macrubyでtwitter検索のサンプル(twitter gem使用)
@kei-q
kei-q / amida.hs
Created June 12, 2012 13:33
あみだくじ
import System.Random (randomRIO)
import Data.List (subsequences)
import Control.Applicative ((<$>))
import Control.Monad (replicateM)
main :: IO ()
main = test 4 10
test :: Int -> Int -> IO ()
@kei-q
kei-q / z.zsh
Created July 8, 2012 12:46
zawでzを使うためのsource。半分以上cdrから拝借
if ! (( $+functions[_z] )); then
return
fi
function zaw-src-z() {
# <rank><space><dir> の組でくるので、そこからdirだけ抜き取る
: ${(A)candidates::=${${(f)"$(z -l | sort -n -r)"}##<->(.<->)# ##}}
actions=(zaw-src-z-cd)
act_descriptions=("cd")
}
local ztarget
z-recommend() {
local datafile="${_Z_DATA:-$HOME/.z}"
LBUFFER+=$KEYS
fnd=$LBUFFER
ztarget="$(while read line; do
[ -d "${line%%\|*}" ] && echo $line
done < "$datafile" | awk -v t="$(date +%s)" -v list="$list" -v typ="$typ" -v q="$fnd" -F"|" '
function frecent(rank, time) {
dbname = "hoge"
access' action = do
pipe <- DB.runIOE $ DB.connect (DB.host "localhost")
DB.access pipe DB.master dbname action
mapFn = Bson.Javascript [] "function() {for(i in this.category) {emit(this.category[i], 1);}}"
reduceFn = Bson.Javascript [] "function(p, cur) {var c=0; for(i in cur){c+= cur[i];}; return c;}"
mapReduce = DB.mapReduce "Pkg" mapFn reduceFn
@kei-q
kei-q / calc.hs
Created August 14, 2012 15:59
10.1
import Control.Monad (mplus)
data Token = TInt Int
| Operator (Int -> Int -> Int)
sample :: String
sample = "10 4 3 + 2 * -"
calc :: String -> Int
calc = calc' [] . tokenize
@kei-q
kei-q / route.hs
Created August 14, 2012 16:00
10.2
type Point = (Int, Int)
type Section = (Int, Int, Int)
data Label = A | B | C deriving Show
data Route = Route { path :: [(Label, Int)], distance :: Int } deriving Show
input :: [Section]
input = [(50, 10, 30), (5, 90, 20), (40, 2, 25), (10, 8, 0)]
zeroRoute :: Route
zeroRoute = Route { path = [], distance = 0 }