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

  • A+
Category:Languages

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.

 


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

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: