{"id":143,"date":"2016-01-05T18:44:46","date_gmt":"2016-01-05T09:44:46","guid":{"rendered":"http:\/\/mitsuji.org\/?p=143"},"modified":"2016-01-05T18:59:10","modified_gmt":"2016-01-05T09:59:10","slug":"reader%e3%83%a2%e3%83%8a%e3%83%89%e3%81%ae%e4%bd%bf%e3%81%84%e9%81%93-%e4%bb%ae%e6%83%b3%e3%82%b0%e3%83%ad%e3%83%bc%e3%83%90%e3%83%ab%e5%a4%89%e6%95%b0","status":"publish","type":"post","link":"https:\/\/mitsuji.org\/?p=143","title":{"rendered":"Reader\u30e2\u30ca\u30c9\u306e\u4f7f\u3044\u9053 \u4eee\u60f3\u30b0\u30ed\u30fc\u30d0\u30eb\u5909\u6570"},"content":{"rendered":"<p>\u73fe\u5b9f\u7684\u304b\u3069\u3046\u304b\u306f\u3055\u3066\u304a\u304d\u3001\u7a0e\u8fbc\u307f\u4fa1\u683c\u306e\u5546\u54c1\u3068\u7a0e\u629c\u304d\u4fa1\u683c\u306e\u5546\u54c1\u304c\u6df7\u5728\u3057\u3066\u3044\u308b\u5e97\u3092\u8003\u3048\u3066\u307f\u3088\u3046\u3002<br \/>\n\u4f1a\u8a08\u306e\u90fd\u5408\u4e0a\u3001\u8cfc\u5165\u91d1\u984d\u306e\u5408\u8a08\u3092\u8a08\u7b97\u3059\u308b\u3068\u304d\u306f\u3001\u4e00\u65e6\u3059\u3079\u3066\u306e\u5358\u4fa1\u3092\u7a0e\u629c\u304d\u306b\u63c3\u3048\u3066\u304b\u3089\u96c6\u8a08\u3057\u3001\u307e\u3068\u3081\u3066\u7a0e\u984d\u3092\u8a08\u7b97\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3082\u306e\u3068\u3059\u308b\u3002<\/p>\n<p>\u5408\u8a08\u3092\u8a08\u7b97\u3059\u308b\u30b3\u30fc\u30c9\u306f\u4e0b\u8a18\u306e\u3088\u3046\u306b\u306a\u308a\u305d\u3046\u3060\u3002<br \/>\n\u3053\u306e\u30b3\u30fc\u30c9\u3092\u773a\u3081\u3066\u3044\u308b\u3068\u3001toTaxIncluded \u3068 fromTaxIncluded \u306e \u5f15\u6570 taxRate \u3092\u6d88\u3057\u305f\u304f\u306a\u3063\u3066\u304f\u308b\u3060\u308d\u3046\u3002<br \/>\n\u307b\u307c\u5b9a\u6570\u306e\u3088\u3046\u306a\u3082\u306e\u3067\u3042\u308a\u3001\u5408\u8a08\u306e\u8a08\u7b97\u3068\u306f\u672c\u8cea\u7684\u306b\u95a2\u4fc2\u306e\u306a\u3044\u8981\u7d20\u3067\u3042\u308b\u305f\u3081\u3060\u3002<\/p>\n<pre class=\"lang:haskell\">\nmain = do\n  print $ total  0.08 [(108,True,2),(200,False,1),(324,True,2),(400,False,1)]\n\n\ntoTaxIncluded :: Float -> Float -> Float\ntoTaxIncluded taxRate price = price * (1.00 + taxRate)\n\nfromTaxIncluded :: Float -> Float -> Float\nfromTaxIncluded taxRate tiPrice = tiPrice \/ (1.00 + taxRate)\n\ntotal :: Float -> [(Float,Bool,Int)] -> Float\ntotal taxRate xs =\n  let subTotal = foldl' step 0.0 xs\n  in toTaxIncluded taxRate subTotal\n  where\n    step acc (price,taxin,quan) = acc + (unitPrice taxin price) * (fromIntegral quan)\n    unitPrice taxin = if taxin\n                      then fromTaxIncluded taxRate\n                      else id\n\n\n<\/pre>\n<p>\u305d\u3053\u3067\u30b3\u30fc\u30c9\u3092\u4e0b\u8a18\u306e\u3088\u3046\u306b\u6539\u5584\u3057\u3066\u307f\u3088\u3046\u3002taxRate \u304c\u5f15\u6570\u304b\u3089\u6d88\u3048\u3066\u3059\u3063\u304d\u308a\u3057\u305f\u3002<br \/>\nHaskell \u306e\u30b3\u30fc\u30c9\u3092\u30b9\u30af\u30ea\u30d7\u30c8\u3068\u3057\u3066\u4f7f\u7528\u3057\u3066\u3044\u308b\u5834\u5408\u306f\u3053\u308c\u3067\u5341\u5206\u3060\u308d\u3046\u3002<br \/>\n\u305f\u3060\u3001\u4e00\u65e6\u30b3\u30f3\u30d1\u30a4\u30eb\u3055\u308c\u3066\u3057\u307e\u3046\u3068\u3001toTaxIncludedG \u3068 fromTaxIncludedG \u304c\u4f7f\u7528\u3059\u308b\u7a0e\u7387\u306f\u56fa\u5b9a\u3055\u308c\u3066\u3057\u307e\u3046\u3002<\/p>\n<pre class=\"lang:haskell\">\nmain = do\n  print $ totalG      [(108,True,2),(200,False,1),(324,True,2),(400,False,1)]\n\n\ntaxRateG :: Float\ntaxRateG = 0.08\n\ntoTaxIncludedG :: Float -> Float\ntoTaxIncludedG price = price * (1.00 + taxRateG)\n\nfromTaxIncludedG :: Float -> Float\nfromTaxIncludedG tiPrice = tiPrice \/ (1.00 + taxRateG)\n\ntotalG :: [(Float,Bool,Int)] -> Float\ntotalG xs =\n  let subTotal = foldl' step 0.0 xs\n  in toTaxIncludedG subTotal\n  where\n    step acc (price,taxin,quan) = acc + (unitPrice taxin price) * (fromIntegral quan)\n    unitPrice taxin = if taxin\n                      then fromTaxIncludedG\n                      else id\n\n\n<\/pre>\n<p>\u305d\u3053\u3067Reader\u30e2\u30ca\u30c9\u767b\u5834\u3002Reader\u30e2\u30ca\u30c9\u3092\u4f7f\u7528\u3059\u308b\u3068\u3001\u624b\u7d9a\u304d\u578b\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u3067\u30b0\u30ed\u30fc\u30d0\u30eb\u5909\u6570\u3068\u3057\u3066\u4fdd\u6301\u3057\u305f\u3044\u3088\u3046\u306a\u8981\u7d20\u3092\u3001\u81ea\u7136\u306b\u4fdd\u6301\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3002<br \/>\nrunReader\u95a2\u6570 \u306e \u7b2c2\u5f15\u6570\u3067\u6307\u5b9a\u3057\u305f\u5024\u3092\u3001\u30e2\u30ca\u30c9\u5185\u306e\u4efb\u610f\u306e\u95a2\u6570\u5185\u3067 ask \u95a2\u6570\u3092\u4f7f\u7528\u3057\u3066\u53d6\u308a\u51fa\u3059\u3053\u3068\u304c\u3067\u304d\u308b\u3002<\/p>\n<pre class=\"lang:haskell\">\nmain = do\n  print $ totalR 0.08 [(108,True,2),(200,False,1),(324,True,2),(400,False,1)]\n\n\n\ntoTaxIncludedR :: Float -> Reader Float Float\ntoTaxIncludedR price = do\n  taxRate <- ask\n  return $ price * (1.00 + taxRate)\n\nfromTaxIncludedR :: Float -> Reader Float Float\nfromTaxIncludedR tiPrice = do\n  taxRate <- ask\n  return $ tiPrice \/ (1.00 + taxRate)\n  \ntotalR :: Float -> [(Float,Bool,Int)] -> Float\ntotalR taxRate xs = (`runReader` taxRate) $ do\n  subTotal <- foldM step 0.0 xs\n  toTaxIncludedR subTotal\n  where\n    step :: Float -> (Float,Bool,Int) -> Reader Float Float\n    step acc (price,taxin,quan) = do\n      up <- unitPrice taxin price\n      return $ acc + up * (fromIntegral quan)\n  \n    unitPrice taxin = if taxin\n                      then fromTaxIncludedR\n                      else return\n\n\n<\/pre>\n<p>\u5168\u90e8\u306e\u305b\u3066\u304a\u304f\u3002<\/p>\n<pre class=\"lang:haskell\">\nimport Data.List (foldl')\nimport Control.Monad.Reader (Reader,runReader,ask)\nimport Control.Monad (foldM)\n\n\nmain = do\n  print $ total  0.08 [(108,True,2),(200,False,1),(324,True,2),(400,False,1)]\n  print $ totalG      [(108,True,2),(200,False,1),(324,True,2),(400,False,1)]\n  print $ totalR 0.08 [(108,True,2),(200,False,1),(324,True,2),(400,False,1)]\n\n\n\ntoTaxIncluded :: Float -> Float -> Float\ntoTaxIncluded taxRate price = price * (1.00 + taxRate)\n\nfromTaxIncluded :: Float -> Float -> Float\nfromTaxIncluded taxRate tiPrice = tiPrice \/ (1.00 + taxRate)\n\ntotal :: Float -> [(Float,Bool,Int)] -> Float\ntotal taxRate xs =\n  let subTotal = foldl' step 0.0 xs\n  in toTaxIncluded taxRate subTotal\n  where\n    step acc (price,taxin,quan) = acc + (unitPrice taxin price) * (fromIntegral quan)\n    unitPrice taxin = if taxin\n                      then fromTaxIncluded taxRate\n                      else id\n\n\n\ntaxRateG :: Float\ntaxRateG = 0.08\n\ntoTaxIncludedG :: Float -> Float\ntoTaxIncludedG price = price * (1.00 + taxRateG)\n\nfromTaxIncludedG :: Float -> Float\nfromTaxIncludedG tiPrice = tiPrice \/ (1.00 + taxRateG)\n\ntotalG :: [(Float,Bool,Int)] -> Float\ntotalG xs =\n  let subTotal = foldl' step 0.0 xs\n  in toTaxIncludedG subTotal\n  where\n    step acc (price,taxin,quan) = acc + (unitPrice taxin price) * (fromIntegral quan)\n    unitPrice taxin = if taxin\n                      then fromTaxIncludedG\n                      else id\n\n\n\ntoTaxIncludedR :: Float -> Reader Float Float\ntoTaxIncludedR price = do\n  taxRate <- ask\n  return $ price * (1.00 + taxRate)\n\nfromTaxIncludedR :: Float -> Reader Float Float\nfromTaxIncludedR tiPrice = do\n  taxRate <- ask\n  return $ tiPrice \/ (1.00 + taxRate)\n  \ntotalR :: Float -> [(Float,Bool,Int)] -> Float\ntotalR taxRate xs = (`runReader` taxRate) $ do\n  subTotal <- foldM step 0.0 xs\n  toTaxIncludedR subTotal\n  where\n    step :: Float -> (Float,Bool,Int) -> Reader Float Float\n    step acc (price,taxin,quan) = do\n      up <- unitPrice taxin price\n      return $ acc + up * (fromIntegral quan)\n  \n    unitPrice taxin = if taxin\n                      then fromTaxIncludedR\n                      else return\n\n\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u73fe\u5b9f\u7684\u304b\u3069\u3046\u304b\u306f\u3055\u3066\u304a\u304d\u3001\u7a0e\u8fbc\u307f\u4fa1\u683c\u306e\u5546\u54c1\u3068\u7a0e\u629c\u304d\u4fa1\u683c\u306e\u5546\u54c1\u304c\u6df7\u5728\u3057\u3066\u3044\u308b\u5e97\u3092\u8003\u3048\u3066\u307f\u3088\u3046\u3002 \u4f1a\u8a08\u306e\u90fd\u5408\u4e0a\u3001\u8cfc\u5165\u91d1\u984d\u306e\u5408\u8a08\u3092\u8a08\u7b97\u3059\u308b\u3068\u304d\u306f\u3001\u4e00\u65e6\u3059\u3079\u3066\u306e\u5358\u4fa1\u3092\u7a0e\u629c\u304d\u306b\u63c3\u3048\u3066\u304b\u3089\u96c6\u8a08\u3057\u3001\u307e\u3068\u3081\u3066\u7a0e\u984d\u3092\u8a08\u7b97\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3082\u306e &hellip; <a href=\"https:\/\/mitsuji.org\/?p=143\" class=\"more-link\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"screen-reader-text\">Reader\u30e2\u30ca\u30c9\u306e\u4f7f\u3044\u9053 \u4eee\u60f3\u30b0\u30ed\u30fc\u30d0\u30eb\u5909\u6570<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[3,5],"_links":{"self":[{"href":"https:\/\/mitsuji.org\/index.php?rest_route=\/wp\/v2\/posts\/143"}],"collection":[{"href":"https:\/\/mitsuji.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mitsuji.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mitsuji.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mitsuji.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=143"}],"version-history":[{"count":5,"href":"https:\/\/mitsuji.org\/index.php?rest_route=\/wp\/v2\/posts\/143\/revisions"}],"predecessor-version":[{"id":148,"href":"https:\/\/mitsuji.org\/index.php?rest_route=\/wp\/v2\/posts\/143\/revisions\/148"}],"wp:attachment":[{"href":"https:\/\/mitsuji.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mitsuji.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mitsuji.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}