Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a standard function like `>>`, but returning the result of the first operand?

For instance, suppose I want to read a line and also ring a bell:

λ getLine >> putChar '\007'
How lang and dreary is the night when I am frae my Dearie.
-- Blip and `()`. The line is lost.
λ getLine >>= (\x -> putChar '\007' >> return x
I restless lie frae e'en to morn though I were ne'er sae weary.
"I restless lie frae e'en to morn though I were ne'er sae weary."
-- A line and also a blip side effect.

The idea seems to have much in common with const, the only difference being that the values given are effectful, and are both executed, even though only the value of the first action is retained. (Unlike with >>, which retains the value from the second.) What I mean exactly is this:

λ constM a b = a >>= \x -> b >> return x

Here is a more complex example, involving parsers from Text.ParserCombinators.ReadP:

λ readP_to_S (many1 (munch1 (not . isSpace) `constM` skipSpaces ) `constM` eof) <$> getLine
How slow ye move, ye heavy hours.
[(["How","slow","ye","move,","ye","heavy","hours."],"")]

 

I wonder if this function is available in base, or easily constructible from other functions in base.

like image 750
Ignat Insarov Avatar asked Dec 10 '22 05:12

Ignat Insarov


1 Answers

(<*) :: Applicative f => f a -> f b -> f a

like image 162
leftaroundabout Avatar answered Jan 25 '23 14:01

leftaroundabout