haskell / mtl

The Monad Transformer Library
http://www.haskell.org/haskellwiki/Monad_Transformers
Other
362 stars 63 forks source link

Uneasy with example using `fromJust` #23

Closed FranklinChen closed 7 years ago

FranklinChen commented 9 years ago

Reader.hs documentation has an example that makes me very uneasy because it uses fromJust. I think code snippets make a big impression on newcomers and the example code to illustrate Reader is not anything anyone would really write because it doesn't account for errors.

lookupVar :: String -> Bindings -> Int
lookupVar name bindings = fromJust (Map.lookup name bindings)
ekmett commented 9 years ago

I'd happily take a pull request to change the example to something more neutral.

tmcgilchrist commented 8 years ago

Regarding an improvement to this example, would you consider it enough that there is a non-partial function in place of fromJust e.g.

-- The Reader monad, which implements this complicated check.
calc_isCountCorrect :: Reader Bindings Bool
calc_isCountCorrect = do
  count <- asks (lookupVar "count")
  bindings <- ask
  return . boolM $ ((== (Map.size bindings)) <$> count )

-- Not really a good idea?
boolM :: Maybe Bool -> Bool
boolM Nothing = False
boolM (Just b) = b
ekmett commented 8 years ago

We could just call maybe appropriately instead.

ekmett commented 7 years ago

Merged