Hacker News new | past | comments | ask | show | jobs | submit login

Just one. If you have a series of functions that could return Nothing (Maybe's version of null), you chain them like:

  foo >>= bar >>= baz
This will automatically return Nothing if any of the contained functions do. This does require all of the function to have the type (a -> Maybe b), indicating that they take a value and can return either another value, or Nothing. If you have a function with type (a -> b), indicating that it is guarenteed to return something, you can wrap it like so:

  foo >>= return . bar >>= baz
A complete usage might look like:

  f x = fromMaybe defualtValue $ Just x >>= foo >>= bar >>= baz



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: