-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcondi.hs
51 lines (43 loc) · 1.72 KB
/
condi.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
{-# LANGUAGE OverloadedStrings #-}
-- 03. Conduitでコンソールアプリケーション
-- https://sites.google.com/site/toriaezuzakki/haskell/conduit-console
-- Data.Conduit
-- https://hackage.haskell.org/package/conduit-1.3.4.3/docs/Data-Conduit.html
-- Data.Conduit.Lift
-- https://hackage.haskell.org/package/conduit-1.3.4.3/docs/Data-Conduit-Lift.html
import Text.Read(readMaybe)
import Data.Conduit
import qualified Data.Conduit.Combinators as C
import Data.Text
import Data.Text.Lazy (toStrict)
import Data.Text.Lazy.Builder
import Data.Text.Lazy.Builder.Int
import Control.Applicative
import Control.Monad.Trans.Maybe(MaybeT)
import Data.Conduit.Lift
awaitInt :: Monad m => ConduitT Text Text (MaybeT m) Int
awaitInt = maybeC await >>= go
where
go t = case (readMaybe. unpack $t:: Maybe Int) of
Just i -> return i
Nothing -> yield "数字ではありません。再入力してください。" >> awaitInt
awaitIntDouble :: Monad m => ConduitT Text Text (MaybeT m) (Int, Int)
awaitIntDouble = (,) <$>
(yield "1番目の数値を入力してください。" *> awaitInt) <*>
(yield "2番目の数値を入力してください。" *> awaitInt)
appConduit :: Monad m => ConduitT Text Text m ()
appConduit = runMaybeC awaitIntDouble >>= maybe (yield "END") go
where
go (fst,snd) = (yield. toStrict. toLazyText $
(decimal $ fst) <> fromString "+" <>
(decimal $ snd) <> fromString "の答えは" <>
(decimal $ (fst) + (snd)) <> fromString "です。")
>> appConduit
main :: IO ()
main =runConduit $ C.stdin
.| C.decodeUtf8
.| C.linesUnbounded
.| appConduit
.| C.unlines
.| C.encodeUtf8
.| C.stdout