Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lookupDefault and lookup are curiously lazy #23

Open
treeowl opened this issue Nov 14, 2015 · 1 comment
Open

lookupDefault and lookup are curiously lazy #23

treeowl opened this issue Nov 14, 2015 · 1 comment

Comments

@treeowl
Copy link

treeowl commented Nov 14, 2015

Currently,

lookupDefault def cfg name = fromMaybe def <$> lookup cfg name

I'd have expected

lookupDefault def cfg name = maybe (return def) return =<< lookup cfg name

lookup is also a bit weird:

lookup :: Configured a => Config -> Name -> IO (Maybe a)
lookup (Config root BaseConfig{..}) name =
    (join . fmap convert . H.lookup (root `T.append` name)) <$> readIORef cfgMap

There's no obvious reason to delay the lookup to the use site (which keeps the entire HashMap live). Why not use something like this?

lookup (Config root BaseConfig{..}) name =  do
     mp <- readIORef cfgMap
     let result = convert =<< H.lookup (root `T.append` name) mp
     evaluate result
     return result
@treeowl
Copy link
Author

treeowl commented Nov 14, 2015

If you'd prefer to delay the conversion, which seems somewhat less peculiar than delaying the lookup, that would be

lookup (Config root BaseConfig{..}) name =  do
     mp <- readIORef cfgMap
     let result = H.lookup (root `T.append` name) mp
     evaluate result
     return (convert =<< result)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant