]> gitweb @ CieloNegro.org - Rakka.git/blob - Rakka/Utils.hs
Many improvements
[Rakka.git] / Rakka / Utils.hs
1 module Rakka.Utils
2     ( yesOrNo
3     , parseYesOrNo
4     , maybeA
5     , deleteIfEmpty
6     , chomp
7     )
8     where
9
10 import           Control.Arrow
11 import           Control.Arrow.ArrowList
12
13
14 yesOrNo :: Bool -> String
15 yesOrNo True  = "yes"
16 yesOrNo False = "no"
17
18
19 parseYesOrNo :: ArrowChoice a => a String Bool
20 parseYesOrNo 
21     = proc str -> do case str of
22                        "yes" -> returnA -< True
23                        "no"  -> returnA -< False
24                        _     -> returnA -< error ("Expected yes or no: " ++ str)
25
26
27 maybeA :: (ArrowList a, ArrowChoice a) => a b c -> a b (Maybe c)
28 maybeA a = listA a
29            >>>
30            proc xs -> case xs of
31                         []    -> returnA -< Nothing
32                         (x:_) -> returnA -< Just x
33
34
35 deleteIfEmpty :: (ArrowList a, ArrowChoice a) => a String String
36 deleteIfEmpty
37     = proc str -> do case str of
38                        "" -> none    -< ()
39                        _  -> returnA -< str
40
41
42 chomp :: String -> String
43 chomp = reverse . snd . break (/= '\n') . reverse