public
Created

hashtags in Haskell

  • Download Gist
Hashtags.hs
Haskell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
import Data.Char (isAlpha)
import Text.Parsec
 
-- | Parses exactly one hashtag (and nothing else).
hashtag = do
char '#'
many letter
 
-- | Parse hashtags from a string (see the previous version for
-- a list-based version).
hashtags :: String -> [String]
hashtags s = always [] parsed
where parsed = parse (sepBy hashtag noTag) "hashtags" s
 
-- | Parse many things that aren't '#'.
noTag = many $ noneOf "#"
 
-- | When the Either is a Left, always return x. Otherwise, simply
-- unwrap the Right value.
always :: c -> Either a c -> c
always x = either (const x) id

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.