on 5054813 help sign in
streamly/examples/CamelCase.hs
33 lines 915 B history raw
1-- ghc -O2 -fspec-constr-recursive=10 -fmax-worker-args=16
2-- Convert the input file to camel case and write to stdout
4import Data.Maybe (fromJust, isJust)
5import System.Environment (getArgs)
6import System.IO (Handle, IOMode(..), openFile, stdout)
8import qualified Streamly.Prelude as S
9import qualified Streamly.Internal.FileSystem.Handle as FH
11camelCase :: Handle -> Handle -> IO ()
12camelCase src dst =
13 FH.fromBytes dst
14 $ S.map fromJust
15 $ S.filter isJust
16 $ S.map snd
17 $ S.scanl' step (True, Nothing)
18 $ FH.toBytes src
20 where
22 step (wasSpace, _) x =
23 if x == 0x0a || x >= 0x41 && x <= 0x5a
24 then (False, Just x)
25 else if x >= 0x61 && x <= 0x7a
26 then (False, Just $ if wasSpace then x - 32 else x)
27 else (True, Nothing)
29main :: IO ()
30main = do
31 name <- fmap head getArgs
32 src <- openFile name ReadMode
33 camelCase src stdout