
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?