Brace expansion: Difference between revisions
Content added Content deleted
(Updated D entry) |
|||
Line 660: | Line 660: | ||
[http://www.reddit.com/r/readablecode/comments/1w6exe/p6_crosswalk_braceexpansionparses/cf229at "Here is a direct translation to Haskell using parsec"] (of [http://rosettacode.org/mw/index.php?title=Brace_expansion&oldid=175567#Perl_6 an earlier version of the Perl 6 solution]): |
[http://www.reddit.com/r/readablecode/comments/1w6exe/p6_crosswalk_braceexpansionparses/cf229at "Here is a direct translation to Haskell using parsec"] (of [http://rosettacode.org/mw/index.php?title=Brace_expansion&oldid=175567#Perl_6 an earlier version of the Perl 6 solution]): |
||
<lang haskell>import Control.Applicative (pure, (<$>), (<*>)) |
<lang haskell>import Control.Applicative (liftA2, pure, (<$>), (<*>)) |
||
import Control.Monad (forever) |
import Control.Monad (forever) |
||
import Text.Parsec |
import Text.Parsec |
||
Line 666: | Line 666: | ||
parser :: Parsec String u [String] |
parser :: Parsec String u [String] |
||
parser = expand <$> many (try alts <|> try alt1 <|> escape <|> pure . pure <$> anyChar) |
parser = expand <$> many (try alts <|> try alt1 <|> escape <|> pure . pure <$> anyChar) |
||
where |
|||
where alts = concat <$> between (char '{') (char '}') (alt `sepBy2` char ',') |
|||
alts = concat <$> between (char '{') (char '}') (alt `sepBy2` char ',') |
|||
alt1 = (:[]).('{':).(++"}") <$> between (char '{') (char '}') (many $ noneOf ",{}") |
|||
escape |
alt = expand <$> many (try alts <|> try alt1 <|> escape <|> pure . pure <$> noneOf ",}") |
||
escape = pure <$> sequence [char '\\', anyChar] |
|||
expand = foldr (liftA2 (++)) [""] |
|||
p `sepBy2` sep = liftA2 (:) p (many1 (sep >> p)) |
|||
main :: IO () |
main :: IO () |