Skip to content

Instantly share code, notes, and snippets.

View willnet's full-sized avatar
👶
parenting

Shinichi Maeshima willnet

👶
parenting
View GitHub Profile
@willnet
willnet / random_order.rb
Created February 23, 2012 02:46
monkey patch to add random_order to ActiveRecord with multi DB driver
# for rails 3 or higher
module ActiveRecord
module Querying
delegate :random_order, :to => :scoped
end
module QueryMethods
def random_order
relation = clone
@willnet
willnet / gist:2042787
Created March 15, 2012 07:51
RSpec custom matcher's
RSpec::Matchers.define :have_json do |hash|
match do |response|
hash.all? do |k,v|
JSON.parse(response.body)[k] == v
end
end
failure_message_for_should do |response|
body = JSON.parse(response.body)
"expected that #{body} would have #{hash}"
@willnet
willnet / amida.rb
Created June 2, 2012 05:10
minato-amida
#!/usr/bin/env ruby
number = ARGV[0]
number = number.to_i
raise if number == 0
ary = []
9.times do
@willnet
willnet / type.hs
Created July 28, 2012 06:10
learn you a haskell for great good
data Shape = Circle Float Float Float | Rectangle Float Float Float Float
@willnet
willnet / fizzbuzz.hs
Created August 7, 2012 11:39
fizzbuzz
fizzbuzz :: Int -> String
fizzbuzz n
| n `mod` 15 == 0 = "fizzbuzz"
| n `mod` 3 == 0 = "fizz"
| n `mod` 5 == 0 = "buzz"
| otherwise = show n
main = do
mapM_ putStr $ map (++ ", ") (take 100 $ map fizzbuzz [1..])
-- http://www.rubyquiz.com/quiz93.html
import Data.Char
checkHappy :: Int -> Bool
checkHappy n = checkHappy' [n]
checkHappy' :: [Int] -> Bool
checkHappy' list
| firstInput == 1 = True
| firstInput == nextValue = False
| otherwise = checkHappy' (nextValue:list)
-- http://www.haskell.org/haskellwiki/99_questions/54A_to_60
-- Problem 55
data Tree a = Empty | Branch a (Tree a) (Tree a) deriving (Show, Eq)
leaf x = Branch x Empty Empty
cbalTree :: Int -> [Tree Char]
cbalTree 0 = [Empty]
cbalTree n = let (q, r) = (n - 1) `quotRem` 2
in [Branch 'x' left right | i <- [q..(q+r)]
, left <- cbalTree i
@willnet
willnet / poker.hs
Created August 21, 2012 12:02
http://qiita.com/items/cbc3af152ee3f50a822f の問題をhaskellで解いてみた
import Data.List
import Test.HUnit
poker :: String -> String
poker = check . split
split :: String -> [(String, String)]
split ('D':'1':'0':xs) = [("D", "10")] ++ split xs
split ('C':'1':'0':xs) = [("C", "10")] ++ split xs
split ('S':'1':'0':xs) = [("S", "10")] ++ split xs
@willnet
willnet / turn.hs
Created September 4, 2012 07:40
http://qiita.com/items/9d80de41903775296ca6 の問題を haskell で解いてみた
import Data.Char
import Test.HUnit
import System.Environment
data Image = Image Int String
instance Show Image where
show (Image l d) = "Image " ++ [(intToDigit l)] ++ ":" ++ (apply $ appendZero d)
appendZero :: String -> String
@willnet
willnet / baseball.hs
Created September 4, 2012 12:49
http://qiita.com/items/ebd8a56b41711ba459f9 の問題をhaskellで解いてみた
import Data.Char
import System.Environment
main = do
args <- getArgs
print . process. head $ args
process = foldl (\acc x -> cal acc x) []
cal acc x = acc ++ [r x]