---
title: "filter(_:)"
framework: combine
role: symbol
role_heading: Instance Method
path: "combine/publisher/filter(_:)"
---

# filter(_:)

Republishes all elements that match a provided closure.

## Declaration

```swift
func filter(_ isIncluded: @escaping (Self.Output) -> Bool) -> Publishers.Filter<Self>
```

## Parameters

- `isIncluded`: A closure that takes one element and returns a Boolean value indicating whether to republish the element.

## Mentioned in

Receiving and Handling Events with Combine Routing Notifications to Combine Subscribers

## Return Value

Return Value A publisher that republishes all elements that satisfy the closure.

## Discussion

Discussion Combine’s filter(_:) operator performs an operation similar to that of doc://com.apple.documentation/documentation/Swift/Sequence/filter(_:)-5y9d2 in the Swift Standard Library: it uses a closure to test each element to determine whether to republish the element to the downstream subscriber. The following example, uses a filter operation that receives an Int and only republishes a value if it’s even. let numbers: [Int] = [1, 2, 3, 4, 5] cancellable = numbers.publisher     .filter { $0 % 2 == 0 }     .sink { print("\($0)", terminator: " ") }

// Prints: "2 4"

## See Also

### Filtering elements

- [tryFilter(_:)](combine/publisher/tryfilter(_:).md)
- [compactMap(_:)](combine/publisher/compactmap(_:).md)
- [tryCompactMap(_:)](combine/publisher/trycompactmap(_:).md)
- [removeDuplicates()](combine/publisher/removeduplicates().md)
- [removeDuplicates(by:)](combine/publisher/removeduplicates(by:).md)
- [tryRemoveDuplicates(by:)](combine/publisher/tryremoveduplicates(by:).md)
- [replaceEmpty(with:)](combine/publisher/replaceempty(with:).md)
- [replaceError(with:)](combine/publisher/replaceerror(with:).md)
