[Haskell-cafe] Maybe type filtered to remove Nothing

Ganesh Sittampalam ganesh at earth.li
Thu Jan 3 07:43:25 UTC 2019


On 02/01/2019 13:45, Damien Mattei wrote:
> i had filtered a [Maybe Text] type to remove Nothing from the list and
> now i want to put the result in a [Text] type but the compiler complains
> about the incompatible type :

As you've seen, filter removes values but doesn't change the type.

If you did have [Maybe Text] you could use the library function
  catMaybes :: [Maybe a] -> [a]
to both do the filtering and change the types.


>  (bd_rows_WDS :: [Only (Maybe Text)]) <- query conn qry_head_WDS (Only
> (name::String))
>
> -- remove the records having N°BD NULL
>     let fltWDS :: [Only Text] = Prelude.filter (\(Only a) ->
>                                     case a of
>                                       Nothing -> False
>                                       Just a -> True)
>                  bd_rows_WDS

But in your case you actually have [Only (Maybe Text)] rather than
[Maybe Text] so catMaybes won't work. One option is to use a list
comprehension instead:

let fltWDS = [Only a | Only (Just a) <- bd_rows_WDS]

Cheers,

Ganesh


More information about the Haskell-Cafe mailing list