Last active
December 10, 2015 21:59
-
-
Save nabeken/4499259 to your computer and use it in GitHub Desktop.
初haskellなのでこんな感じで…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {-- | |
| - catコマンドみたいに動く。 | |
| - コマンドライン引数を取り、引数が空なら標準入力をそのまま出力する | |
| - それ以外なら引数をファイル名と解釈し、中身をそのまま出力する | |
| -} | |
| import System.IO | |
| import System.Environment | |
| import Control.Applicative | |
| import Control.Monad | |
| import Control.Exception | |
| -- http://d.hatena.ne.jp/kazu-yamamoto/20120605/1338871044 | |
| ioHandler :: IOException -> IO String | |
| ioHandler e = do | |
| hPutStr stderr $ show e ++ "\n" | |
| return "" | |
| cat :: [FilePath] -> IO String | |
| cat [] = do getContents | |
| cat files = do concat <$> forM files (\f -> handle ioHandler $ readFile f) | |
| main = getArgs >>= cat >>= putStr |
Author
Author
本物のcatコマンドは複数のファイル名が与えられた時、ファイルが存在していない場合はそのファイルのみを無視し、継続するようになっている。
cat.hsでは1つでもファイルが存在していないとcatされないことに気がついた。エラーがあった場合はメッセージを出して継続させてみる。
Author
>>= を使うと do記法で <- でいちいち束縛する必要がなくなった。しかし、この記法ではエラー処理を入れるのが面倒か?
Author
Control.Exceptionのhandleを使ってIOExceptionを捕捉するようにしてみた。これでcatの動作に近い。
エラーメッセージは標準エラー出力に出したい…
Author
できた。次は -n で行数を出してみたい。
Author
本物のcatはエラーメッセージは都度表示される。この実装だとreadFile時にエラーがすべて拾われてしまうので、エラーメッセージが最初に固まって表示される。 concat 時に拾えばいいんかな。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
アプリカティブスタイルで書いてみた。一々 <- しているのがちょっと気になっている。