Brace expansion: Difference between revisions

Content added Content deleted
(Updated D entry)
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]):
 
<lang haskell>import Control.Applicative (liftA2, pure, (<$>), (<*>))
import Control.Monad (forever)
import Text.Parsec
Line 666:
parser :: Parsec String u [String]
parser = expand <$> many (try alts <|> try alt1 <|> escape <|> pure . pure <$> anyChar)
where
where alts = concat <$> between (char '{') (char '}') (alt `sepBy2` char ',')
alt1alts = (\s -> ["{" ++ s ++ "}"])concat <$> between (char '{') (char '}') (manyalt $`sepBy2` noneOfchar "',{}"')
altalt1 = expand(:[]).('{':).(++"}") <$> manybetween (trychar alts'{') <|>(char try'}') alt1(many <|> escape <|> pure . pure <$> noneOf ",{}")
alt = expand <$> many (try alts <|> try alt1 <|> escape =<|> pure . pure <$> sequencenoneOf [char '\\'", anyChar]}")
expandescape = foldr (\x xs -> (++)pure <$> xsequence <*>[char xs)'\\', [""anyChar]
expand = pfoldr `sepBy2` sep =(liftA2 (:++)) <$> p <*> many1 (sep >> p)[""]
p `sepBy2` sep = liftA2 (:) p (many1 (sep >> p))
 
main :: IO ()