Skip to content

Instantly share code, notes, and snippets.

View nnabeyang's full-sized avatar

noriaki watanabe nnabeyang

View GitHub Profile
@nnabeyang
nnabeyang / diff.bash
Created December 3, 2017 12:28
ファイルの最後にヌルバイトを追加する。追加後と追加前で差異が無いかチェックする
#!/bin/bash
ls -l $1 | awk '{print $5,$10}'|
while read n f
do
if [ "$f" != "" ]; then
xxd -p "$2/$f" | fold -w2 | head -n $n | diff -u "$3/$f" -
fi
done
@nnabeyang
nnabeyang / main.js
Last active May 8, 2017 23:32
普通の電卓
//usage: node main.js "14 + 34 * 22 + (3 + 4) * 5"
var util = require('util'); var calc = calc || {};
calc.Parser = function() {
this.tokens = []; this.parens = []; this.pos = 0;
};
calc.Parser.prototype.next = function() {
var offset = this.parens[this.parens.length - 1];
if(offset + this.pos < this.tokens.length) return this.tokens[offset + this.pos++];
else return null;
};
@nnabeyang
nnabeyang / main.rb
Last active May 8, 2017 04:00
逆ポーランド記法の電卓
#usage: ruby main.rb
class Parser
def initialize
@stack = []
end
def parse(line)
scanner = Scanner.new(line)
while (token = scanner.next) do
add_token(token)
end
@nnabeyang
nnabeyang / Guardfile
Last active April 22, 2017 06:28
Ruby on Railsのrails new用のテンプレート
# Guardのマッチング規則を定義
guard :minitest, spring: "bin/rails test", all_on_start: false do
watch(%r{^test/(.*)/?(.*)_test\.rb$})
watch('test/test_helper.rb') { 'test' }
watch('config/routes.rb') { integration_tests }
watch(%r{^app/models/(.*?)\.rb$}) do |matches|
"test/models/#{matches[1]}_test.rb"
end
watch(%r{^app/controllers/(.*?)_controller\.rb$}) do |matches|
resource_tests(matches[1])
type Matrix = [[Int]]
dropAt :: Int -> [a] -> [a]
dropAt i xs = [x| (x, j) <- zip xs [0..], i /= j]
cofactor :: Int -> Int -> Matrix -> Matrix
cofactor i j m = [dropAt j v| (v, k) <- zip m [0..], i /= k]
det :: Matrix -> Int
type Matrix = [[Int]]
dropAt :: Int -> [a] -> [a]
dropAt i xs = as ++ bs where (as, _ :bs) = splitAt i xs
cofactor :: Int -> Int -> Matrix -> Matrix
@nnabeyang
nnabeyang / MyParseLib.lhs
Created January 15, 2014 07:09
IFPHの12章のプログラムの動かし方 ref: http://qiita.com/nnabeyang/items/f01103bc4487a5cc202c
> import Data.Char
> instance Functor Parser where
> -- map f p = MkP g
> fmap f p = MkP g
> where g s = [(f x, s') | (x,s') <- papply p s]
> alphanum :: Parser Char
> -- alphanum = sat isAlphaum
> alphanum = sat isAlphaNum
@nnabeyang
nnabeyang / MyParseLib.lhs
Created January 15, 2014 07:06
IFPHの12章のプログラムを動くように修正したファイル。
> module MyParseLib
> (Parser, mplus, orelse, item, many, some, manywith, somewith,
> sat, char, string, digit, lower, upper, letter, alphanum,
> ident, space, token, symbol, applyParser) where
> import Data.Char
> import Control.Monad
> newtype Parser a = MkP (String -> [(a,String)])
@nnabeyang
nnabeyang / calculator.lhs
Created January 13, 2014 07:13
『プログラミングHaskell』の電卓をWindowsでも動くようにする ref: http://qiita.com/nnabeyang/items/994d4fcf44a10b72445e
> import System.Console.ANSI
>
> cls :: IO ()
> -- cls = putStr "\ESC[2J"
> cls = clearScreen
>
> type Pos = (Int,Int)
>
> goto :: Pos -> IO ()
> --goto (x,y) = putStr ("\ESC[" ++ show y ++ ";" ++ show x ++ "H")
@nnabeyang
nnabeyang / MyParser.hs
Created January 10, 2014 15:47
『プログラミングHaskell』の関数型パーサーをStateTを使って書き直す ref: http://qiita.com/nnabeyang/items/97d67191758dabb50752
eval :: String -> Int
eval xs = case (parse expr xs) of
Just (n, "") -> n
Just (_, out) -> error ("unused input " ++ out)
failure -> error "invalid input"
@nnabeyang
nnabeyang / bodies.yml
Last active December 19, 2015 01:28
ruby on railsでコンポジットパターン
# test/fixtures/bodies.yml
one:
id: 1
file_entry_id: 1
description: desc1
two:
id: 2
file_entry_id: 3
description: description2