
I've found myself writing code like this several times now. Is there a better way? read_args h = do line <- hGetLine h case line of "." -> return [] ('#':y) -> do ys <- read_args h return (y:ys)

Hi
I've found myself writing code like this several times now. Is there a better way?
hGetContents might be a different way to write a similar thing: read_args h = do src <- hGetContents h let (has,rest) = span ("#" `isPrefixOf`) $ lines src return (map tail has) Of course, depending on exactly the kind of IO control you need to do, it may not work. Thanks Neil

Neil Mitchell wrote:
Hi
I've found myself writing code like this several times now. Is there a better way?
hGetContents might be a different way to write a similar thing:
read_args h = do src <- hGetContents h let (has,rest) = span ("#" `isPrefixOf`) $ lines src return (map tail has)
Of course, depending on exactly the kind of IO control you need to do, it may not work.
Yeah, I was thinking about that. I'm not sure if I could get it to work. It's reading from a network socket, not a file, and I think I might need to be able to explicitly close it. (What happens if the other end closes? Do I get an exception or just the end of the string?) I might possibly be able to just use hGetContents and run a parser lazily over the result, we'll see. I know there's a few monad-related functions that I don't know about. I was curios to see if any of them help in this instance...

Hi Andrew,
I don't know whether it's intentional, but the patterns for "case line of"
are not exaustive. Are you sure you do not expect anything else apart from a
single "." or a line starting with '#'?
More below:
On Mon, May 5, 2008 at 1:45 PM, Andrew Coppin
Neil Mitchell wrote:
hGetContents might be a different way to write a similar thing:
read_args h = do src <- hGetContents h let (has,rest) = span ("#" `isPrefixOf`) $ lines src return (map tail has)
Of course, depending on exactly the kind of IO control you need to do, it may not work.
Please correct me if I am wrong; but the rest of the contents from the handle h will be unavailable after the evaluation of this function: it goes into a semi-closed state. (Correctly so: 'src' is supposed to have the entire contents obtained from h if needed.)
Another minor observation: if the partial pattern in the original code was intentional, then this is not exactly the same. what about read_args' :: [String] -> ([String],[String]) read_args' src = span ("#" `isPrefixOf`) $ lines src and then using s <- hGetContents let (arg, rest) = read_args' $ lines s ... So that you can get both the result and the remaining list of lines, in case you need them. Again, this does not exactly stop where there is a "." on a single line; it stops as soon as it gets a line without a '#'. Abhay

Abhay Parvate wrote:
Hi Andrew,
I don't know whether it's intentional, but the patterns for "case line of" are not exaustive. Are you sure you do not expect anything else apart from a single "." or a line starting with '#'?
It's reading a wire protocol, so if you hit anything else there's been a protocol error. Arguably I should probably catch this case and report it gracefully. But really, there isn't much you could usefully do in this situation.
Please correct me if I am wrong; but the rest of the contents from the handle h will be unavailable after the evaluation of this function: it goes into a semi-closed state. (Correctly so: 'src' is supposed to have the entire contents obtained from h if needed.)
Quite so. If I was going with this approach, I'd have to call hGetContents and then parse the entire stream and act on it. For my current application, which is basically a server that receives requests and then replies to them, I *think* you could possibly do that. If, however, there were to be some command that said "hey, disregard everything I just said, and switch to this new parsing mode..." then I'd have a problem implementing that this way.

Andrew Coppin
Do I get an exception or just the end of the string?) I might possibly be able to just use hGetContents and run a parser lazily over the result, we'll see.
You get an exception. I used this scheme for a toy http server using lazy Bytestrings, and it worked well, even with connection persistence and everything. -- (c) this sig last receiving data processing entity. Inspect headers for past copyright information. All rights reserved. Unauthorised copying, hiring, renting, public performance and/or broadcasting of this signature prohibited.
participants (4)
-
Abhay Parvate
-
Achim Schneider
-
Andrew Coppin
-
Neil Mitchell