[Haskell-cafe] [Pipes] Can pipes solve this problem? How?
Daniel Hlynskyi
abcz2.uprola at gmail.com
Wed Aug 15 20:54:16 CEST 2012
Hello Cafe.
Consider code, that takes input from handle until special substring matched:
> matchInf a res s | a `isPrefixOf` s = reverse res
> matchInf a res (c:cs) = matchInf a (c:res) cs
> hTakeWhileNotFound str hdl = hGetContents hdl >>= return.matchInf str []
It is simple, but the handle is closed after running. That is not good,
because I want to reuse this function.
Code can be rewritten without hGetContent, but it is much less
comprehensible:
hTakeWhileNotFound str hdl = fmap reverse$ findStr str hdl [0] []
where
findStr str hdl indeces acc = do
c <- hGetChar hdl
let newIndeces = [ i+1 | i <- indeces, i < length str, str!!i == c]
if length str `elem` newIndeces
then return (c : acc)
else findStr str hdl (0 : newIndeces) (c : acc)
So, the question is - can pipes (any package of them) be the Holy Grail in
this situation, to both keep simple code and better deal with handles (do
not close them specifically)? How?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20120815/1c95c2a8/attachment.htm>
More information about the Haskell-Cafe
mailing list