Closed DanielAsher closed 4 years ago
@DanielAsher Yes, you can use the Generator
in this way, here is an example:
var items = [2, 3, 1, 4, 2, 4]
let generator = Generator<((Int, Int) -> Bool) -> Void> { yield in
items.sort { left, right in
var result = false
yield { result = $0(left, right) }
return result
}
}
while let next = generator.next() { next(>) }
XCTAssertEqual(items, items.sorted(by: >))
Maybe Generator
api is not that elegant as in Javascript, but that can definitely be modified. Also, I’d like to draw your attention that Generator
is marked as alpha version in the description, because this implementation was made rather quickly and requires very careful use. I recommend not to use Generator
in your products, but only for own curiosity.
Also, I’d like to inform you that I am now finishing a new version of the framework, where I focused primarily on the performance and safe of using the library. Because of this, I plan to remove a lot of functionality that is not completely refined or not safe, including Generator
. Maybe I'll come back to this api later, but for now it's not a priority for me. Thanks for understanding.
@belozierov This is very interesting information. Perhaps a fork is required to really understand the corner cases. I believe this is the future of Swift, and you are leading the way!
Javascript Generators allow for both input and output arguments in their
yield
:I have found this incredibly useful in using coroutines that typically either:
Generator<Void, Output>
Generator<Input, Output>
Generator<Input, Void>
Is it possible to extend
Generator
in this way?Let me say, I love the ambition of this project! Having been a swift dev for a few years, I recently jumped into javascript and kotlin, and have found async function generators , coroutines and structured concurrency to be the single most important revolution in language design in a generation. Having to go back to using
RxFeedback
with switch yard state-machines fills me with dread.Some motivating examples:
Notice in the examples below how coroutines allow for much more elegant way to store state and correctly consume a sequence (i.e. a grammar) of events.
An async state machine can also be beautifully coded as a
Generator<Event, State>
with events sent to the machine usinglet newState = stateflow.next(event)
I have a server-side generator that creates websocket events:
with a client-side consumer that monitors so:
Or for android / kotlin I use a builder function to implement a coroutine-based state machine so:
Please note the examples above are chopped down and thus prototype code only.