Proposal: Add sequenceWhile and SequenceWhile_ to Control.Monad

I'm not sure of the protocol for adding things to existing libraries but I'd like to propose the addition of these two functions to Control.Monad. sequenceWhile_ :: Monad m => (a -> Bool) -> [m a] -> m () sequenceWhile_ _ [] = return () sequenceWhile_ p (x:xs) = x >>= \c -> if (p c) then sequenceWhile_ p xs else return () sequenceWhile :: Monad m => (a -> Bool) -> [m a] -> m [a] sequenceWhile _ [] = return [] sequenceWhile p (x:xs) = do y <- x if (p y) then do ys <- sequenceWhile p xs return (y:ys) else return [y]

Dominic Steinitz wrote:
I'm not sure of the protocol for adding things to existing libraries
http://www.haskell.org/haskellwiki/Library_submissions Though my general preference is to kick around an idea on here for a while first before starting the formal process.
but I'd like to propose the addition of these two functions to Control.Monad.
sequenceWhile_ :: Monad m => (a -> Bool) -> [m a] -> m () sequenceWhile_ _ [] = return () sequenceWhile_ p (x:xs) = x >>= \c -> if (p c) then sequenceWhile_ p xs else return ()
sequenceWhile :: Monad m => (a -> Bool) -> [m a] -> m [a] sequenceWhile _ [] = return [] sequenceWhile p (x:xs) = do y <- x if (p y) then do ys <- sequenceWhile p xs return (y:ys) else return [y]
This last line should probably be return [] for consistency with takeWhile etc. The downside of that is that the monadic effect associating with producing y has been run but then the value is thrown away. It's also a bit specialised for my taste. A better option would be to have a takeWhileM which would allow sequenceWhile_ and sequenceWhile to be built from that and sequence_/sequence. Cheers, Ganesh =============================================================================== Please access the attached hyperlink for an important electronic communications disclaimer: http://www.credit-suisse.com/legal/en/disclaimer_email_ib.html ===============================================================================

Sittampalam, Ganesh wrote:
Dominic Steinitz wrote:
I'm not sure of the protocol for adding things to existing libraries
http://www.haskell.org/haskellwiki/Library_submissions
Though my general preference is to kick around an idea on here for a while first before starting the formal process.
but I'd like to propose the addition of these two functions to Control.Monad.
sequenceWhile_ :: Monad m => (a -> Bool) -> [m a] -> m () sequenceWhile_ _ [] = return () sequenceWhile_ p (x:xs) = x >>= \c -> if (p c) then sequenceWhile_ p xs else return ()
sequenceWhile :: Monad m => (a -> Bool) -> [m a] -> m [a] sequenceWhile _ [] = return [] sequenceWhile p (x:xs) = do y <- x if (p y) then do ys <- sequenceWhile p xs return (y:ys) else return [y]
This last line should probably be return [] for consistency with takeWhile etc. The downside of that is that the monadic effect associating with producing y has been run but then the value is thrown away.
I use sequenceWhile to go through actions and stop at the one that "fails" (in some sense). I like to know what caused the failure so I return [y]. It did occur to me that one could argue that it should return []. But then you know something "failed" but not how.
It's also a bit specialised for my taste. A better option would be to have a takeWhileM which would allow sequenceWhile_ and sequenceWhile to be built from that and sequence_/sequence.
I'd argue that some of the functions in Control.Monad are more specialised. I'm not sure what you had in mind for takeWhileM. Can you give a type signature / behaviour description? I'd be happy with the composition of two library functions *provided* I can get hold of the value of the last run action. Thanks for the feedback, Dominic.

(I've reordered the quoting a bit to make things fit together better in my responses) Dominic Steinitz wrote:
Sittampalam, Ganesh wrote:
It's also a bit specialised for my taste. A better option would be to have a takeWhileM which would allow sequenceWhile_ and sequenceWhile to be built from that and sequence_/sequence.
I'd argue that some of the functions in Control.Monad are more specialised.
I guess that's true, though I don't think that's in itself an argument for adding more.
I'm not sure what you had in mind for takeWhileM. Can you give a type signature / behaviour description?
Oops, it's not implementable. Sorry! (What I had in mind was something like takeWhileM :: Monad m => (a -> Bool) -> [m a] -> [m a], but it would have to actually run the monadic actions to figure out the structure of the list)
This last line should probably be return [] for consistency with takeWhile etc. The downside of that is that the monadic effect associating with producing y has been run but then the value is thrown away.
I use sequenceWhile to go through actions and stop at the one that "fails" (in some sense). I like to know what caused the failure so I return [y]. It did occur to me that one could argue that it should return []. But then you know something "failed" but not how.
I'd be happy with the composition of two library functions *provided* I can get hold of the value of the last run action.
I think the While name needs to change, in that case. I can't think of a nice sounding alternative though :-( Cheers, Ganesh =============================================================================== Please access the attached hyperlink for an important electronic communications disclaimer: http://www.credit-suisse.com/legal/en/disclaimer_email_ib.html ===============================================================================

On May 9, 2009, at 4:47 PM, Sittampalam, Ganesh wrote:
I'd be happy with the composition of two library functions *provided* I can get hold of the value of the last run action.
I think the While name needs to change, in that case. I can't think of a nice sounding alternative though :-(
what about "sequenceUntil"? (of course, sequenceUntil p as == sequenceWhile (not . p) as) FWIW, I too think a function like sequenceWhile or sequenceUntil would be a convenient addition daniel
participants (3)
-
Daniel Gorín
-
Dominic Steinitz
-
Sittampalam, Ganesh