Brace expansion: Difference between revisions

Content added Content deleted
(→‎{{header|Haskell}}: Enabled compilation by adding FlexibleContexts. Applied hlint hindent. Pruned redundant imports. Simplified test.)
Line 920: 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]):
[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, (<$>), (<*>))
<lang haskell>{-# LANGUAGE FlexibleContexts #-}

import Control.Monad (forever)
import Text.Parsec
import Text.Parsec


parser :: Parsec String u [String]
parser :: Parsec String u [String]
parser =
parser = expand <$> many (try alts <|> try alt1 <|> escape <|> pure . pure <$> anyChar)
expand <$> many (try alts <|> try alt1 <|> escape <|> pure . pure <$> anyChar)
where
where
alts = concat <$> between (char '{') (char '}') (alt `sepBy2` char ',')
alt1 = (:[]).('{':).(++"}") <$> between (char '{') (char '}') (many $ noneOf ",{}")
alts = concat <$> between (char '{') (char '}') (alt `sepBy2` char ',')
alt1 =
alt = expand <$> many (try alts <|> try alt1 <|> escape <|> pure . pure <$> noneOf ",}")
(: []) . ('{' :) . (++ "}") <$>
escape = pure <$> sequence [char '\\', anyChar]
expand = foldr (liftA2 (++)) [""]
between (char '{') (char '}') (many $ noneOf ",{}")
alt =
p `sepBy2` sep = liftA2 (:) p (many1 (sep >> p))
expand <$>
many (try alts <|> try alt1 <|> escape <|> pure . pure <$> noneOf ",}")
escape = pure <$> sequence [char '\\', anyChar]
expand = foldr ((<*>) . fmap (++)) [""]
p `sepBy2` sep = (:) <$> p <*> many1 (sep >> p)

showExpansion :: String -> String
showExpansion =
(++) <*> (("\n-->\n" ++) . either show unlines . parse parser [])


main :: IO ()
main :: IO ()
main =
main = forever $ parse parser [] <$> getLine >>= either print (mapM_ putStrLn)</lang>
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}}
{{out}}
<pre>~/{Downloads,Pictures}/*.{jpg,gif,png}
<pre>$ ./bracex
-->
~/{Downloads,Pictures}/*.{jpg,gif,png}
~/Downloads/*.jpg
~/Downloads/*.jpg
~/Downloads/*.gif
~/Downloads/*.gif
Line 945: Line 961:
~/Pictures/*.gif
~/Pictures/*.gif
~/Pictures/*.png
~/Pictures/*.png

It{{em,alic}iz,erat}e{d,}, please.
It{{em,alic}iz,erat}e{d,}, please.
-->
Itemized, please.
Itemized, please.
Itemize, please.
Itemize, please.
Line 952: Line 970:
Iterated, please.
Iterated, please.
Iterate, please.
Iterate, please.

{,{,gotta have{ ,\, again\, }}more }cowbell!
{,{,gotta have{ ,\, again\, }}more }cowbell!
-->
cowbell!
cowbell!
more cowbell!
more cowbell!
gotta have more cowbell!
gotta have more cowbell!
gotta have\, again\, more cowbell!
gotta have\, again\, more cowbell!

{}} some {\\{edge,edgy} }{ cases, here\\\}
{}} some {\\edge }{ cases, here\\\}
{}} some {\\{edge,edgy} }{ cases, here\\\\\}
-->
{}} some {\\edgy }{ cases, here\\\}
{}} some {\\edge }{ cases, here\\\\\}
a{b{1,2}c
{}} some {\\edgy }{ cases, here\\\\\}</pre>
a{b1c
a{b2c
a{1,2}b}c
a1b}c
a2b}c
a{1,{2},3}b
a1b
a{2}b
a3b
a{b{1,2}c{}}
a{b1c{}}
a{b2c{}}
^D
bracex: <stdin>: hGetLine: end of file</pre>


=={{header|Go}}==
=={{header|Go}}==