12/04/07 21:58:48.39
>>22
> オブジェクト指向で枠組み設計を行い
> 関数型言語でその枠組みの中身を書く
こんな感じ?
{-# LANGUAGE RecordWildCards #-}
import System
import Control.Monad
data Filter a b = Filter { begin :: a, body :: a -> String -> b, end :: b -> IO () }
awkLike Filter{..} = end . body begin
countFilter = Filter {
begin = Nothing,
body = \_ -> maximum . map length . lines,
end = \p -> putStrLn $ "最大文字数は" ++ show p ++ "文字でした"}
splitBy _ [] = []
splitBy p xs = a : (splitBy p $ drop 1 b) where (a, b) = break p xs
countFilterSplitBy sep filepath = countFilter {
body = \_ -> maximum . map length . splitBy (== sep),
end = \p -> writeFile filepath $ "最大文字数は" ++ show p ++ "文字でした"
}
main' = awkLike countFilter =<< getContents -- 例1: 標準入出力使用、普通にカウント
main = mapM_ (awkLike (countFilterSplitBy ',' "test.txt") <=< readFile) =<< getArgs -- 改行コードを変えて入出力先も変更