[Haskell-cafe] Maybe type filtered to remove Nothing
Damien Mattei
damien.mattei at gmail.com
Sat Jan 5 08:50:19 UTC 2019
yes both works, code looks like this now:
-- remove the records having N°BD NULL
let fltWDS = Prelude.filter (\(Only a) ->
case a of
Nothing -> False
Just a -> True)
bd_rows_WDS
-- let fltWDS = [a | Only (Just a) <- bd_rows_WDS]
-- let fltWDS = catMaybes (Prelude.map fromOnly bd_rows_WDS)
putStr "bd_rows_WDS filtered fltWDS ="
putStrLn $ show fltWDS
let lg_fltWDS = Prelude.length fltWDS
putStrLn ("lg_fltWDS = " ++ (show lg_fltWDS))
let resBDtxt = if lg_fltWDS == 0
then
Nothing
else
if lg_fltWDS == 1
then
--Just (Prelude.head fltWDS)
fromOnly (Prelude.head fltWDS)
else
--trace "WARNING: multiple BD in WDS result"
(Just (Prelude.head fltWDS))
trace "WARNING: multiple BD in WDS result"
fromOnly (Prelude.head fltWDS)
putStr "resBDtxt ="
putStrLn (maybe "Empty List" show resBDtxt)
i will keep a Maybe type because in fact there is a possibility of having
no result or one (extract sometimes from many) from the databases, so there
is a possibility for Nothing i must keep and handle for the next
instructions...
thanks
On Thu, Jan 3, 2019 at 3:54 PM Neil Mayhew <
neil_mayhew at users.sourceforge.net> wrote:
> 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.
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20190105/2a819360/attachment.html>
More information about the Haskell-Cafe
mailing list