Skip to content

Instantly share code, notes, and snippets.

View fujimura's full-sized avatar

Daisuke Fujimura fujimura

View GitHub Profile
def json_response
json_with_indifferent_access JSON.parse response.body
end
def json_with_indifferent_access(json)
traverse_json(json) {|j| j.is_a?(Hash) ? j.with_indifferent_access : j }
end
def traverse_json(json, &block)
import Data.List
main = IO ()
main = do
f ["one"]
f ["one", "two", "three"]
f ["ein", "zwei", "drei"]
f []
f :: [String] -> IO ()
@fujimura
fujimura / runwithstdin.hs
Created October 27, 2015 14:27
Run program with mocked stdin
import Control.Exception
import GHC.IO.Handle
import System.IO
import System.IO.Temp
main :: IO ()
main = do
runWithStdin "Ornette Coleman Trio\nAt the Golden Circle Stockholm" $ do
getLine >>= putStrLn
getLine >>= putStrLn
@fujimura
fujimura / a.hs
Last active October 27, 2015 13:12
Interact with stdin
import System.IO
import System.IO.Temp
main :: IO ()
main = do
withSystemTempFile "uuu" $ \path handle -> do
hPutStrLn handle "hello\nbye"
hClose handle
handle' <- openFile path ReadMode
go handle' stdout
@fujimura
fujimura / interactiveProcess.hs
Created October 27, 2015 10:34
Interactive process in haskell
import System.Process
import System.IO
main :: IO ()
main = do
(Just hi, Just ho, _, ph) <- createProcess (proc "ruby" ["-e", "f = gets; puts f "]) { std_in = CreatePipe, std_out = CreatePipe }
hPutStrLn hi "foobar"
hGetLine ho >>= print
@fujimura
fujimura / post-checkout
Created January 6, 2015 02:49
post-checkout hook to prevent from checking out master
#! /usr/bin/env ruby
class String
# colorization
def colorize(color_code)
"\e[#{color_code}m#{self}\e[0m"
end
def red
colorize(31)
@fujimura
fujimura / cabal-sandbox-user-install.sh
Last active August 29, 2015 14:12
cabal-sandbox-user-install.sh
#! /bin/sh
# Original idea came from http://maoe.hatenadiary.jp/entry/2014/12/25/064900
hash $1 2>/dev/null || {
cabal get $1
r=`ls | grep $1 | head -1`
cd $r
cabal sandbox init
cabal install -j --bindir ~/.cabal/bin --datadir ~/.cabal/share
ubuntu@box69:~/hi$ hpc-coveralls spec --display-report -p --exclude-dir=test
{"source_files":[{"coverage":[null,null,null,null,null,null,null,null,null,null,null,null,0,null,null,null,2,null,null,0,0,0,0,0,null,null,0,0,0,0,0,null,null,0,0,0],"name":"dist/build/autogen/Paths_hi.hs","source":"module Paths_hi (\n version,\n getBinDir, getLibDir, getDataDir, getLibexecDir,\n getDataFileName, getSysconfDir\n ) where\n\nimpor
t qualified Control.Exception as Exception\nimport Data.Version (Version(..))\nimport System.Environment (getEnv)\nimport Prelude\n\ncatchIO :: IO a -> (Exception.IOException -> IO a) -> IO a\ncatchIO = Exception.catch\n\n\nversion :: Version\nversion = Version {versionBranch = [1,0,0,0], versionTags = []}\nbindir, libdir, datadir, libexecdir, sysconfdir
:: FilePath\n\nbindir = \"/home/ubuntu/hi/.cabal-sandbox/bin\"\nlibdir = \"/home/ubuntu/hi/.cabal-sandbox/lib/x86_64-lin
@fujimura
fujimura / guess.hs
Created December 13, 2014 11:28
Compare "Guessing Game" in The Rust Guide http://doc.rust-lang.org/guide.html#guessing-game
import Control.Applicative
import Control.Exception
import System.Random
import Text.Read
main :: IO ()
main = go
go :: IO ()
go = do
module Api
def self.config
@@_config ||= OpenStruct.new
end
def self.configure
yield self.config
end
end