Sean Clark Hess
Sean Clark Hess

Reputation: 16059

How to use non-monadic functions in a bind operation

I feel like I should already know this, but how could I use fromMaybe in one line instead of breaking it into 2 with a let?

main = do
    maybePort <- lookupEnv "PORT"
    let port = fromMaybe "4020" maybePort
    putStrLn $ "Listening on:" ++ port

Upvotes: 4

Views: 94

Answers (1)

Random Dev
Random Dev

Reputation: 52280

you can use fmap or <$> like this:

import Control.Applicative ((<$>))

main = do
    port <- fromMaybe "4020" <$> lookupEnv "PORT"
    putStrLn $ "Listening on:" ++ port

Upvotes: 8

Related Questions