I'm looking for feedback on writing idiomatic PureScript code. This code below is sample code to read from a Twitter API. The signature of the helper methods are:
-- read consumer credentials from a config file
readConfig :: String -> Aff (Either String TwitterCredentials)
-- get the bearer authentication using the consumer credentials
getTokenCredentials :: TwitterCredentials -> Aff (Either String BearerAuthorization)
-- read the Twitter API using the bearer authentication
showResults :: BearerAuthorization -> String -> Aff (Either String SearchResults)
My code is:
main :: Effect Unit
main = launchAff_ do
let searchTerm = "PureScript"
config <- readConfig "./config/twitter_credentials.json"
case config of
Left errorStr -> errorShow errorStr
Right credentials -> do
tokenCredentialsE <- getTokenCredentials credentials
case tokenCredentialsE of
Left error ->
errorShow error
Right tokenCredentials -> do
resultsE <- showResults tokenCredentials searchTerm
case resultsE of
Left error ->
errorShow error
Right result ->
liftEffect $ logShow $ "Response:" <> (show result.statuses)
As you can see, there is a lot of nested Either statements and I call errorShow three times. How would you write this code making it more readable and possibly removing the code duplication?