In fact, the Haskell operator exists only for this case:
(<*) :: Applicative f => fa -> fb -> fa
For example:
ghci> getLine <* putStrLn "Thanks for the input!" asdf Thanks for the input! "asdf"
All that remains then is to find the same statement in scalaz , since scalaz usually replicates everything that Haskell has. You can transfer values ββto Identity , since Scala does not have an IO to classify effects. The result will look something like this:
import scalaz._ import Scalaz._ def foo(a: A): Int = (a.calculateImportantThings.pure[Identity] <* a.cleanup.pure[Identity]).value
This is rather unpleasant, since we must explicitly transfer side calculations to Identity. Well, true, scalaz does some magic that implicitly converts to and from the Identity container, so you can simply write:
def foo(a: A): Int = Identity(a.calculateImportantThings) <* a.cleanup()
You need to somehow hint at the compiler, which is the leftmost in the Identity monad. The above was the shortest way I could think of. Another possibility is to use Identity() *> foo <* bar , which will trigger the effects of foo and bar in that order, and then produce the value foo .
To return to the ghci example:
scala> import scalaz._; import Scalaz._ import scalaz._ import Scalaz._ scala> val x : String = Identity(readLine) <* println("Thanks for the input!") << input asdf and press enter >> Thanks for the input! x: String = asdf
Dan burton
source share