# Using 'filter' on an array of actors

**URL:** <https://forums.swift.org/t/using-filter-on-an-array-of-actors/73386>\
**Category:** Using Swift\
**Tags:** collection, concurrency\
**Created:** [July 23, 2024, 8:14am UTC](https://forums.swift.org/t/using-filter-on-an-array-of-actors/73386 "2024-07-23T08:14:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![maartene](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/maartene/32/42076_2.png) [@maartene](https://forums.swift.org/u/maartene)\
**Post date:** [July 23, 2024, 8:14am UTC](https://forums.swift.org/t/using-filter-on-an-array-of-actors/73386/1 "2024-07-23T08:14:34Z")

</div>

Hi everyone,

I have a function that filters an array of Actors:

```swift
actor Entity {
    // other stuff
    var state: State {
        // determine state
    }
}

actor World {
    // other stuff
    let entities: [Entity]

    func getActiveEntities() async -> [Entity] {
        entities.filter { $0.state == .active }
    }
}

```

This doesn't compile because of the following error: `Actor-isolated property 'state' can not be referenced from a non-isolated context`.

My question is, how can I use the typical collection functions like `filter`, `first`, `map`, `reduce` when iterating over a collection of actors?

---

<div class="post-metadata">

**Author:** ![vanvoorden](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/vanvoorden/32/33619_2.png) [@vanvoorden](https://forums.swift.org/u/vanvoorden)\
**Post date:** [July 23, 2024, 9:02am UTC](https://forums.swift.org/t/using-filter-on-an-array-of-actors/73386/2 "2024-07-23T09:02:47Z")

</div>

> [@maartene](#):
>
> My question is, how can I use the typical collection functions like `filter`, `first`, `map`, `reduce` when iterating over a collection of actors?

I don't know if there is something direct to solve this… but you might be able to use [AsyncSyncSequence](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/AsyncAlgorithms.docc/Guides/Lazy.md) to transform `entities` to an `AsyncSequence` that can then accept the `async` version of [`filter`](https://developer.apple.com/documentation/swift/asyncsequence/filter(_:)-2cc0l). And then try to use the [`Collection Initializers`](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/AsyncAlgorithms.docc/Guides/Collections.md) to `await` on a regular `Array`.

---

<div class="post-metadata">

**Author:** ![maartene](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/maartene/32/42076_2.png) [@maartene](https://forums.swift.org/u/maartene)\
**Post date:** [July 23, 2024, 9:18am UTC](https://forums.swift.org/t/using-filter-on-an-array-of-actors/73386/3 "2024-07-23T09:18:41Z")

</div>

that's an interesting approach. I'm gonna give this a try!

---

<div class="post-metadata">

**Author:** ![maartene](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/maartene/32/42076_2.png) [@maartene](https://forums.swift.org/u/maartene)\
**Post date:** [July 23, 2024, 10:15am UTC](https://forums.swift.org/t/using-filter-on-an-array-of-actors/73386/4 "2024-07-23T10:15:33Z")

</div>

OK. First off, I like this approach way more than the CoPilot suggestion to do:

```swift
var activeEntities = [Entity]()
for entity in entities {
    if await entity.state == .active {
        activeEntities.append(entity)
    }
}
return activeEntities

```

There is something interesting about using Async Sequences this way. After some more filtering, this is the type I ended up with:  
`AsyncFilterSequence<AsyncFilterSequence<AsyncFilterSequence<AsyncSyncSequence<[Entity]>>>>`

This isn't really what's stopping me now though.

I would like to sort the array, based on distance from a specific entity, to get the closest one to the player. The sort method isn't available though, however `min` is.

But this gives this warning message, that I can't really explain in this context:  
`Passing argument of non-sendable type '(AsyncFilterSequence<AsyncFilterSequence<AsyncFilterSequence<AsyncSyncSequence<[Entity]>>>>.Element, AsyncFilterSequence<AsyncFilterSequence<AsyncFilterSequence<AsyncSyncSequence<[Entity]>>>>.Element) async -> Bool' (aka '(Entity, Entity) async -> Bool') outside of actor-isolated context may introduce data races`  
I would expect that `min` would work similar to `filter`, but apparently it does not.

Is there a tutorial/book somewhere that explains these use cases some more? I.e. working with collections of actors?

---

<div class="post-metadata">

**Author:** ![maartene](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/maartene/32/42076_2.png) [@maartene](https://forums.swift.org/u/maartene)\
**Post date:** [July 23, 2024, 7:27pm UTC](https://forums.swift.org/t/using-filter-on-an-array-of-actors/73386/5 "2024-07-23T19:27:06Z")

</div>

Ah, fixed the last warning. Had to mark the closure that was passed in to ‘min’ as ‘@Sendable’.

Regardless, I am truly impressed with the concurrency checker in Swift. Never have I felt more confident with async and multi threaded code.
