Lists of layouts and mayba

I am looking for code that can simultaneously display and align lists and maybs. I found such a flat map in this section :

flatMap :: (t -> [a]) -> [t] -> [a] flatMap _ [] = [] flatMap f (x:xs) = fx ++ flatMap f xs 

This works great:

 > flatMap id [[],[1,2],[3],[],[4,5,6]] [1,2,3,4,5,6] 

The only problem is that it does not work for Maybes. Instead, I should use Data.Maybe.mapMaybe :

 > Data.Maybe.mapMaybe id [Just 1, Nothing, Just 2, Just 3, Nothing] [1,2,3] 

Is there one built-in function that can handle both lists and maybes (and possibly some other types)?

+4
source share
1 answer

I think Data.Foldable might be what you are looking for:

 > let flatMap f = concatMap (Data.Foldable.toList . f) > :t flatMap flatMap :: Data.Foldable.Foldable t => (a -> tb) -> [a] -> [b] > flatMap id [[],[1,2],[3],[],[4,5,6]] [1,2,3,4,5,6] > flatMap id [Just 1, Nothing, Just 2, Just 3, Nothing] [1,2,3] 
+11
source

Source: https://habr.com/ru/post/1398783/


All Articles