You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I wonder if anyone has explored or would find useful a flatMap on Shared? I find myself wanting functionality similar to flatMap on Publisher in Combine, where the transform closure produces a newShared (actually SharedReader, probably) rather than a new Value.
A little pseudocode to illustrate the functionality, borrowing the FactKey from the APIClientDemo:
@Test func sharedFlatMap() async throws {
@Shared(.inMemory("count")) var count = 3
@SharedReader var fact = $count.flatMap { count in
return FactKey(number: count)
}
try await $fact.load()
#expect(fact == "3 is a very good number.")
$count.withLock { $0 += 1 }
try await $fact.load()
#expect(fact == "4 is a very good number.")
}
I don't know if this is exactly the right shape. I have my theoretical implementation of flatMap returning a SharedReaderKey rather than a SharedReader per se so the implementation can call SharedReader.load(_:) async throws rather than re-creating the whole reader. I presume this might require a new Reference implementation, eg _FlatMapClosureReference: Reference { ... }?
Thoughts? If this is fundamentally not possible for XYZ reason, or there's already a way to achieve similar functionality with existing APIs, those responses are welcome too.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I wonder if anyone has explored or would find useful a
flatMaponShared? I find myself wanting functionality similar toflatMaponPublisherin Combine, where the transform closure produces a newShared(actuallySharedReader, probably) rather than a newValue.A little pseudocode to illustrate the functionality, borrowing the
FactKeyfrom the APIClientDemo:I don't know if this is exactly the right shape. I have my theoretical implementation of
flatMapreturning aSharedReaderKeyrather than aSharedReaderper se so the implementation can callSharedReader.load(_:) async throwsrather than re-creating the whole reader. I presume this might require a newReferenceimplementation, eg_FlatMapClosureReference: Reference { ... }?Thoughts? If this is fundamentally not possible for XYZ reason, or there's already a way to achieve similar functionality with existing APIs, those responses are welcome too.
Beta Was this translation helpful? Give feedback.
All reactions