Is the Haskell function known / implemented as another name?

I find many things that I combine on my own, which seem to be generally useful, actually have a standard implementation that I simply did not know about, so it was curious if anyone could say that they had seen such a thing before:

It takes a monadic function and will add it until the predicate is chosen as an alternative, and then returns the result of the predicate:

until :: (Monad m, Alternative m) => (a -> ma) -> (a -> mc) -> a -> mc f `until` p = \a -> (f >=> (p `altF` (until fp))) a where f1 `altF` f2 = \a -> f1 a <|> f2 a 

I understand that this is a clash with the prelude, I will probably call it something else, but I thought that at first I would see if there is similar functionality in the standard library, which I just don’t know about.

It also seems to me that I wonder if the compositional alternative that I wrote is defined elsewhere, or if any of these bits of functionality seem erroneous to start with. But the essence of my question is whether it is implemented elsewhere or something very similar is implemented elsewhere, perhaps

+6
source share
1 answer

There are a surprisingly large number of handy features that are present in Prelude or standard libraries, but perhaps should be. I would not worry too much about redefining a few if you find them useful.

Noting that altF equivalent to liftA2 (<|>) , you can write it more like this:

 till :: (Monad m, Alternative m) => (a -> ma) -> (a -> mb) -> a -> mb -- To do f till p is to first do f; then either p, or f till p. f `till` p = f >=> (<|>) <$> p <*> f `till` p 

And noting that (Monad m, Alternative m) almost the same as MonadPlus m , you can simplify the type a bit:

 till :: MonadPlus m => (a -> ma) -> (a -> mb) -> a -> mb f `till` p = f >=> mplus <$> p <*> f `till` p 

A similar function is called untilM in Control.Monad.Loops , which uses the Bool predicate, and the LoopT transformer exists for an arbitrary loop in Control.Monad.Trans.Loop . But no, this particular function is not yet known.

+7
source

All Articles