Search code examples
option-typepurescriptdo-notation

How can I return a Maybe value from do notation in PureScript?


I'm trying to return a Maybe value from a function that uses do notation, but I can't seem to get it to work. This function takes a string (The "filename") and a Path to search in...

findIn :: String -> Path -> Maybe Path
findIn search start = do
    file <- ls start
    if ((filename file) == search)
      then Just file
      else Nothing

Where...

ls :: Path -> Array Path
filename :: Path -> String

But I keep getting the error "Count not match Type Array with type Maybe", so it looks like the compiler is expecting the do notation to return an array. How would I return a maybe value?


Solution

  • You can't mix monads like that.

    When you write:

    file <- ls start
    

    it's a bit like saying "for each value file in the array..." so you are in the context of multiple possible values.

    But then the rest of the code is in the context of Maybe, which can only handle one (or zero) values.

    In the module Data.Foldable there is a find function, which does the bulk of the work of your main function, by searching for a single item that matches some criteria. Its actual type is more general, but when constrained to Arrays it's like this:

    find :: forall a. (a -> Boolean) -> Array a -> Maybe a
    

    Then you can just write:

    findIn search start = find (\x => x == search) $ ls start