[back]

Filter Map

Implement the function

val filter_map : ('a -> 'b option) -> 'a list -> 'b list

so that filter_map p l is the result of applying p to every element of l, filtering out the None constructors, and keeping only the values carried by Some constructors.

Examples

let f x = if x mod 2 = 0 then Some (x * x) else None
let _ = assert (filter_map f [1;2;3;4;5;6] = [4;16;36])
let _ = assert (filter_map f [1;3;5] = [])