mirror of
https://github.com/composewell/streamly.git
synced 2024-09-19 07:29:02 +03:00
Add camel case conversion example
This commit is contained in:
parent
1266a13e0e
commit
9714d362ca
32
examples/CamelCase.hs
Normal file
32
examples/CamelCase.hs
Normal file
@ -0,0 +1,32 @@
|
||||
-- Convert the input file to camel case and write to stdout
|
||||
|
||||
import Data.Maybe (fromJust, isJust)
|
||||
import System.Environment (getArgs)
|
||||
import System.IO (Handle, IOMode(..), openFile, stdout)
|
||||
|
||||
import qualified Streamly.Prelude as S
|
||||
import qualified Streamly.Internal.FileSystem.Handle as FH
|
||||
|
||||
camelCase :: Handle -> Handle -> IO ()
|
||||
camelCase src dst =
|
||||
FH.fromStream dst
|
||||
$ S.map fromJust
|
||||
$ S.filter isJust
|
||||
$ S.map snd
|
||||
$ S.scanl' step (True, Nothing)
|
||||
$ FH.toStream src
|
||||
|
||||
where
|
||||
|
||||
step (wasSpace, _) x =
|
||||
if x == 0x0a || x >= 0x41 && x <= 0x5a
|
||||
then (False, Just x)
|
||||
else if x >= 0x61 && x <= 0x7a
|
||||
then (False, Just $ if wasSpace then x - 32 else x)
|
||||
else (True, Nothing)
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
name <- fmap head getArgs
|
||||
src <- openFile name ReadMode
|
||||
camelCase src stdout
|
Loading…
Reference in New Issue
Block a user