Brace expansion: Difference between revisions
→{{header|Haskell}}: Enabled compilation by adding FlexibleContexts. Applied hlint hindent. Pruned redundant imports. Simplified test.
(→{{header|Haskell}}: Enabled compilation by adding FlexibleContexts. Applied hlint hindent. Pruned redundant imports. Simplified test.) |
|||
Line 920:
[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 Text.Parsec
parser :: Parsec String u [String]
parser =
alt1 =
alt = expand <$> many (try alts <|> try alt1 <|> escape <|> pure . pure <$> noneOf ",}")▼
(: []) . ('{' :) . (++ "}") <$>
escape = pure <$> sequence [char '\\', anyChar]▼
between (char
alt =
p `sepBy2` sep = liftA2 (:) p (many1 (sep >> p))▼
expand <$>
expand = foldr ((<*>) . fmap (++)) [""]
showExpansion :: String -> String
showExpansion =
(++) <*> (("\n-->\n" ++) . either show unlines . parse parser [])
main :: IO ()
main =
mapM_
(putStrLn . showExpansion)
[ "~/{Downloads,Pictures}/*.{jpg,gif,png}"▼
, "It{{em,alic}iz,erat}e{d,}, please."
, "{,{,gotta have{ ,\\, again\\, }}more }cowbell!"
, "{}} some {\\\\{edge,edgy} }{ cases, here\\\\\\\\\\}"▼
]</lang>
{{out}}
<pre>~/{Downloads,Pictures}/*.{jpg,gif,png}
-->
▲~/{Downloads,Pictures}/*.{jpg,gif,png}
~/Downloads/*.jpg
~/Downloads/*.gif
Line 945 ⟶ 961:
~/Pictures/*.gif
~/Pictures/*.png
It{{em,alic}iz,erat}e{d,}, please.
-->
Itemized, please.
Itemize, please.
Line 952 ⟶ 970:
Iterated, please.
Iterate, please.
{,{,gotta have{ ,\, again\, }}more }cowbell!
-->
cowbell!
more cowbell!
gotta have more cowbell!
gotta have\, again\, more cowbell!
▲{}} some {\\{edge,edgy} }{ cases, here\\\}
{}} some {\\{edge,edgy} }{ cases, here\\\\\}
-->
{}} some {\\
{}} some {\\edgy }{ cases, here\\\\\}</pre>
=={{header|Go}}==
|