Skip to content

Commit 4678d40

Browse files
author
Thibault Wittemberg
committed
operators: implement Assign
1 parent c205cbc commit 4678d40

3 files changed

Lines changed: 73 additions & 0 deletions

File tree

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ AsyncSequences
4141
* [SwitchToLatest](#SwitchToLatest)
4242
* [FlatMapLatest](#FlatMapLatest)
4343
* [HandleEvents](#HandleEvents)
44+
* [Assign](#Assign)
4445
* [EraseToAnyAsyncSequence](#EraseToAnyAsyncSequence)
4546

4647
More operators and extensions are to come. Pull requests are of course welcome.
@@ -376,6 +377,20 @@ for try await element in handledSequence {}
376377
// finished
377378
```
378379

380+
### Assign
381+
382+
`assign(to:on:)` assigns each element from the async sequence to a property on an object.
383+
384+
```swift
385+
class Root {
386+
var property: String = ""
387+
}
388+
389+
let root = Root()
390+
let fromSequence = AsyncSequences.From(["1", "2", "3"])
391+
try await fromSequence.assign(to: \.property, on: root) // will set the property value to "1", "2", "3"
392+
```
393+
379394
### EraseToAnyAsyncSequence
380395

381396
`eraseToAnyAsyncSequence()` type-erases the async sequence into an AnyAsyncSequence.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//
2+
// AsyncSequence+Assign.swift
3+
//
4+
//
5+
// Created by Thibault Wittemberg on 02/02/2022.
6+
//
7+
8+
public extension AsyncSequence {
9+
/// Assigns each element from the async sequence to a property on an object.
10+
///
11+
/// ```
12+
/// class Root {
13+
/// var property: String = ""
14+
/// }
15+
///
16+
/// let root = Root()
17+
/// let fromSequence = AsyncSequences.From(["1", "2", "3"])
18+
/// try await fromSequence.assign(to: \.property, on: root) // will set the property value to "1", "2", "3"
19+
/// ```
20+
///
21+
/// - Parameters:
22+
/// - keyPath: A key path that indicates the property to assign.
23+
/// - object: The object that contains the property.
24+
func assign<Root>(to keyPath: ReferenceWritableKeyPath<Root, Self.Element>, on object: Root) async throws {
25+
for try await element in self {
26+
object[keyPath: keyPath] = element
27+
}
28+
}
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//
2+
// AsyncSequence+AssignTests.swift
3+
//
4+
//
5+
// Created by Thibault Wittemberg on 02/02/2022.
6+
//
7+
8+
import AsyncExtensions
9+
import XCTest
10+
11+
private class Root {
12+
13+
var successiveValues = [String]()
14+
15+
var property: String = "" {
16+
didSet {
17+
self.successiveValues.append(self.property)
18+
}
19+
}
20+
}
21+
22+
final class AsyncSequence_AssignTests: XCTestCase {
23+
func testAssign_sets_elements_on_the_root() async throws {
24+
let root = Root()
25+
let sut = AsyncSequences.From(["1", "2", "3"])
26+
try await sut.assign(to: \.property, on: root)
27+
XCTAssertEqual(root.successiveValues, ["1", "2", "3"])
28+
}
29+
}

0 commit comments

Comments
 (0)