[Haskell-cafe] Maybe type filtered to remove Nothing

Neil Mayhew neil_mayhew at users.sourceforge.net
Thu Jan 3 14:53:45 UTC 2019


On 2019-01-03 12:43 AM, Ganesh Sittampalam wrote:
> 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.
>
> ...
>
> 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]

It probably would be helpful to remove the Only wrapper at this stage, 
so this might be even better:

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

The way to do it with catMaybes would be to map with fromOnly first:

let fltWDS = catMaybes (map fromOnly bd_rows_WDS)

As Ganesh shows, there's no need for a type annotation after you've done 
the query, because the compiler can infer the type [Text] from the types 
of the functions that are used. The annotations are necessary with query 
only because it's polymorphic and can work with a wide variety of types.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20190103/7af66958/attachment.html>


More information about the Haskell-Cafe mailing list