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> |
<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 = |
|||
expand <$> many (try alts <|> try alt1 <|> escape <|> pure . pure <$> anyChar) |
|||
where |
|||
alts = concat <$> between (char '{') (char '}') (alt `sepBy2` char ',') |
|||
alts = concat <$> between (char '{') (char '}') (alt `sepBy2` char ',') |
|||
alt1 = |
|||
⚫ | |||
(: []) . ('{' :) . (++ "}") <$> |
|||
⚫ | |||
between (char '{') (char '}') (many $ noneOf ",{}") |
|||
alt = |
|||
⚫ | |||
expand <$> |
|||
⚫ | |||
⚫ | |||
expand = foldr ((<*>) . fmap (++)) [""] |
|||
⚫ | |||
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) |
|||
⚫ | |||
, "It{{em,alic}iz,erat}e{d,}, please." |
|||
, "{,{,gotta have{ ,\\, again\\, }}more }cowbell!" |
|||
⚫ | |||
]</lang> |
|||
{{out}} |
{{out}} |
||
<pre>~/{Downloads,Pictures}/*.{jpg,gif,png} |
|||
<pre>$ ./bracex |
|||
--> |
|||
⚫ | |||
~/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 }{ cases, here\\\} |
{}} some {\\{edge,edgy} }{ cases, here\\\\\} |
||
--> |
|||
{}} some {\\ |
{}} 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}}== |