Haskell idiom for a custom map

Suppose you want mapa collection, but only collect the results of the displayed function if the displayed value meets certain criteria. I am currently doing this as such:

func = foldl (\acc x, ->  (maybeGrab x):acc) []


maybeGrab a
    | a > 5 = [someFunc a]
    | otherwise = []

While this works, I am sure that there is a more idiomatic way of “right / general / more recognizable”.

+5
source share
3 answers
 mapMaybe :: (a -> Maybe b) -> [a] -> [b]

mapMaybe from the Data.Maybe package looks like it does the job. The documentation states:

mapMaybe - , . , - Maybe b. , . b, b .

+10

: -, , , .

func = map someFunc . filter (>5)

.

func xs = [someFunc x | x <- xs, x > 5]
+5

. , . :

func = foldl (\acc x -> let a = g x in if a > 5 then a:acc else acc) []

g - , .

- , .

[EDIT]

, , . . . , - .

0

All Articles