Created
February 10, 2016 00:17
-
-
Save yoshitsugu/b36fe9130958fac26652 to your computer and use it in GitHub Desktop.
ruby chunk (http://ruby-doc.org/core-2.1.0/Enumerable.html#method-i-chunk) in Haskell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
chunk :: (Eq b) => [a] -> (a -> b) -> [(b, [a])] | |
chunk [] _ = [] | |
chunk (x:xs) f = let fx = f x | |
(h, t) = span ((==) fx . f) xs | |
in [(fx,(x:h))] ++ chunk t f | |
data ChunkType = Word | Blank deriving (Show, Eq) | |
chunkByBlank :: Char -> ChunkType | |
chunkByBlank ' ' = Blank | |
chunkByBlank _ = Word | |
main = putStrLn . show $ chunk "Haskell is awesome!!" chunkByBlank | |
-- => [(Word,"Haskell"),(Blank," "),(Word,"is"),(Blank," "),(Word,"awesome!!")] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment