let result = items |>List.scan (fun (removed, _) item -> if removed then true, Some(item) //If already removed, just propagate elif predicate item then true, None //If not removed but predicate matches, don't propagate else false, Some(item)) //If not removed and predicate doesn't match, propagate (false, None) |>List.choose snd
A state is a tuple. The first element is a logical flag indicating that we have already removed an element from the list. The second element is an option: Some, when we want to emit an element, None otherwise.
The last line takes two elements from the states and for each of them emits a wrapped value (in the case of Some) or does nothing (in the case of None).
source share