Karl Bielefeldt
import cats.effect.{IO, IOApp}
import cats.implicits._
object HelloWorld extends IOApp.Simple {
val run = IO.println("Hello, World!")
}
import Data.Either
import Control.Exception
main :: IO ()
main = putStrLn "Hello, World!"
IO.readLine.map("Hello, " + _) >>= IO.println
("Hello, " ++) <$> getLine >>= putStrLn
for {
name <- IO.readLine
_ <- IO.println("Hello, " + name)
} yield ()
do
name <- getLine
putStrLn ("Hello, " ++ name)
fetchName.map("Hello, " + _) >>= IO.println
("Hello, " ++) <$> fetchName >>= putStrLn
fetchName
.map("Hello, " + _)
.handleError(_ => "Error fetching name") >>= IO.println
fromRight "Error fetching name" <$>
tryAny (("Hello, " ++) <$> fetchName)
>>= putStrLn
tryAny :: IO a -> IO (Either SomeException a)
tryAny = try
val connections =
List("server1", "server2", "server3").map(connect)
val run =
findFirstSuccess(connections) >>= useConnection
def findFirstSuccess[A](xs: Iterable[IO[A]]): IO[A]
findFirstSuccess connections >>= useConnection
where
connections = connect <$> ["server1", "server2", "server3"]
findFirstSuccess :: [IO a] -> IO a
retryUntilSuccess(connect("server1")) >>= useConnection
retryUntilSuccess (connect "server1") >>= useConnection