filter -package:base -package:containers -package:vector package:rio

O(n) filter, applied to a predicate and a ByteString, returns a ByteString containing those characters that satisfy the predicate.
Filter this map by retaining only elements which values satisfy a predicate.
Filter this set by retaining only elements satisfying a predicate.
filter, applied to a predicate and a list, returns the list of those elements that satisfy the predicate; i.e.,
filter p xs = [ x | x <- xs, p x]
>>> filter odd [1, 2, 3]
[1,3]
O(n). Filter all values that satisfy the predicate.
filter (> "a") (fromList [(5,"a"), (3,"b")]) == singleton 3 "b"
filter (> "x") (fromList [(5,"a"), (3,"b")]) == empty
filter (< "a") (fromList [(5,"a"), (3,"b")]) == empty
filter p xs removes any elements from xs that do not satisfy p.
The filter function takes a predicate p and a sequence xs and returns a sequence of those elements which satisfy the predicate.
O(n). Filter all elements that satisfy the predicate.
O(n) filter, applied to a predicate and a Text, returns a Text containing those characters that satisfy the predicate.
O(n) Drop elements that do not satisfy the predicate
O(n) Drop elements that do not satisfy the predicate
O(n) Drop elements that do not satisfy the predicate
O(n) Drop elements that do not satisfy the predicate
Filter this map by retaining only elements satisfying a predicate.
filtered is a traversal that filters elements “passing” through it:
>>> (1,2,3,4) ^.. each
[1,2,3,4]
>>> (1,2,3,4) ^.. each . filtered even
[2,4]
It also can be used to modify elements selectively:
>>> (1,2,3,4) & each . filtered even %~ (*100)
(1,200,3,400)
The implementation of filtered is very simple. Consider this traversal, which always “traverses” just the value it's given:
id :: Traversal' a a
id f s = f s
And this traversal, which traverses nothing (in other words, doesn't traverse the value it's given):
ignored :: Traversal' a a
ignored f s = pure s
And now combine them into a traversal that conditionally traverses the value it's given, and you get filtered:
filtered :: (a -> Bool) -> Traversal' a a
filtered p f s = if p s then f s else pure s
By the way, note that filtered can generate illegal traversals – sometimes this can bite you. In particular, an optimisation that should be safe becomes unsafe. (To the best of my knowledge, this optimisation never happens automatically. If you just use filtered to modify/view something, you're safe. If you don't define any traversals that use filtered, you're safe too.) Let's use evens as an example:
evens = filtered even
If evens was a legal traversal, you'd be able to fuse several applications of evens like this:
over evens f . over evens g = over evens (f . g)
Unfortunately, in case of evens this isn't a correct optimisation:
  • the left-side variant applies g to all even numbers, and then applies f to all even numbers that are left after f (because f might've turned some even numbers into odd ones)
  • the right-side variant applies f and g to all even numbers
Of course, when you are careful and know what you're doing, you won't try to make such an optimisation. However, if you export an illegal traversal created with filtered and someone tries to use it, they might mistakenly assume that it's legal, do the optimisation, and silently get an incorrect result. If you are using filtered with some another traversal that doesn't overlap with -whatever the predicate checks-, the resulting traversal will be legal. For instance, here the predicate looks at the 1st element of a tuple, but the resulting traversal only gives you access to the 2nd:
pairedWithEvens :: Traversal [(Int, a)] [(Int, b)] a b
pairedWithEvens = each . filtered (even . fst) . _2
Since you can't do anything with the 1st components through this traversal, the following holds for any f and g:
over pairedWithEvens f . over pairedWithEvens g = over pairedWithEvens (f . g)
O(n). Filter all keys/values that satisfy the predicate.
filterWithKey (\k _ -> k > 4) (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
This generalizes the list-based filter function.
O(n) Drop elements that do not satisfy the monadic predicate
O(n) Drop elements that do not satisfy the monadic predicate
O(n) Drop elements that do not satisfy the monadic predicate
O(n) Drop elements that do not satisfy the monadic predicate
Direct MonadPlus equivalent of filter.

Examples

The filter function is just mfilter specialized to the list monad:
filter = ( mfilter :: (a -> Bool) -> [a] -> [a] )
An example using mfilter with the Maybe monad:
>>> mfilter odd (Just 1)
Just 1
>>> mfilter odd (Just 2)
Nothing
O(n) Drop elements that do not satisfy the predicate which is applied to values and their indices
O(n) Drop elements that do not satisfy the predicate which is applied to values and their indices
O(n) Drop elements that do not satisfy the predicate which is applied to values and their indices