[Haskell-cafe] Reinventing the wheel? Does any existing package provide an applicatively lifted (>>) ?

Jeffrey Brown jeffbrown.the at gmail.com
Thu Sep 28 10:57:11 UTC 2017


> or, equivalently:
>
>        a `andthen` b = (>>) <$> a <*> b
>
> for which http://pointree.io dutifully gives me:
>
>      andthen = (<*>) . ((>>) <$>)

That link is not working for me, and Google isn't finding it, and it sounds
like a useful thing.


On Thu, Sep 28, 2017 at 1:55 AM, Viktor Dukhovni <ietf-dane at dukhovni.org>
wrote:

>
> When generating a report file from a database I found it much more
> efficient (significantly shorter runtime) to represent each row
> by an I/O action that prints the row, rather than to construct a
> Row object that to print and throw away.
>
> But the naive way to construct the I/O action can be tedious to
> maintain once the column count gets appreciably high:
>
>     newtype Foo = Foo { _foo :: IO () }
>     instance FromRow Foo where
>       fromRow = Foo <$> (rowPrinter <$> field <*> field <*> field <*> ...
> <*> field)
>         where
>           rowPrinter :: Type1 -> Type2 -> Type3 -> ... -> TypeN -> IO ()
>           rowPrinter p1 p2 p3 ... pN = do
>             printP1
>             printP2
>             printP3
>             ...
>             printPN
>
> So I decided to applicatively decompose the rowPrinter function
> (with the actual name of "andthen" to be determined later) as:
>
>    rowPrinter = (printP1 <$> field) `andthen`
>                 (printP2 <$> field) `andthen`
>                 (printP3 <$> field) `andthen`
>                 ...
>                 (printPN <$> field)
>
> which avoids the need to package the column printers explicitly into
> a single function, and may be somewhat more efficient a well.
>
> What was not immediately obvious to me was whether there's an "off the
> shelf" implementation of "andthen" I could just reuse. The necessary
> operator satisfies:
>
>         andthen (f (m a)) (f (m b)) = f (ma >> mb)
>
> or, equivalently:
>
>         a `andthen` b = (>>) <$> a <*> b
>
> for which http://pointree.io dutifully gives me:
>
>        andthen = (<*>) . ((>>) <$>)
>
> Its type signature is:
>
>     Prelude> :set prompt "l> "
>     l> :m + Control.Applicative
>     l> :m + Control.Monad
>     l> :t ((<*>) . ((>>) <$>))
>     ((<*>) . ((>>) <$>))
>       :: (Monad m, Applicative f) => f (m a) -> f (m b) -> f (m b)
>     l>
>
> It seems to me that this would have been done before, and the operator
> would already be present in some package, but I'm having trouble finding
> it.
>
> (Due to the hidden constructors of FromRow defining a Semigroup does not
>  work out here, so I can't use (<>), which also inconveniently conflicts
>  with  Monoid (<>)).
>
> So my question is whether the operator in question is already available,
> under some name in some package, or else suggested names for it if new.
>
> --
>         Viktor.
>
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.




-- 
Jeff Brown | Jeffrey Benjamin Brown
Website <https://msu.edu/~brown202/>   |   Facebook
<https://www.facebook.com/mejeff.younotjeff>   |   LinkedIn
<https://www.linkedin.com/in/jeffreybenjaminbrown>(spammy, so I often miss
messages here)   |   Github <https://github.com/jeffreybenjaminbrown>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20170928/fe2dda83/attachment.html>


More information about the Haskell-Cafe mailing list