कोटलीन उपज कार्यान्वयन

कोटिन एम 5 की रिहाई के बारे में हाल ही में एक पोस्ट ने मुझे भाषा के साथ थोड़ा खेलना चाहा (यह पहले सामने आया, लेकिन मेरे हाथ नहीं पहुंचे)। क्लासिक हैलो दुनिया लिखने के लिए मेरे लिए यह दिलचस्प नहीं था, इसलिए मैंने कुछ जटिल पहेली के साथ आने का फैसला किया जो अभी भी हमें विभिन्न दिलचस्प भाषा सुविधाओं का उपयोग करने की अनुमति देगा।
आपको याद दिला दूं कि कोटलिन एक सांख्यिकीय रूप से टाइप की जाने वाली प्रोग्रामिंग भाषा है जिसे JVM बायटेकोड या जावास्क्रिप्ट में संकलित किया जा सकता है। Jetbrains द्वारा विकसित।

कोटलिन में कई अलग-अलग सिंटैक्ट मिठाई हैं, जो आपको काफी दिलचस्प चीजें करने की अनुमति देता है। आधिकारिक दस्तावेज में एक नियमित फ़ंक्शन के रूप में सिंक्रनाइज़ किए गए निर्माण कार्यान्वयन का एक उदाहरण (उच्च-क्रम फ़ंक्शन देखें) है, जैसा कि जावा में दिखता है, ठीक उसी तरह।
मुझे उपज को समान तरीके से लागू करने का विचार था।


उपज


कई सी # डेवलपर्स उपज निर्माण को जानते हैं और उपयोग करते हैं।
यील्ड कोरटाइन को लागू करने का एक उदाहरण है। एक कोरआउट एक प्रक्रिया की अवधारणा का एक सामान्यीकरण है और कई प्रवेश बिंदुओं का समर्थन करता है। C # भाषा के मामले में, उपज का उपयोग करते हुए लिखे गए कोरयूटीन मक्खी पर IEnumerable तत्वों को उत्पन्न करने की अनुमति देते हैं:
static IEnumerable<int> CountToTen() { for (int i = 0; i <= 10; ++i) { yield return i; } } 

उपज के साथ , आप एक अंतहीन सूची लिख सकते हैं:
 IEnumerable<int> Fibonacci() { int a = 0, b = 1; while (true) { int sum = a + b; yield return sum; a = b; b = sum; } } //--- foreach (var i in Fibonacci().Take(10)) { Console.WriteLine(i); } 


शार्प में, उपज को कंपाइलर स्तर पर लागू किया जाता है: उपज विधि के शरीर से एक एन्यूमरेटर उत्पन्न होता है, जो एक राज्य मशीन है। अधिक विवरण यहां पाया जा सकता है।

और, हालांकि जावा के लिए उपज कार्यान्वयन हैं जो बायटेकोड को संशोधित करके और समान राज्य-मशीनों का निर्माण करके काम करते हैं, यह दृष्टिकोण बहुत जटिल है और कोड के साथ अतिरिक्त क्रियाओं की आवश्यकता है: संकलन का एक अतिरिक्त चरण या कस्टम क्लास लोडर का उपयोग।
हमारी समस्या को हल करने के लिए, हम दूसरे रास्ते पर जाएंगे - हम एक अतिरिक्त स्ट्रीम का उपयोग करेंगे।

कार्यान्वयन


इसलिए, यह विचार काफी पारदर्शी है - आपको एक अतिरिक्त स्ट्रीम का उपयोग करके निर्माता-उपभोक्ता योजना को लागू करने की आवश्यकता है, जो सीधे मुख्य स्ट्रीम के अनुरोध पर अगले अनुक्रम मान की गणना करेगा।

सिंक्रनाइज़ के साथ, हमारी उपज जनरेटर एक बाहरी फ़ंक्शन (कोटलिन कक्षाओं के बाहर कार्यों का समर्थन करता है) का उपयोग करके बनाया जाएगा, जो एक तर्क के रूप में एक और कार्य करेगा और वापस लौटेगा
 Iterable.   yieldfun .       : 
fun Test() = yieldfun<Int> { (context) -> for (val i in 0 .. 3) context.yield(i); }

context - , yield.

yieldfun , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> { return YieldIterable<T>(body) }
YieldIterable body - -. , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> = YieldIterable<T>(body)


Iterable , Iterator, YieldIterable :
private class YieldIterable<T>(val body: (YieldContext<T>) -> Unit): Iterable<T>{ public override fun iterator(): Iterator<T> = YieldIterator<T>(body) }


body ( trait) YieldContext, :
trait YieldContext<T> { fun yield(value : T) }

, yield(T) .

yield - YieldIterator:
private class YieldIterator<T> (val body: (YieldContext<T>) -> Unit): Iterator<T>, YieldContext<T> { private var thread: Thread? = null private val resultQueue = SynchronousQueue<Message>() private val continuationSync = SynchronousQueue<Any>() private var currentMessage: Message? = null { val r = object : Runnable { public override fun run() { try { continuationSync.take() body(this@YieldIterator) resultQueue.put(CompletedMessage()) } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) } } } thread = Thread(r) thread!!.start() } override fun yield(value: T) { resultQueue.put(ValueMessage(value)) continuationSync.take() } public override fun next(): T { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception if (currentMessage !is ValueMessage) throw NoSuchElementException() val value = (currentMessage as ValueMessage).value as T currentMessage = null return value } public override fun hasNext(): Boolean { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception return currentMessage is ValueMessage } private val dummy = Any() private inline fun evaluateNext() { if (currentMessage == null) { continuationSync.put(dummy) currentMessage = resultQueue.take() } } }

, , :
, . , - , put take , ( ).

resultQueue thread , - continuationSync - , .

, continuationSync . :
1) next() hasNext() , , (. evaluateNext()), .
2) body, .. .
3) - yield(T), ( ) , . , .
4) body , , .

, :
private open class Message { } private class ValueMessage(public val value: Any): Message() { } private class CompletedMessage: Message() { } private class ExceptionThrownMessage(public val exception: Throwable): Message() { }

, Test() :
assertEquals(listOf(0, 1, 2, 3), Test().toList())


yieldfun .

context
, , . it :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) it.yield(i); }
.
.. Extension function literals - body , body . YieldContext :
public fun yieldfun<T: Any>(body: YieldContext<T>.() -> Unit): Iterable<T> = YieldIterable<T>(body)

body .
? :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) yield(i); }
.. yieldfun YieldContext, YieldContext this :
this.yield(i)

, this ,
yield(i)
.. body , :
continuationSync.take() body() resultQueue.put(CompletedMessage())
, YieldIterator YieldContext, body - extension-.


. - yield :)

YieldContext ret :
public trait YieldContext<T> { fun yield(value: T): Unit val ret: YieldContext<T> }

YieldIterator this :
override val ret: YieldContext<T> = this
? :
ret.yield(i)
, yield(T) this , :
ret yield i
, .

, C# Kotlin:
fun fibonacci(): Iterable<Int> = yieldfun { var a = 0 var b = 1 while (true) { val sum = a + b; ret yield sum; a = b; b = sum; } }
, , generic- yieldfun - .


, , . . finalize() :
protected fun finalize() { thread?.interrupt() }
catch :
try { continuationSync.take() body() resultQueue.put(CompletedMessage()) } catch (e: InterruptedException) { // if not all items were iterated yield will wait for signal, so finalizer should // interrupt the thread to exit } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) }
, continuationSync , .
, , , yieldfun , InterruptedException , yield try-catch , .


, extension- .Net, .. , "" . , , Jetbrains. , Kotlin .

UPD. bitbucket.org/Nerzhul500/kyield/src
Iterable. yieldfun . :
fun Test() = yieldfun<Int> { (context) -> for (val i in 0 .. 3) context.yield(i); }

context - , yield.

yieldfun , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> { return YieldIterable<T>(body) }
YieldIterable body - -. , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> = YieldIterable<T>(body)


Iterable , Iterator, YieldIterable :
private class YieldIterable<T>(val body: (YieldContext<T>) -> Unit): Iterable<T>{ public override fun iterator(): Iterator<T> = YieldIterator<T>(body) }


body ( trait) YieldContext, :
trait YieldContext<T> { fun yield(value : T) }

, yield(T) .

yield - YieldIterator:
private class YieldIterator<T> (val body: (YieldContext<T>) -> Unit): Iterator<T>, YieldContext<T> { private var thread: Thread? = null private val resultQueue = SynchronousQueue<Message>() private val continuationSync = SynchronousQueue<Any>() private var currentMessage: Message? = null { val r = object : Runnable { public override fun run() { try { continuationSync.take() body(this@YieldIterator) resultQueue.put(CompletedMessage()) } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) } } } thread = Thread(r) thread!!.start() } override fun yield(value: T) { resultQueue.put(ValueMessage(value)) continuationSync.take() } public override fun next(): T { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception if (currentMessage !is ValueMessage) throw NoSuchElementException() val value = (currentMessage as ValueMessage).value as T currentMessage = null return value } public override fun hasNext(): Boolean { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception return currentMessage is ValueMessage } private val dummy = Any() private inline fun evaluateNext() { if (currentMessage == null) { continuationSync.put(dummy) currentMessage = resultQueue.take() } } }

, , :
, . , - , put take , ( ).

resultQueue thread , - continuationSync - , .

, continuationSync . :
1) next() hasNext() , , (. evaluateNext()), .
2) body, .. .
3) - yield(T), ( ) , . , .
4) body , , .

, :
private open class Message { } private class ValueMessage(public val value: Any): Message() { } private class CompletedMessage: Message() { } private class ExceptionThrownMessage(public val exception: Throwable): Message() { }

, Test() :
assertEquals(listOf(0, 1, 2, 3), Test().toList())


yieldfun .

context
, , . it :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) it.yield(i); }
.
.. Extension function literals - body , body . YieldContext :
public fun yieldfun<T: Any>(body: YieldContext<T>.() -> Unit): Iterable<T> = YieldIterable<T>(body)

body .
? :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) yield(i); }
.. yieldfun YieldContext, YieldContext this :
this.yield(i)

, this ,
yield(i)
.. body , :
continuationSync.take() body() resultQueue.put(CompletedMessage())
, YieldIterator YieldContext, body - extension-.


. - yield :)

YieldContext ret :
public trait YieldContext<T> { fun yield(value: T): Unit val ret: YieldContext<T> }

YieldIterator this :
override val ret: YieldContext<T> = this
? :
ret.yield(i)
, yield(T) this , :
ret yield i
, .

, C# Kotlin:
fun fibonacci(): Iterable<Int> = yieldfun { var a = 0 var b = 1 while (true) { val sum = a + b; ret yield sum; a = b; b = sum; } }
, , generic- yieldfun - .


, , . . finalize() :
protected fun finalize() { thread?.interrupt() }
catch :
try { continuationSync.take() body() resultQueue.put(CompletedMessage()) } catch (e: InterruptedException) { // if not all items were iterated yield will wait for signal, so finalizer should // interrupt the thread to exit } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) }
, continuationSync , .
, , , yieldfun , InterruptedException , yield try-catch , .


, extension- .Net, .. , "" . , , Jetbrains. , Kotlin .

UPD. bitbucket.org/Nerzhul500/kyield/src
 Iterable.   yieldfun .       : 
fun Test() = yieldfun<Int> { (context) -> for (val i in 0 .. 3) context.yield(i); }

context - , yield.

yieldfun , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> { return YieldIterable<T>(body) }
YieldIterable body - -. , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> = YieldIterable<T>(body)


Iterable , Iterator, YieldIterable :
private class YieldIterable<T>(val body: (YieldContext<T>) -> Unit): Iterable<T>{ public override fun iterator(): Iterator<T> = YieldIterator<T>(body) }


body ( trait) YieldContext, :
trait YieldContext<T> { fun yield(value : T) }

, yield(T) .

yield - YieldIterator:
private class YieldIterator<T> (val body: (YieldContext<T>) -> Unit): Iterator<T>, YieldContext<T> { private var thread: Thread? = null private val resultQueue = SynchronousQueue<Message>() private val continuationSync = SynchronousQueue<Any>() private var currentMessage: Message? = null { val r = object : Runnable { public override fun run() { try { continuationSync.take() body(this@YieldIterator) resultQueue.put(CompletedMessage()) } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) } } } thread = Thread(r) thread!!.start() } override fun yield(value: T) { resultQueue.put(ValueMessage(value)) continuationSync.take() } public override fun next(): T { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception if (currentMessage !is ValueMessage) throw NoSuchElementException() val value = (currentMessage as ValueMessage).value as T currentMessage = null return value } public override fun hasNext(): Boolean { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception return currentMessage is ValueMessage } private val dummy = Any() private inline fun evaluateNext() { if (currentMessage == null) { continuationSync.put(dummy) currentMessage = resultQueue.take() } } }

, , :
, . , - , put take , ( ).

resultQueue thread , - continuationSync - , .

, continuationSync . :
1) next() hasNext() , , (. evaluateNext()), .
2) body, .. .
3) - yield(T), ( ) , . , .
4) body , , .

, :
private open class Message { } private class ValueMessage(public val value: Any): Message() { } private class CompletedMessage: Message() { } private class ExceptionThrownMessage(public val exception: Throwable): Message() { }

, Test() :
assertEquals(listOf(0, 1, 2, 3), Test().toList())


yieldfun .

context
, , . it :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) it.yield(i); }
.
.. Extension function literals - body , body . YieldContext :
public fun yieldfun<T: Any>(body: YieldContext<T>.() -> Unit): Iterable<T> = YieldIterable<T>(body)

body .
? :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) yield(i); }
.. yieldfun YieldContext, YieldContext this :
this.yield(i)

, this ,
yield(i)
.. body , :
continuationSync.take() body() resultQueue.put(CompletedMessage())
, YieldIterator YieldContext, body - extension-.


. - yield :)

YieldContext ret :
public trait YieldContext<T> { fun yield(value: T): Unit val ret: YieldContext<T> }

YieldIterator this :
override val ret: YieldContext<T> = this
? :
ret.yield(i)
, yield(T) this , :
ret yield i
, .

, C# Kotlin:
fun fibonacci(): Iterable<Int> = yieldfun { var a = 0 var b = 1 while (true) { val sum = a + b; ret yield sum; a = b; b = sum; } }
, , generic- yieldfun - .


, , . . finalize() :
protected fun finalize() { thread?.interrupt() }
catch :
try { continuationSync.take() body() resultQueue.put(CompletedMessage()) } catch (e: InterruptedException) { // if not all items were iterated yield will wait for signal, so finalizer should // interrupt the thread to exit } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) }
, continuationSync , .
, , , yieldfun , InterruptedException , yield try-catch , .


, extension- .Net, .. , "" . , , Jetbrains. , Kotlin .

UPD. bitbucket.org/Nerzhul500/kyield/src
Iterable. yieldfun . :
fun Test() = yieldfun<Int> { (context) -> for (val i in 0 .. 3) context.yield(i); }

context - , yield.

yieldfun , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> { return YieldIterable<T>(body) }
YieldIterable body - -. , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> = YieldIterable<T>(body)


Iterable , Iterator, YieldIterable :
private class YieldIterable<T>(val body: (YieldContext<T>) -> Unit): Iterable<T>{ public override fun iterator(): Iterator<T> = YieldIterator<T>(body) }


body ( trait) YieldContext, :
trait YieldContext<T> { fun yield(value : T) }

, yield(T) .

yield - YieldIterator:
private class YieldIterator<T> (val body: (YieldContext<T>) -> Unit): Iterator<T>, YieldContext<T> { private var thread: Thread? = null private val resultQueue = SynchronousQueue<Message>() private val continuationSync = SynchronousQueue<Any>() private var currentMessage: Message? = null { val r = object : Runnable { public override fun run() { try { continuationSync.take() body(this@YieldIterator) resultQueue.put(CompletedMessage()) } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) } } } thread = Thread(r) thread!!.start() } override fun yield(value: T) { resultQueue.put(ValueMessage(value)) continuationSync.take() } public override fun next(): T { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception if (currentMessage !is ValueMessage) throw NoSuchElementException() val value = (currentMessage as ValueMessage).value as T currentMessage = null return value } public override fun hasNext(): Boolean { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception return currentMessage is ValueMessage } private val dummy = Any() private inline fun evaluateNext() { if (currentMessage == null) { continuationSync.put(dummy) currentMessage = resultQueue.take() } } }

, , :
, . , - , put take , ( ).

resultQueue thread , - continuationSync - , .

, continuationSync . :
1) next() hasNext() , , (. evaluateNext()), .
2) body, .. .
3) - yield(T), ( ) , . , .
4) body , , .

, :
private open class Message { } private class ValueMessage(public val value: Any): Message() { } private class CompletedMessage: Message() { } private class ExceptionThrownMessage(public val exception: Throwable): Message() { }

, Test() :
assertEquals(listOf(0, 1, 2, 3), Test().toList())


yieldfun .

context
, , . it :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) it.yield(i); }
.
.. Extension function literals - body , body . YieldContext :
public fun yieldfun<T: Any>(body: YieldContext<T>.() -> Unit): Iterable<T> = YieldIterable<T>(body)

body .
? :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) yield(i); }
.. yieldfun YieldContext, YieldContext this :
this.yield(i)

, this ,
yield(i)
.. body , :
continuationSync.take() body() resultQueue.put(CompletedMessage())
, YieldIterator YieldContext, body - extension-.


. - yield :)

YieldContext ret :
public trait YieldContext<T> { fun yield(value: T): Unit val ret: YieldContext<T> }

YieldIterator this :
override val ret: YieldContext<T> = this
? :
ret.yield(i)
, yield(T) this , :
ret yield i
, .

, C# Kotlin:
fun fibonacci(): Iterable<Int> = yieldfun { var a = 0 var b = 1 while (true) { val sum = a + b; ret yield sum; a = b; b = sum; } }
, , generic- yieldfun - .


, , . . finalize() :
protected fun finalize() { thread?.interrupt() }
catch :
try { continuationSync.take() body() resultQueue.put(CompletedMessage()) } catch (e: InterruptedException) { // if not all items were iterated yield will wait for signal, so finalizer should // interrupt the thread to exit } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) }
, continuationSync , .
, , , yieldfun , InterruptedException , yield try-catch , .


, extension- .Net, .. , "" . , , Jetbrains. , Kotlin .

UPD. bitbucket.org/Nerzhul500/kyield/src
 Iterable.   yieldfun .       : 
fun Test() = yieldfun<Int> { (context) -> for (val i in 0 .. 3) context.yield(i); }

context - , yield.

yieldfun , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> { return YieldIterable<T>(body) }
YieldIterable body - -. , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> = YieldIterable<T>(body)

Iterable , Iterator, YieldIterable :
private class YieldIterable<T>(val body: (YieldContext<T>) -> Unit): Iterable<T>{ public override fun iterator(): Iterator<T> = YieldIterator<T>(body) }


body ( trait) YieldContext, :
trait YieldContext<T> { fun yield(value : T) }

, yield(T) .

yield - YieldIterator:
private class YieldIterator<T> (val body: (YieldContext<T>) -> Unit): Iterator<T>, YieldContext<T> { private var thread: Thread? = null private val resultQueue = SynchronousQueue<Message>() private val continuationSync = SynchronousQueue<Any>() private var currentMessage: Message? = null { val r = object : Runnable { public override fun run() { try { continuationSync.take() body(this@YieldIterator) resultQueue.put(CompletedMessage()) } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) } } } thread = Thread(r) thread!!.start() } override fun yield(value: T) { resultQueue.put(ValueMessage(value)) continuationSync.take() } public override fun next(): T { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception if (currentMessage !is ValueMessage) throw NoSuchElementException() val value = (currentMessage as ValueMessage).value as T currentMessage = null return value } public override fun hasNext(): Boolean { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception return currentMessage is ValueMessage } private val dummy = Any() private inline fun evaluateNext() { if (currentMessage == null) { continuationSync.put(dummy) currentMessage = resultQueue.take() } } }

, , :
, . , - , put take , ( ).

resultQueue thread , - continuationSync - , .

, continuationSync . :
1) next() hasNext() , , (. evaluateNext()), .
2) body, .. .
3) - yield(T), ( ) , . , .
4) body , , .

, :
private open class Message { } private class ValueMessage(public val value: Any): Message() { } private class CompletedMessage: Message() { } private class ExceptionThrownMessage(public val exception: Throwable): Message() { }

, Test() :
assertEquals(listOf(0, 1, 2, 3), Test().toList())


yieldfun .

context
, , . it :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) it.yield(i); }
.
.. Extension function literals - body , body . YieldContext :
public fun yieldfun<T: Any>(body: YieldContext<T>.() -> Unit): Iterable<T> = YieldIterable<T>(body)

body .
? :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) yield(i); }
.. yieldfun YieldContext, YieldContext this :
this.yield(i)

, this ,
yield(i)
.. body , :
continuationSync.take() body() resultQueue.put(CompletedMessage())
, YieldIterator YieldContext, body - extension-.


. - yield :)

YieldContext ret :
public trait YieldContext<T> { fun yield(value: T): Unit val ret: YieldContext<T> }

YieldIterator this :
override val ret: YieldContext<T> = this
? :
ret.yield(i)
, yield(T) this , :
ret yield i
, .

, C# Kotlin:
fun fibonacci(): Iterable<Int> = yieldfun { var a = 0 var b = 1 while (true) { val sum = a + b; ret yield sum; a = b; b = sum; } }
, , generic- yieldfun - .


, , . . finalize() :
protected fun finalize() { thread?.interrupt() }
catch :
try { continuationSync.take() body() resultQueue.put(CompletedMessage()) } catch (e: InterruptedException) { // if not all items were iterated yield will wait for signal, so finalizer should // interrupt the thread to exit } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) }
, continuationSync , .
, , , yieldfun , InterruptedException , yield try-catch , .


, extension- .Net, .. , "" . , , Jetbrains. , Kotlin .

UPD. bitbucket.org/Nerzhul500/kyield/src
Iterable. yieldfun . :
fun Test() = yieldfun<Int> { (context) -> for (val i in 0 .. 3) context.yield(i); }

context - , yield.

yieldfun , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> { return YieldIterable<T>(body) }
YieldIterable body - -. , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> = YieldIterable<T>(body)


Iterable , Iterator, YieldIterable :
private class YieldIterable<T>(val body: (YieldContext<T>) -> Unit): Iterable<T>{ public override fun iterator(): Iterator<T> = YieldIterator<T>(body) }


body ( trait) YieldContext, :
trait YieldContext<T> { fun yield(value : T) }

, yield(T) .

yield - YieldIterator:
private class YieldIterator<T> (val body: (YieldContext<T>) -> Unit): Iterator<T>, YieldContext<T> { private var thread: Thread? = null private val resultQueue = SynchronousQueue<Message>() private val continuationSync = SynchronousQueue<Any>() private var currentMessage: Message? = null { val r = object : Runnable { public override fun run() { try { continuationSync.take() body(this@YieldIterator) resultQueue.put(CompletedMessage()) } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) } } } thread = Thread(r) thread!!.start() } override fun yield(value: T) { resultQueue.put(ValueMessage(value)) continuationSync.take() } public override fun next(): T { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception if (currentMessage !is ValueMessage) throw NoSuchElementException() val value = (currentMessage as ValueMessage).value as T currentMessage = null return value } public override fun hasNext(): Boolean { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception return currentMessage is ValueMessage } private val dummy = Any() private inline fun evaluateNext() { if (currentMessage == null) { continuationSync.put(dummy) currentMessage = resultQueue.take() } } }

, , :
, . , - , put take , ( ).

resultQueue thread , - continuationSync - , .

, continuationSync . :
1) next() hasNext() , , (. evaluateNext()), .
2) body, .. .
3) - yield(T), ( ) , . , .
4) body , , .

, :
private open class Message { } private class ValueMessage(public val value: Any): Message() { } private class CompletedMessage: Message() { } private class ExceptionThrownMessage(public val exception: Throwable): Message() { }

, Test() :
assertEquals(listOf(0, 1, 2, 3), Test().toList())


yieldfun .

context
, , . it :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) it.yield(i); }
.
.. Extension function literals - body , body . YieldContext :
public fun yieldfun<T: Any>(body: YieldContext<T>.() -> Unit): Iterable<T> = YieldIterable<T>(body)

body .
? :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) yield(i); }
.. yieldfun YieldContext, YieldContext this :
this.yield(i)

, this ,
yield(i)
.. body , :
continuationSync.take() body() resultQueue.put(CompletedMessage())
, YieldIterator YieldContext, body - extension-.


. - yield :)

YieldContext ret :
public trait YieldContext<T> { fun yield(value: T): Unit val ret: YieldContext<T> }

YieldIterator this :
override val ret: YieldContext<T> = this
? :
ret.yield(i)
, yield(T) this , :
ret yield i
, .

, C# Kotlin:
fun fibonacci(): Iterable<Int> = yieldfun { var a = 0 var b = 1 while (true) { val sum = a + b; ret yield sum; a = b; b = sum; } }
, , generic- yieldfun - .


, , . . finalize() :
protected fun finalize() { thread?.interrupt() }
catch :
try { continuationSync.take() body() resultQueue.put(CompletedMessage()) } catch (e: InterruptedException) { // if not all items were iterated yield will wait for signal, so finalizer should // interrupt the thread to exit } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) }
, continuationSync , .
, , , yieldfun , InterruptedException , yield try-catch , .


, extension- .Net, .. , "" . , , Jetbrains. , Kotlin .

UPD. bitbucket.org/Nerzhul500/kyield/src
 Iterable.   yieldfun .       : 
fun Test() = yieldfun<Int> { (context) -> for (val i in 0 .. 3) context.yield(i); }

context - , yield.

yieldfun , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> { return YieldIterable<T>(body) }
YieldIterable body - -. , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> = YieldIterable<T>(body)


Iterable , Iterator, YieldIterable :
private class YieldIterable<T>(val body: (YieldContext<T>) -> Unit): Iterable<T>{ public override fun iterator(): Iterator<T> = YieldIterator<T>(body) }


body ( trait) YieldContext, :
trait YieldContext<T> { fun yield(value : T) }

, yield(T) .

yield - YieldIterator:
private class YieldIterator<T> (val body: (YieldContext<T>) -> Unit): Iterator<T>, YieldContext<T> { private var thread: Thread? = null private val resultQueue = SynchronousQueue<Message>() private val continuationSync = SynchronousQueue<Any>() private var currentMessage: Message? = null { val r = object : Runnable { public override fun run() { try { continuationSync.take() body(this@YieldIterator) resultQueue.put(CompletedMessage()) } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) } } } thread = Thread(r) thread!!.start() } override fun yield(value: T) { resultQueue.put(ValueMessage(value)) continuationSync.take() } public override fun next(): T { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception if (currentMessage !is ValueMessage) throw NoSuchElementException() val value = (currentMessage as ValueMessage).value as T currentMessage = null return value } public override fun hasNext(): Boolean { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception return currentMessage is ValueMessage } private val dummy = Any() private inline fun evaluateNext() { if (currentMessage == null) { continuationSync.put(dummy) currentMessage = resultQueue.take() } } }

, , :
, . , - , put take , ( ).

resultQueue thread , - continuationSync - , .

, continuationSync . :
1) next() hasNext() , , (. evaluateNext()), .
2) body, .. .
3) - yield(T), ( ) , . , .
4) body , , .

, :
private open class Message { } private class ValueMessage(public val value: Any): Message() { } private class CompletedMessage: Message() { } private class ExceptionThrownMessage(public val exception: Throwable): Message() { }

, Test() :
assertEquals(listOf(0, 1, 2, 3), Test().toList())


yieldfun .

context
, , . it :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) it.yield(i); }
.
.. Extension function literals - body , body . YieldContext :
public fun yieldfun<T: Any>(body: YieldContext<T>.() -> Unit): Iterable<T> = YieldIterable<T>(body)

body .
? :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) yield(i); }
.. yieldfun YieldContext, YieldContext this :
this.yield(i)

, this ,
yield(i)
.. body , :
continuationSync.take() body() resultQueue.put(CompletedMessage())
, YieldIterator YieldContext, body - extension-.


. - yield :)

YieldContext ret :
public trait YieldContext<T> { fun yield(value: T): Unit val ret: YieldContext<T> }

YieldIterator this :
override val ret: YieldContext<T> = this
? :
ret.yield(i)
, yield(T) this , :
ret yield i
, .

, C# Kotlin:
fun fibonacci(): Iterable<Int> = yieldfun { var a = 0 var b = 1 while (true) { val sum = a + b; ret yield sum; a = b; b = sum; } }
, , generic- yieldfun - .


, , . . finalize() :
protected fun finalize() { thread?.interrupt() }
catch :
try { continuationSync.take() body() resultQueue.put(CompletedMessage()) } catch (e: InterruptedException) { // if not all items were iterated yield will wait for signal, so finalizer should // interrupt the thread to exit } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) }
, continuationSync , .
, , , yieldfun , InterruptedException , yield try-catch , .


, extension- .Net, .. , "" . , , Jetbrains. , Kotlin .

UPD. bitbucket.org/Nerzhul500/kyield/src
Iterable. yieldfun . :
fun Test() = yieldfun<Int> { (context) -> for (val i in 0 .. 3) context.yield(i); }

context - , yield.

yieldfun , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> { return YieldIterable<T>(body) }
YieldIterable body - -. , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> = YieldIterable<T>(body)


Iterable , Iterator, YieldIterable :
private class YieldIterable<T>(val body: (YieldContext<T>) -> Unit): Iterable<T>{ public override fun iterator(): Iterator<T> = YieldIterator<T>(body) }


body ( trait) YieldContext, :
trait YieldContext<T> { fun yield(value : T) }

, yield(T) .

yield - YieldIterator:
private class YieldIterator<T> (val body: (YieldContext<T>) -> Unit): Iterator<T>, YieldContext<T> { private var thread: Thread? = null private val resultQueue = SynchronousQueue<Message>() private val continuationSync = SynchronousQueue<Any>() private var currentMessage: Message? = null { val r = object : Runnable { public override fun run() { try { continuationSync.take() body(this@YieldIterator) resultQueue.put(CompletedMessage()) } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) } } } thread = Thread(r) thread!!.start() } override fun yield(value: T) { resultQueue.put(ValueMessage(value)) continuationSync.take() } public override fun next(): T { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception if (currentMessage !is ValueMessage) throw NoSuchElementException() val value = (currentMessage as ValueMessage).value as T currentMessage = null return value } public override fun hasNext(): Boolean { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception return currentMessage is ValueMessage } private val dummy = Any() private inline fun evaluateNext() { if (currentMessage == null) { continuationSync.put(dummy) currentMessage = resultQueue.take() } } }

, , :
, . , - , put take , ( ).

resultQueue thread , - continuationSync - , .

, continuationSync . :
1) next() hasNext() , , (. evaluateNext()), .
2) body, .. .
3) - yield(T), ( ) , . , .
4) body , , .

, :
private open class Message { } private class ValueMessage(public val value: Any): Message() { } private class CompletedMessage: Message() { } private class ExceptionThrownMessage(public val exception: Throwable): Message() { }

, Test() :
assertEquals(listOf(0, 1, 2, 3), Test().toList())


yieldfun .

context
, , . it :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) it.yield(i); }
.
.. Extension function literals - body , body . YieldContext :
public fun yieldfun<T: Any>(body: YieldContext<T>.() -> Unit): Iterable<T> = YieldIterable<T>(body)

body .
? :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) yield(i); }
.. yieldfun YieldContext, YieldContext this :
this.yield(i)

, this ,
yield(i)
.. body , :
continuationSync.take() body() resultQueue.put(CompletedMessage())
, YieldIterator YieldContext, body - extension-.


. - yield :)

YieldContext ret :
public trait YieldContext<T> { fun yield(value: T): Unit val ret: YieldContext<T> }

YieldIterator this :
override val ret: YieldContext<T> = this
? :
ret.yield(i)
, yield(T) this , :
ret yield i
, .

, C# Kotlin:
fun fibonacci(): Iterable<Int> = yieldfun { var a = 0 var b = 1 while (true) { val sum = a + b; ret yield sum; a = b; b = sum; } }
, , generic- yieldfun - .


, , . . finalize() :
protected fun finalize() { thread?.interrupt() }
catch :
try { continuationSync.take() body() resultQueue.put(CompletedMessage()) } catch (e: InterruptedException) { // if not all items were iterated yield will wait for signal, so finalizer should // interrupt the thread to exit } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) }
, continuationSync , .
, , , yieldfun , InterruptedException , yield try-catch , .


, extension- .Net, .. , "" . , , Jetbrains. , Kotlin .

UPD. bitbucket.org/Nerzhul500/kyield/src
 Iterable.   yieldfun .       : 
fun Test() = yieldfun<Int> { (context) -> for (val i in 0 .. 3) context.yield(i); }

context - , yield.

yieldfun , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> { return YieldIterable<T>(body) }
YieldIterable body - -. , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> = YieldIterable<T>(body)


Iterable , Iterator, YieldIterable :
private class YieldIterable<T>(val body: (YieldContext<T>) -> Unit): Iterable<T>{ public override fun iterator(): Iterator<T> = YieldIterator<T>(body) }


body ( trait) YieldContext, :
trait YieldContext<T> { fun yield(value : T) }

, yield(T) .

yield - YieldIterator:
private class YieldIterator<T> (val body: (YieldContext<T>) -> Unit): Iterator<T>, YieldContext<T> { private var thread: Thread? = null private val resultQueue = SynchronousQueue<Message>() private val continuationSync = SynchronousQueue<Any>() private var currentMessage: Message? = null { val r = object : Runnable { public override fun run() { try { continuationSync.take() body(this@YieldIterator) resultQueue.put(CompletedMessage()) } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) } } } thread = Thread(r) thread!!.start() } override fun yield(value: T) { resultQueue.put(ValueMessage(value)) continuationSync.take() } public override fun next(): T { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception if (currentMessage !is ValueMessage) throw NoSuchElementException() val value = (currentMessage as ValueMessage).value as T currentMessage = null return value } public override fun hasNext(): Boolean { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception return currentMessage is ValueMessage } private val dummy = Any() private inline fun evaluateNext() { if (currentMessage == null) { continuationSync.put(dummy) currentMessage = resultQueue.take() } } }

, , :
, . , - , put take , ( ).

resultQueue thread , - continuationSync - , .

, continuationSync . :
1) next() hasNext() , , (. evaluateNext()), .
2) body, .. .
3) - yield(T), ( ) , . , .
4) body , , .

, :
private open class Message { } private class ValueMessage(public val value: Any): Message() { } private class CompletedMessage: Message() { } private class ExceptionThrownMessage(public val exception: Throwable): Message() { }

, Test() :
assertEquals(listOf(0, 1, 2, 3), Test().toList())


yieldfun .

context
, , . it :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) it.yield(i); }
.
.. Extension function literals - body , body . YieldContext :
public fun yieldfun<T: Any>(body: YieldContext<T>.() -> Unit): Iterable<T> = YieldIterable<T>(body)

body .
? :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) yield(i); }
.. yieldfun YieldContext, YieldContext this :
this.yield(i)

, this ,
yield(i)
.. body , :
continuationSync.take() body() resultQueue.put(CompletedMessage())
, YieldIterator YieldContext, body - extension-.


. - yield :)

YieldContext ret :
public trait YieldContext<T> { fun yield(value: T): Unit val ret: YieldContext<T> }

YieldIterator this :
override val ret: YieldContext<T> = this
? :
ret.yield(i)
, yield(T) this , :
ret yield i
, .

, C# Kotlin:
fun fibonacci(): Iterable<Int> = yieldfun { var a = 0 var b = 1 while (true) { val sum = a + b; ret yield sum; a = b; b = sum; } }
, , generic- yieldfun - .


, , . . finalize() :
protected fun finalize() { thread?.interrupt() }
catch :
try { continuationSync.take() body() resultQueue.put(CompletedMessage()) } catch (e: InterruptedException) { // if not all items were iterated yield will wait for signal, so finalizer should // interrupt the thread to exit } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) }
, continuationSync , .
, , , yieldfun , InterruptedException , yield try-catch , .


, extension- .Net, .. , "" . , , Jetbrains. , Kotlin .

UPD. bitbucket.org/Nerzhul500/kyield/src
Iterable. yieldfun . :
fun Test() = yieldfun<Int> { (context) -> for (val i in 0 .. 3) context.yield(i); }

context - , yield.

yieldfun , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> { return YieldIterable<T>(body) }
YieldIterable body - -. , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> = YieldIterable<T>(body)


Iterable , Iterator, YieldIterable :
private class YieldIterable<T>(val body: (YieldContext<T>) -> Unit): Iterable<T>{ public override fun iterator(): Iterator<T> = YieldIterator<T>(body) }


body ( trait) YieldContext, :
trait YieldContext<T> { fun yield(value : T) }

, yield(T) .

yield - YieldIterator:
private class YieldIterator<T> (val body: (YieldContext<T>) -> Unit): Iterator<T>, YieldContext<T> { private var thread: Thread? = null private val resultQueue = SynchronousQueue<Message>() private val continuationSync = SynchronousQueue<Any>() private var currentMessage: Message? = null { val r = object : Runnable { public override fun run() { try { continuationSync.take() body(this@YieldIterator) resultQueue.put(CompletedMessage()) } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) } } } thread = Thread(r) thread!!.start() } override fun yield(value: T) { resultQueue.put(ValueMessage(value)) continuationSync.take() } public override fun next(): T { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception if (currentMessage !is ValueMessage) throw NoSuchElementException() val value = (currentMessage as ValueMessage).value as T currentMessage = null return value } public override fun hasNext(): Boolean { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception return currentMessage is ValueMessage } private val dummy = Any() private inline fun evaluateNext() { if (currentMessage == null) { continuationSync.put(dummy) currentMessage = resultQueue.take() } } }

, , :
, . , - , put take , ( ).

resultQueue thread , - continuationSync - , .

, continuationSync . :
1) next() hasNext() , , (. evaluateNext()), .
2) body, .. .
3) - yield(T), ( ) , . , .
4) body , , .

, :
private open class Message { } private class ValueMessage(public val value: Any): Message() { } private class CompletedMessage: Message() { } private class ExceptionThrownMessage(public val exception: Throwable): Message() { }

, Test() :
assertEquals(listOf(0, 1, 2, 3), Test().toList())


yieldfun .

context
, , . it :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) it.yield(i); }
.
.. Extension function literals - body , body . YieldContext :
public fun yieldfun<T: Any>(body: YieldContext<T>.() -> Unit): Iterable<T> = YieldIterable<T>(body)

body .
? :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) yield(i); }
.. yieldfun YieldContext, YieldContext this :
this.yield(i)

, this ,
yield(i)
.. body , :
continuationSync.take() body() resultQueue.put(CompletedMessage())
, YieldIterator YieldContext, body - extension-.


. - yield :)

YieldContext ret :
public trait YieldContext<T> { fun yield(value: T): Unit val ret: YieldContext<T> }

YieldIterator this :
override val ret: YieldContext<T> = this
? :
ret.yield(i)
, yield(T) this , :
ret yield i
, .

, C# Kotlin:
fun fibonacci(): Iterable<Int> = yieldfun { var a = 0 var b = 1 while (true) { val sum = a + b; ret yield sum; a = b; b = sum; } }
, , generic- yieldfun - .


, , . . finalize() :
protected fun finalize() { thread?.interrupt() }
catch :
try { continuationSync.take() body() resultQueue.put(CompletedMessage()) } catch (e: InterruptedException) { // if not all items were iterated yield will wait for signal, so finalizer should // interrupt the thread to exit } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) }
, continuationSync , .
, , , yieldfun , InterruptedException , yield try-catch , .


, extension- .Net, .. , "" . , , Jetbrains. , Kotlin .

UPD. bitbucket.org/Nerzhul500/kyield/src
 Iterable.   yieldfun .       : 
fun Test() = yieldfun<Int> { (context) -> for (val i in 0 .. 3) context.yield(i); }

context - , yield.

yieldfun , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> { return YieldIterable<T>(body) }
YieldIterable body - -. , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> = YieldIterable<T>(body)


Iterable , Iterator, YieldIterable :
private class YieldIterable<T>(val body: (YieldContext<T>) -> Unit): Iterable<T>{ public override fun iterator(): Iterator<T> = YieldIterator<T>(body) }


body ( trait) YieldContext, :
trait YieldContext<T> { fun yield(value : T) }

, yield(T) .

yield - YieldIterator:
private class YieldIterator<T> (val body: (YieldContext<T>) -> Unit): Iterator<T>, YieldContext<T> { private var thread: Thread? = null private val resultQueue = SynchronousQueue<Message>() private val continuationSync = SynchronousQueue<Any>() private var currentMessage: Message? = null { val r = object : Runnable { public override fun run() { try { continuationSync.take() body(this@YieldIterator) resultQueue.put(CompletedMessage()) } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) } } } thread = Thread(r) thread!!.start() } override fun yield(value: T) { resultQueue.put(ValueMessage(value)) continuationSync.take() } public override fun next(): T { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception if (currentMessage !is ValueMessage) throw NoSuchElementException() val value = (currentMessage as ValueMessage).value as T currentMessage = null return value } public override fun hasNext(): Boolean { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception return currentMessage is ValueMessage } private val dummy = Any() private inline fun evaluateNext() { if (currentMessage == null) { continuationSync.put(dummy) currentMessage = resultQueue.take() } } }

, , :
, . , - , put take , ( ).

resultQueue thread , - continuationSync - , .

, continuationSync . :
1) next() hasNext() , , (. evaluateNext()), .
2) body, .. .
3) - yield(T), ( ) , . , .
4) body , , .

, :
private open class Message { } private class ValueMessage(public val value: Any): Message() { } private class CompletedMessage: Message() { } private class ExceptionThrownMessage(public val exception: Throwable): Message() { }

, Test() :
assertEquals(listOf(0, 1, 2, 3), Test().toList())


yieldfun .

context
, , . it :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) it.yield(i); }
.
.. Extension function literals - body , body . YieldContext :
public fun yieldfun<T: Any>(body: YieldContext<T>.() -> Unit): Iterable<T> = YieldIterable<T>(body)

body .
? :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) yield(i); }
.. yieldfun YieldContext, YieldContext this :
this.yield(i)

, this ,
yield(i)
.. body , :
continuationSync.take() body() resultQueue.put(CompletedMessage())
, YieldIterator YieldContext, body - extension-.


. - yield :)

YieldContext ret :
public trait YieldContext<T> { fun yield(value: T): Unit val ret: YieldContext<T> }

YieldIterator this :
override val ret: YieldContext<T> = this
? :
ret.yield(i)
, yield(T) this , :
ret yield i
, .

, C# Kotlin:
fun fibonacci(): Iterable<Int> = yieldfun { var a = 0 var b = 1 while (true) { val sum = a + b; ret yield sum; a = b; b = sum; } }
, , generic- yieldfun - .


, , . . finalize() :
protected fun finalize() { thread?.interrupt() }
catch :
try { continuationSync.take() body() resultQueue.put(CompletedMessage()) } catch (e: InterruptedException) { // if not all items were iterated yield will wait for signal, so finalizer should // interrupt the thread to exit } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) }
, continuationSync , .
, , , yieldfun , InterruptedException , yield try-catch , .


, extension- .Net, .. , "" . , , Jetbrains. , Kotlin .

UPD. bitbucket.org/Nerzhul500/kyield/src
Iterable. yieldfun . :
fun Test() = yieldfun<Int> { (context) -> for (val i in 0 .. 3) context.yield(i); }

context - , yield.

yieldfun , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> { return YieldIterable<T>(body) }
YieldIterable body - -. , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> = YieldIterable<T>(body)


Iterable , Iterator, YieldIterable :
private class YieldIterable<T>(val body: (YieldContext<T>) -> Unit): Iterable<T>{ public override fun iterator(): Iterator<T> = YieldIterator<T>(body) }


body ( trait) YieldContext, :
trait YieldContext<T> { fun yield(value : T) }

, yield(T) .

yield - YieldIterator:
private class YieldIterator<T> (val body: (YieldContext<T>) -> Unit): Iterator<T>, YieldContext<T> { private var thread: Thread? = null private val resultQueue = SynchronousQueue<Message>() private val continuationSync = SynchronousQueue<Any>() private var currentMessage: Message? = null { val r = object : Runnable { public override fun run() { try { continuationSync.take() body(this@YieldIterator) resultQueue.put(CompletedMessage()) } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) } } } thread = Thread(r) thread!!.start() } override fun yield(value: T) { resultQueue.put(ValueMessage(value)) continuationSync.take() } public override fun next(): T { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception if (currentMessage !is ValueMessage) throw NoSuchElementException() val value = (currentMessage as ValueMessage).value as T currentMessage = null return value } public override fun hasNext(): Boolean { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception return currentMessage is ValueMessage } private val dummy = Any() private inline fun evaluateNext() { if (currentMessage == null) { continuationSync.put(dummy) currentMessage = resultQueue.take() } } }

, , :
, . , - , put take , ( ).

resultQueue thread , - continuationSync - , .

, continuationSync . :
1) next() hasNext() , , (. evaluateNext()), .
2) body, .. .
3) - yield(T), ( ) , . , .
4) body , , .

, :
private open class Message { } private class ValueMessage(public val value: Any): Message() { } private class CompletedMessage: Message() { } private class ExceptionThrownMessage(public val exception: Throwable): Message() { }

, Test() :
assertEquals(listOf(0, 1, 2, 3), Test().toList())


yieldfun .

context
, , . it :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) it.yield(i); }
.
.. Extension function literals - body , body . YieldContext :
public fun yieldfun<T: Any>(body: YieldContext<T>.() -> Unit): Iterable<T> = YieldIterable<T>(body)

body .
? :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) yield(i); }
.. yieldfun YieldContext, YieldContext this :
this.yield(i)

, this ,
yield(i)
.. body , :
continuationSync.take() body() resultQueue.put(CompletedMessage())
, YieldIterator YieldContext, body - extension-.


. - yield :)

YieldContext ret :
public trait YieldContext<T> { fun yield(value: T): Unit val ret: YieldContext<T> }

YieldIterator this :
override val ret: YieldContext<T> = this
? :
ret.yield(i)
, yield(T) this , :
ret yield i
, .

, C# Kotlin:
fun fibonacci(): Iterable<Int> = yieldfun { var a = 0 var b = 1 while (true) { val sum = a + b; ret yield sum; a = b; b = sum; } }
, , generic- yieldfun - .


, , . . finalize() :
protected fun finalize() { thread?.interrupt() }
catch :
try { continuationSync.take() body() resultQueue.put(CompletedMessage()) } catch (e: InterruptedException) { // if not all items were iterated yield will wait for signal, so finalizer should // interrupt the thread to exit } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) }
, continuationSync , .
, , , yieldfun , InterruptedException , yield try-catch , .


, extension- .Net, .. , "" . , , Jetbrains. , Kotlin .

UPD. bitbucket.org/Nerzhul500/kyield/src
 Iterable.   yieldfun .       : 
fun Test() = yieldfun<Int> { (context) -> for (val i in 0 .. 3) context.yield(i); }

context - , yield.

yieldfun , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> { return YieldIterable<T>(body) }
YieldIterable body - -. , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> = YieldIterable<T>(body)


Iterable , Iterator, YieldIterable :
private class YieldIterable<T>(val body: (YieldContext<T>) -> Unit): Iterable<T>{ public override fun iterator(): Iterator<T> = YieldIterator<T>(body) }


body ( trait) YieldContext, :
trait YieldContext<T> { fun yield(value : T) }

, yield(T) .

yield - YieldIterator:
private class YieldIterator<T> (val body: (YieldContext<T>) -> Unit): Iterator<T>, YieldContext<T> { private var thread: Thread? = null private val resultQueue = SynchronousQueue<Message>() private val continuationSync = SynchronousQueue<Any>() private var currentMessage: Message? = null { val r = object : Runnable { public override fun run() { try { continuationSync.take() body(this@YieldIterator) resultQueue.put(CompletedMessage()) } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) } } } thread = Thread(r) thread!!.start() } override fun yield(value: T) { resultQueue.put(ValueMessage(value)) continuationSync.take() } public override fun next(): T { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception if (currentMessage !is ValueMessage) throw NoSuchElementException() val value = (currentMessage as ValueMessage).value as T currentMessage = null return value } public override fun hasNext(): Boolean { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception return currentMessage is ValueMessage } private val dummy = Any() private inline fun evaluateNext() { if (currentMessage == null) { continuationSync.put(dummy) currentMessage = resultQueue.take() } } }

, , :
, . , - , put take , ( ).

resultQueue thread , - continuationSync - , .

, continuationSync . :
1) next() hasNext() , , (. evaluateNext()), .
2) body, .. .
3) - yield(T), ( ) , . , .
4) body , , .

, :
private open class Message { } private class ValueMessage(public val value: Any): Message() { } private class CompletedMessage: Message() { } private class ExceptionThrownMessage(public val exception: Throwable): Message() { }

, Test() :
assertEquals(listOf(0, 1, 2, 3), Test().toList())


yieldfun .

context
, , . it :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) it.yield(i); }
.
.. Extension function literals - body , body . YieldContext :
public fun yieldfun<T: Any>(body: YieldContext<T>.() -> Unit): Iterable<T> = YieldIterable<T>(body)

body .
? :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) yield(i); }
.. yieldfun YieldContext, YieldContext this :
this.yield(i)

, this ,
yield(i)
.. body , :
continuationSync.take() body() resultQueue.put(CompletedMessage())
, YieldIterator YieldContext, body - extension-.


. - yield :)

YieldContext ret :
public trait YieldContext<T> { fun yield(value: T): Unit val ret: YieldContext<T> }

YieldIterator this :
override val ret: YieldContext<T> = this
? :
ret.yield(i)
, yield(T) this , :
ret yield i
, .

, C# Kotlin:
fun fibonacci(): Iterable<Int> = yieldfun { var a = 0 var b = 1 while (true) { val sum = a + b; ret yield sum; a = b; b = sum; } }
, , generic- yieldfun - .


, , . . finalize() :
protected fun finalize() { thread?.interrupt() }
catch :
try { continuationSync.take() body() resultQueue.put(CompletedMessage()) } catch (e: InterruptedException) { // if not all items were iterated yield will wait for signal, so finalizer should // interrupt the thread to exit } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) }
, continuationSync , .
, , , yieldfun , InterruptedException , yield try-catch , .


, extension- .Net, .. , "" . , , Jetbrains. , Kotlin .

UPD. bitbucket.org/Nerzhul500/kyield/src
Iterable. yieldfun . :
fun Test() = yieldfun<Int> { (context) -> for (val i in 0 .. 3) context.yield(i); }

context - , yield.

yieldfun , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> { return YieldIterable<T>(body) }
YieldIterable body - -. , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> = YieldIterable<T>(body)


Iterable , Iterator, YieldIterable :
private class YieldIterable<T>(val body: (YieldContext<T>) -> Unit): Iterable<T>{ public override fun iterator(): Iterator<T> = YieldIterator<T>(body) }


body ( trait) YieldContext, :
trait YieldContext<T> { fun yield(value : T) }

, yield(T) .

yield - YieldIterator:
private class YieldIterator<T> (val body: (YieldContext<T>) -> Unit): Iterator<T>, YieldContext<T> { private var thread: Thread? = null private val resultQueue = SynchronousQueue<Message>() private val continuationSync = SynchronousQueue<Any>() private var currentMessage: Message? = null { val r = object : Runnable { public override fun run() { try { continuationSync.take() body(this@YieldIterator) resultQueue.put(CompletedMessage()) } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) } } } thread = Thread(r) thread!!.start() } override fun yield(value: T) { resultQueue.put(ValueMessage(value)) continuationSync.take() } public override fun next(): T { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception if (currentMessage !is ValueMessage) throw NoSuchElementException() val value = (currentMessage as ValueMessage).value as T currentMessage = null return value } public override fun hasNext(): Boolean { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception return currentMessage is ValueMessage } private val dummy = Any() private inline fun evaluateNext() { if (currentMessage == null) { continuationSync.put(dummy) currentMessage = resultQueue.take() } } }

, , :
, . , - , put take , ( ).

resultQueue thread , - continuationSync - , .

, continuationSync . :
1) next() hasNext() , , (. evaluateNext()), .
2) body, .. .
3) - yield(T), ( ) , . , .
4) body , , .

, :
private open class Message { } private class ValueMessage(public val value: Any): Message() { } private class CompletedMessage: Message() { } private class ExceptionThrownMessage(public val exception: Throwable): Message() { }

, Test() :
assertEquals(listOf(0, 1, 2, 3), Test().toList())


yieldfun .

context
, , . it :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) it.yield(i); }
.
.. Extension function literals - body , body . YieldContext :
public fun yieldfun<T: Any>(body: YieldContext<T>.() -> Unit): Iterable<T> = YieldIterable<T>(body)

body .
? :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) yield(i); }
.. yieldfun YieldContext, YieldContext this :
this.yield(i)

, this ,
yield(i)
.. body , :
continuationSync.take() body() resultQueue.put(CompletedMessage())
, YieldIterator YieldContext, body - extension-.


. - yield :)

YieldContext ret :
public trait YieldContext<T> { fun yield(value: T): Unit val ret: YieldContext<T> }

YieldIterator this :
override val ret: YieldContext<T> = this
? :
ret.yield(i)
, yield(T) this , :
ret yield i
, .

, C# Kotlin:
fun fibonacci(): Iterable<Int> = yieldfun { var a = 0 var b = 1 while (true) { val sum = a + b; ret yield sum; a = b; b = sum; } }
, , generic- yieldfun - .


, , . . finalize() :
protected fun finalize() { thread?.interrupt() }
catch :
try { continuationSync.take() body() resultQueue.put(CompletedMessage()) } catch (e: InterruptedException) { // if not all items were iterated yield will wait for signal, so finalizer should // interrupt the thread to exit } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) }
, continuationSync , .
, , , yieldfun , InterruptedException , yield try-catch , .


, extension- .Net, .. , "" . , , Jetbrains. , Kotlin .

UPD. bitbucket.org/Nerzhul500/kyield/src
 Iterable.   yieldfun .       : 
fun Test() = yieldfun<Int> { (context) -> for (val i in 0 .. 3) context.yield(i); }

context - , yield.

yieldfun , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> { return YieldIterable<T>(body) }
YieldIterable body - -. , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> = YieldIterable<T>(body)


Iterable , Iterator, YieldIterable :
private class YieldIterable<T>(val body: (YieldContext<T>) -> Unit): Iterable<T>{ public override fun iterator(): Iterator<T> = YieldIterator<T>(body) }


body ( trait) YieldContext, :
trait YieldContext<T> { fun yield(value : T) }

, yield(T) .

yield - YieldIterator:
private class YieldIterator<T> (val body: (YieldContext<T>) -> Unit): Iterator<T>, YieldContext<T> { private var thread: Thread? = null private val resultQueue = SynchronousQueue<Message>() private val continuationSync = SynchronousQueue<Any>() private var currentMessage: Message? = null { val r = object : Runnable { public override fun run() { try { continuationSync.take() body(this@YieldIterator) resultQueue.put(CompletedMessage()) } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) } } } thread = Thread(r) thread!!.start() } override fun yield(value: T) { resultQueue.put(ValueMessage(value)) continuationSync.take() } public override fun next(): T { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception if (currentMessage !is ValueMessage) throw NoSuchElementException() val value = (currentMessage as ValueMessage).value as T currentMessage = null return value } public override fun hasNext(): Boolean { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception return currentMessage is ValueMessage } private val dummy = Any() private inline fun evaluateNext() { if (currentMessage == null) { continuationSync.put(dummy) currentMessage = resultQueue.take() } } }

, , :
, . , - , put take , ( ).

resultQueue thread , - continuationSync - , .

, continuationSync . :
1) next() hasNext() , , (. evaluateNext()), .
2) body, .. .
3) - yield(T), ( ) , . , .
4) body , , .

, :
private open class Message { } private class ValueMessage(public val value: Any): Message() { } private class CompletedMessage: Message() { } private class ExceptionThrownMessage(public val exception: Throwable): Message() { }

, Test() :
assertEquals(listOf(0, 1, 2, 3), Test().toList())


yieldfun .

context
, , . it :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) it.yield(i); }
.
.. Extension function literals - body , body . YieldContext :
public fun yieldfun<T: Any>(body: YieldContext<T>.() -> Unit): Iterable<T> = YieldIterable<T>(body)

body .
? :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) yield(i); }
.. yieldfun YieldContext, YieldContext this :
this.yield(i)

, this ,
yield(i)
.. body , :
continuationSync.take() body() resultQueue.put(CompletedMessage())
, YieldIterator YieldContext, body - extension-.


. - yield :)

YieldContext ret :
public trait YieldContext<T> { fun yield(value: T): Unit val ret: YieldContext<T> }

YieldIterator this :
override val ret: YieldContext<T> = this
? :
ret.yield(i)
, yield(T) this , :
ret yield i
, .

, C# Kotlin:
fun fibonacci(): Iterable<Int> = yieldfun { var a = 0 var b = 1 while (true) { val sum = a + b; ret yield sum; a = b; b = sum; } }
, , generic- yieldfun - .


, , . . finalize() :
protected fun finalize() { thread?.interrupt() }
catch :
try { continuationSync.take() body() resultQueue.put(CompletedMessage()) } catch (e: InterruptedException) { // if not all items were iterated yield will wait for signal, so finalizer should // interrupt the thread to exit } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) }
, continuationSync , .
, , , yieldfun , InterruptedException , yield try-catch , .


, extension- .Net, .. , "" . , , Jetbrains. , Kotlin .

UPD. bitbucket.org/Nerzhul500/kyield/src
Iterable. yieldfun . :
fun Test() = yieldfun<Int> { (context) -> for (val i in 0 .. 3) context.yield(i); }

context - , yield.

yieldfun , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> { return YieldIterable<T>(body) }
YieldIterable body - -. , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> = YieldIterable<T>(body)


Iterable , Iterator, YieldIterable :
private class YieldIterable<T>(val body: (YieldContext<T>) -> Unit): Iterable<T>{ public override fun iterator(): Iterator<T> = YieldIterator<T>(body) }


body ( trait) YieldContext, :
trait YieldContext<T> { fun yield(value : T) }

, yield(T) .

yield - YieldIterator:
private class YieldIterator<T> (val body: (YieldContext<T>) -> Unit): Iterator<T>, YieldContext<T> { private var thread: Thread? = null private val resultQueue = SynchronousQueue<Message>() private val continuationSync = SynchronousQueue<Any>() private var currentMessage: Message? = null { val r = object : Runnable { public override fun run() { try { continuationSync.take() body(this@YieldIterator) resultQueue.put(CompletedMessage()) } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) } } } thread = Thread(r) thread!!.start() } override fun yield(value: T) { resultQueue.put(ValueMessage(value)) continuationSync.take() } public override fun next(): T { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception if (currentMessage !is ValueMessage) throw NoSuchElementException() val value = (currentMessage as ValueMessage).value as T currentMessage = null return value } public override fun hasNext(): Boolean { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception return currentMessage is ValueMessage } private val dummy = Any() private inline fun evaluateNext() { if (currentMessage == null) { continuationSync.put(dummy) currentMessage = resultQueue.take() } } }

, , :
, . , - , put take , ( ).

resultQueue thread , - continuationSync - , .

, continuationSync . :
1) next() hasNext() , , (. evaluateNext()), .
2) body, .. .
3) - yield(T), ( ) , . , .
4) body , , .

, :
private open class Message { } private class ValueMessage(public val value: Any): Message() { } private class CompletedMessage: Message() { } private class ExceptionThrownMessage(public val exception: Throwable): Message() { }

, Test() :
assertEquals(listOf(0, 1, 2, 3), Test().toList())


yieldfun .

context
, , . it :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) it.yield(i); }
.
.. Extension function literals - body , body . YieldContext :
public fun yieldfun<T: Any>(body: YieldContext<T>.() -> Unit): Iterable<T> = YieldIterable<T>(body)

body .
? :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) yield(i); }
.. yieldfun YieldContext, YieldContext this :
this.yield(i)

, this ,
yield(i)
.. body , :
continuationSync.take() body() resultQueue.put(CompletedMessage())
, YieldIterator YieldContext, body - extension-.


. - yield :)

YieldContext ret :
public trait YieldContext<T> { fun yield(value: T): Unit val ret: YieldContext<T> }

YieldIterator this :
override val ret: YieldContext<T> = this
? :
ret.yield(i)
, yield(T) this , :
ret yield i
, .

, C# Kotlin:
fun fibonacci(): Iterable<Int> = yieldfun { var a = 0 var b = 1 while (true) { val sum = a + b; ret yield sum; a = b; b = sum; } }
, , generic- yieldfun - .


, , . . finalize() :
protected fun finalize() { thread?.interrupt() }
catch :
try { continuationSync.take() body() resultQueue.put(CompletedMessage()) } catch (e: InterruptedException) { // if not all items were iterated yield will wait for signal, so finalizer should // interrupt the thread to exit } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) }
, continuationSync , .
, , , yieldfun , InterruptedException , yield try-catch , .


, extension- .Net, .. , "" . , , Jetbrains. , Kotlin .

UPD. bitbucket.org/Nerzhul500/kyield/src

Iterable. yieldfun . :
fun Test() = yieldfun<Int> { (context) -> for (val i in 0 .. 3) context.yield(i); }

context - , yield.

yieldfun , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> { return YieldIterable<T>(body) }
YieldIterable body - -. , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> = YieldIterable<T>(body)


Iterable , Iterator, YieldIterable :
private class YieldIterable<T>(val body: (YieldContext<T>) -> Unit): Iterable<T>{ public override fun iterator(): Iterator<T> = YieldIterator<T>(body) }


body ( trait) YieldContext, :
trait YieldContext<T> { fun yield(value : T) }

, yield(T) .

yield - YieldIterator:
private class YieldIterator<T> (val body: (YieldContext<T>) -> Unit): Iterator<T>, YieldContext<T> { private var thread: Thread? = null private val resultQueue = SynchronousQueue<Message>() private val continuationSync = SynchronousQueue<Any>() private var currentMessage: Message? = null { val r = object : Runnable { public override fun run() { try { continuationSync.take() body(this@YieldIterator) resultQueue.put(CompletedMessage()) } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) } } } thread = Thread(r) thread!!.start() } override fun yield(value: T) { resultQueue.put(ValueMessage(value)) continuationSync.take() } public override fun next(): T { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception if (currentMessage !is ValueMessage) throw NoSuchElementException() val value = (currentMessage as ValueMessage).value as T currentMessage = null return value } public override fun hasNext(): Boolean { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception return currentMessage is ValueMessage } private val dummy = Any() private inline fun evaluateNext() { if (currentMessage == null) { continuationSync.put(dummy) currentMessage = resultQueue.take() } } }

, , :
, . , - , put take , ( ).

resultQueue thread , - continuationSync - , .

, continuationSync . :
1) next() hasNext() , , (. evaluateNext()), .
2) body, .. .
3) - yield(T), ( ) , . , .
4) body , , .

, :
private open class Message { } private class ValueMessage(public val value: Any): Message() { } private class CompletedMessage: Message() { } private class ExceptionThrownMessage(public val exception: Throwable): Message() { }

, Test() :
assertEquals(listOf(0, 1, 2, 3), Test().toList())


yieldfun .

context
, , . it :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) it.yield(i); }
.
.. Extension function literals - body , body . YieldContext :
public fun yieldfun<T: Any>(body: YieldContext<T>.() -> Unit): Iterable<T> = YieldIterable<T>(body)

body .
? :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) yield(i); }
.. yieldfun YieldContext, YieldContext this :
this.yield(i)

, this ,
yield(i)
.. body , :
continuationSync.take() body() resultQueue.put(CompletedMessage())
, YieldIterator YieldContext, body - extension-.


. - yield :)

YieldContext ret :
public trait YieldContext<T> { fun yield(value: T): Unit val ret: YieldContext<T> }

YieldIterator this :
override val ret: YieldContext<T> = this
? :
ret.yield(i)
, yield(T) this , :
ret yield i
, .

, C# Kotlin:
fun fibonacci(): Iterable<Int> = yieldfun { var a = 0 var b = 1 while (true) { val sum = a + b; ret yield sum; a = b; b = sum; } }
, , generic- yieldfun - .


, , . . finalize() :
protected fun finalize() { thread?.interrupt() }
catch :
try { continuationSync.take() body() resultQueue.put(CompletedMessage()) } catch (e: InterruptedException) { // if not all items were iterated yield will wait for signal, so finalizer should // interrupt the thread to exit } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) }
, continuationSync , .
, , , yieldfun , InterruptedException , yield try-catch , .


, extension- .Net, .. , "" . , , Jetbrains. , Kotlin .

UPD. bitbucket.org/Nerzhul500/kyield/src

Iterable. yieldfun . :
fun Test() = yieldfun<Int> { (context) -> for (val i in 0 .. 3) context.yield(i); }

context - , yield.

yieldfun , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> { return YieldIterable<T>(body) }
YieldIterable body - -. , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> = YieldIterable<T>(body)


Iterable , Iterator, YieldIterable :
private class YieldIterable<T>(val body: (YieldContext<T>) -> Unit): Iterable<T>{ public override fun iterator(): Iterator<T> = YieldIterator<T>(body) }


body ( trait) YieldContext, :
trait YieldContext<T> { fun yield(value : T) }

, yield(T) .

yield - YieldIterator:
private class YieldIterator<T> (val body: (YieldContext<T>) -> Unit): Iterator<T>, YieldContext<T> { private var thread: Thread? = null private val resultQueue = SynchronousQueue<Message>() private val continuationSync = SynchronousQueue<Any>() private var currentMessage: Message? = null { val r = object : Runnable { public override fun run() { try { continuationSync.take() body(this@YieldIterator) resultQueue.put(CompletedMessage()) } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) } } } thread = Thread(r) thread!!.start() } override fun yield(value: T) { resultQueue.put(ValueMessage(value)) continuationSync.take() } public override fun next(): T { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception if (currentMessage !is ValueMessage) throw NoSuchElementException() val value = (currentMessage as ValueMessage).value as T currentMessage = null return value } public override fun hasNext(): Boolean { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception return currentMessage is ValueMessage } private val dummy = Any() private inline fun evaluateNext() { if (currentMessage == null) { continuationSync.put(dummy) currentMessage = resultQueue.take() } } }

, , :
, . , - , put take , ( ).

resultQueue thread , - continuationSync - , .

, continuationSync . :
1) next() hasNext() , , (. evaluateNext()), .
2) body, .. .
3) - yield(T), ( ) , . , .
4) body , , .

, :
private open class Message { } private class ValueMessage(public val value: Any): Message() { } private class CompletedMessage: Message() { } private class ExceptionThrownMessage(public val exception: Throwable): Message() { }

, Test() :
assertEquals(listOf(0, 1, 2, 3), Test().toList())


yieldfun .

context
, , . it :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) it.yield(i); }
.
.. Extension function literals - body , body . YieldContext :
public fun yieldfun<T: Any>(body: YieldContext<T>.() -> Unit): Iterable<T> = YieldIterable<T>(body)

body .
? :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) yield(i); }
.. yieldfun YieldContext, YieldContext this :
this.yield(i)

, this ,
yield(i)
.. body , :
continuationSync.take() body() resultQueue.put(CompletedMessage())
, YieldIterator YieldContext, body - extension-.


. - yield :)

YieldContext ret :
public trait YieldContext<T> { fun yield(value: T): Unit val ret: YieldContext<T> }

YieldIterator this :
override val ret: YieldContext<T> = this
? :
ret.yield(i)
, yield(T) this , :
ret yield i
, .

, C# Kotlin:
fun fibonacci(): Iterable<Int> = yieldfun { var a = 0 var b = 1 while (true) { val sum = a + b; ret yield sum; a = b; b = sum; } }
, , generic- yieldfun - .


, , . . finalize() :
protected fun finalize() { thread?.interrupt() }
catch :
try { continuationSync.take() body() resultQueue.put(CompletedMessage()) } catch (e: InterruptedException) { // if not all items were iterated yield will wait for signal, so finalizer should // interrupt the thread to exit } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) }
, continuationSync , .
, , , yieldfun , InterruptedException , yield try-catch , .


, extension- .Net, .. , "" . , , Jetbrains. , Kotlin .

UPD. bitbucket.org/Nerzhul500/kyield/src
Iterable. yieldfun . :
fun Test() = yieldfun<Int> { (context) -> for (val i in 0 .. 3) context.yield(i); }

context - , yield.

yieldfun , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> { return YieldIterable<T>(body) }
YieldIterable body - -. , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> = YieldIterable<T>(body)


Iterable , Iterator, YieldIterable :
private class YieldIterable<T>(val body: (YieldContext<T>) -> Unit): Iterable<T>{ public override fun iterator(): Iterator<T> = YieldIterator<T>(body) }


body ( trait) YieldContext, :
trait YieldContext<T> { fun yield(value : T) }

, yield(T) .

yield - YieldIterator:
private class YieldIterator<T> (val body: (YieldContext<T>) -> Unit): Iterator<T>, YieldContext<T> { private var thread: Thread? = null private val resultQueue = SynchronousQueue<Message>() private val continuationSync = SynchronousQueue<Any>() private var currentMessage: Message? = null { val r = object : Runnable { public override fun run() { try { continuationSync.take() body(this@YieldIterator) resultQueue.put(CompletedMessage()) } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) } } } thread = Thread(r) thread!!.start() } override fun yield(value: T) { resultQueue.put(ValueMessage(value)) continuationSync.take() } public override fun next(): T { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception if (currentMessage !is ValueMessage) throw NoSuchElementException() val value = (currentMessage as ValueMessage).value as T currentMessage = null return value } public override fun hasNext(): Boolean { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception return currentMessage is ValueMessage } private val dummy = Any() private inline fun evaluateNext() { if (currentMessage == null) { continuationSync.put(dummy) currentMessage = resultQueue.take() } } }

, , :
, . , - , put take , ( ).

resultQueue thread , - continuationSync - , .

, continuationSync . :
1) next() hasNext() , , (. evaluateNext()), .
2) body, .. .
3) - yield(T), ( ) , . , .
4) body , , .

, :
private open class Message { } private class ValueMessage(public val value: Any): Message() { } private class CompletedMessage: Message() { } private class ExceptionThrownMessage(public val exception: Throwable): Message() { }

, Test() :
assertEquals(listOf(0, 1, 2, 3), Test().toList())


yieldfun .

context
, , . it :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) it.yield(i); }
.
.. Extension function literals - body , body . YieldContext :
public fun yieldfun<T: Any>(body: YieldContext<T>.() -> Unit): Iterable<T> = YieldIterable<T>(body)

body .
? :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) yield(i); }
.. yieldfun YieldContext, YieldContext this :
this.yield(i)

, this ,
yield(i)
.. body , :
continuationSync.take() body() resultQueue.put(CompletedMessage())
, YieldIterator YieldContext, body - extension-.


. - yield :)

YieldContext ret :
public trait YieldContext<T> { fun yield(value: T): Unit val ret: YieldContext<T> }

YieldIterator this :
override val ret: YieldContext<T> = this
? :
ret.yield(i)
, yield(T) this , :
ret yield i
, .

, C# Kotlin:
fun fibonacci(): Iterable<Int> = yieldfun { var a = 0 var b = 1 while (true) { val sum = a + b; ret yield sum; a = b; b = sum; } }
, , generic- yieldfun - .


, , . . finalize() :
protected fun finalize() { thread?.interrupt() }
catch :
try { continuationSync.take() body() resultQueue.put(CompletedMessage()) } catch (e: InterruptedException) { // if not all items were iterated yield will wait for signal, so finalizer should // interrupt the thread to exit } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) }
, continuationSync , .
, , , yieldfun , InterruptedException , yield try-catch , .


, extension- .Net, .. , "" . , , Jetbrains. , Kotlin .

UPD. bitbucket.org/Nerzhul500/kyield/src
Iterable. yieldfun . :
fun Test() = yieldfun<Int> { (context) -> for (val i in 0 .. 3) context.yield(i); }

context - , yield.

yieldfun , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> { return YieldIterable<T>(body) }
YieldIterable body - -. , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> = YieldIterable<T>(body)


Iterable , Iterator, YieldIterable :
private class YieldIterable<T>(val body: (YieldContext<T>) -> Unit): Iterable<T>{ public override fun iterator(): Iterator<T> = YieldIterator<T>(body) }


body ( trait) YieldContext, :
trait YieldContext<T> { fun yield(value : T) }

, yield(T) .

yield - YieldIterator:
private class YieldIterator<T> (val body: (YieldContext<T>) -> Unit): Iterator<T>, YieldContext<T> { private var thread: Thread? = null private val resultQueue = SynchronousQueue<Message>() private val continuationSync = SynchronousQueue<Any>() private var currentMessage: Message? = null { val r = object : Runnable { public override fun run() { try { continuationSync.take() body(this@YieldIterator) resultQueue.put(CompletedMessage()) } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) } } } thread = Thread(r) thread!!.start() } override fun yield(value: T) { resultQueue.put(ValueMessage(value)) continuationSync.take() } public override fun next(): T { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception if (currentMessage !is ValueMessage) throw NoSuchElementException() val value = (currentMessage as ValueMessage).value as T currentMessage = null return value } public override fun hasNext(): Boolean { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception return currentMessage is ValueMessage } private val dummy = Any() private inline fun evaluateNext() { if (currentMessage == null) { continuationSync.put(dummy) currentMessage = resultQueue.take() } } }

, , :
, . , - , put take , ( ).

resultQueue thread , - continuationSync - , .

, continuationSync . :
1) next() hasNext() , , (. evaluateNext()), .
2) body, .. .
3) - yield(T), ( ) , . , .
4) body , , .

, :
private open class Message { } private class ValueMessage(public val value: Any): Message() { } private class CompletedMessage: Message() { } private class ExceptionThrownMessage(public val exception: Throwable): Message() { }

, Test() :
assertEquals(listOf(0, 1, 2, 3), Test().toList())


yieldfun .

context
, , . it :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) it.yield(i); }
.
.. Extension function literals - body , body . YieldContext :
public fun yieldfun<T: Any>(body: YieldContext<T>.() -> Unit): Iterable<T> = YieldIterable<T>(body)

body .
? :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) yield(i); }
.. yieldfun YieldContext, YieldContext this :
this.yield(i)

, this ,
yield(i)
.. body , :
continuationSync.take() body() resultQueue.put(CompletedMessage())
, YieldIterator YieldContext, body - extension-.


. - yield :)

YieldContext ret :
public trait YieldContext<T> { fun yield(value: T): Unit val ret: YieldContext<T> }

YieldIterator this :
override val ret: YieldContext<T> = this
? :
ret.yield(i)
, yield(T) this , :
ret yield i
, .

, C# Kotlin:
fun fibonacci(): Iterable<Int> = yieldfun { var a = 0 var b = 1 while (true) { val sum = a + b; ret yield sum; a = b; b = sum; } }
, , generic- yieldfun - .


, , . . finalize() :
protected fun finalize() { thread?.interrupt() }
catch :
try { continuationSync.take() body() resultQueue.put(CompletedMessage()) } catch (e: InterruptedException) { // if not all items were iterated yield will wait for signal, so finalizer should // interrupt the thread to exit } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) }
, continuationSync , .
, , , yieldfun , InterruptedException , yield try-catch , .


, extension- .Net, .. , "" . , , Jetbrains. , Kotlin .

UPD. bitbucket.org/Nerzhul500/kyield/src
 Iterable.   yieldfun .       : 
fun Test() = yieldfun<Int> { (context) -> for (val i in 0 .. 3) context.yield(i); }

context - , yield.

yieldfun , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> { return YieldIterable<T>(body) }
YieldIterable body - -. , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> = YieldIterable<T>(body)


Iterable , Iterator, YieldIterable :
private class YieldIterable<T>(val body: (YieldContext<T>) -> Unit): Iterable<T>{ public override fun iterator(): Iterator<T> = YieldIterator<T>(body) }


body ( trait) YieldContext, :
trait YieldContext<T> { fun yield(value : T) }

, yield(T) .

yield - YieldIterator:
private class YieldIterator<T> (val body: (YieldContext<T>) -> Unit): Iterator<T>, YieldContext<T> { private var thread: Thread? = null private val resultQueue = SynchronousQueue<Message>() private val continuationSync = SynchronousQueue<Any>() private var currentMessage: Message? = null { val r = object : Runnable { public override fun run() { try { continuationSync.take() body(this@YieldIterator) resultQueue.put(CompletedMessage()) } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) } } } thread = Thread(r) thread!!.start() } override fun yield(value: T) { resultQueue.put(ValueMessage(value)) continuationSync.take() } public override fun next(): T { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception if (currentMessage !is ValueMessage) throw NoSuchElementException() val value = (currentMessage as ValueMessage).value as T currentMessage = null return value } public override fun hasNext(): Boolean { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception return currentMessage is ValueMessage } private val dummy = Any() private inline fun evaluateNext() { if (currentMessage == null) { continuationSync.put(dummy) currentMessage = resultQueue.take() } } }

, , :
, . , - , put take , ( ).

resultQueue thread , - continuationSync - , .

, continuationSync . :
1) next() hasNext() , , (. evaluateNext()), .
2) body, .. .
3) - yield(T), ( ) , . , .
4) body , , .

, :
private open class Message { } private class ValueMessage(public val value: Any): Message() { } private class CompletedMessage: Message() { } private class ExceptionThrownMessage(public val exception: Throwable): Message() { }

, Test() :
assertEquals(listOf(0, 1, 2, 3), Test().toList())


yieldfun .

context
, , . it :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) it.yield(i); }
.
.. Extension function literals - body , body . YieldContext :
public fun yieldfun<T: Any>(body: YieldContext<T>.() -> Unit): Iterable<T> = YieldIterable<T>(body)

body .
? :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) yield(i); }
.. yieldfun YieldContext, YieldContext this :
this.yield(i)

, this ,
yield(i)
.. body , :
continuationSync.take() body() resultQueue.put(CompletedMessage())
, YieldIterator YieldContext, body - extension-.


. - yield :)

YieldContext ret :
public trait YieldContext<T> { fun yield(value: T): Unit val ret: YieldContext<T> }

YieldIterator this :
override val ret: YieldContext<T> = this
? :
ret.yield(i)
, yield(T) this , :
ret yield i
, .

, C# Kotlin:
fun fibonacci(): Iterable<Int> = yieldfun { var a = 0 var b = 1 while (true) { val sum = a + b; ret yield sum; a = b; b = sum; } }
, , generic- yieldfun - .


, , . . finalize() :
protected fun finalize() { thread?.interrupt() }
catch :
try { continuationSync.take() body() resultQueue.put(CompletedMessage()) } catch (e: InterruptedException) { // if not all items were iterated yield will wait for signal, so finalizer should // interrupt the thread to exit } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) }
, continuationSync , .
, , , yieldfun , InterruptedException , yield try-catch , .


, extension- .Net, .. , "" . , , Jetbrains. , Kotlin .

UPD. bitbucket.org/Nerzhul500/kyield/src
Iterable. yieldfun . :
fun Test() = yieldfun<Int> { (context) -> for (val i in 0 .. 3) context.yield(i); }

context - , yield.

yieldfun , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> { return YieldIterable<T>(body) }
YieldIterable body - -. , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> = YieldIterable<T>(body)


Iterable , Iterator, YieldIterable :
private class YieldIterable<T>(val body: (YieldContext<T>) -> Unit): Iterable<T>{ public override fun iterator(): Iterator<T> = YieldIterator<T>(body) }


body ( trait) YieldContext, :
trait YieldContext<T> { fun yield(value : T) }

, yield(T) .

yield - YieldIterator:
private class YieldIterator<T> (val body: (YieldContext<T>) -> Unit): Iterator<T>, YieldContext<T> { private var thread: Thread? = null private val resultQueue = SynchronousQueue<Message>() private val continuationSync = SynchronousQueue<Any>() private var currentMessage: Message? = null { val r = object : Runnable { public override fun run() { try { continuationSync.take() body(this@YieldIterator) resultQueue.put(CompletedMessage()) } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) } } } thread = Thread(r) thread!!.start() } override fun yield(value: T) { resultQueue.put(ValueMessage(value)) continuationSync.take() } public override fun next(): T { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception if (currentMessage !is ValueMessage) throw NoSuchElementException() val value = (currentMessage as ValueMessage).value as T currentMessage = null return value } public override fun hasNext(): Boolean { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception return currentMessage is ValueMessage } private val dummy = Any() private inline fun evaluateNext() { if (currentMessage == null) { continuationSync.put(dummy) currentMessage = resultQueue.take() } } }

, , :
, . , - , put take , ( ).

resultQueue thread , - continuationSync - , .

, continuationSync . :
1) next() hasNext() , , (. evaluateNext()), .
2) body, .. .
3) - yield(T), ( ) , . , .
4) body , , .

, :
private open class Message { } private class ValueMessage(public val value: Any): Message() { } private class CompletedMessage: Message() { } private class ExceptionThrownMessage(public val exception: Throwable): Message() { }

, Test() :
assertEquals(listOf(0, 1, 2, 3), Test().toList())


yieldfun .

context
, , . it :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) it.yield(i); }
.
.. Extension function literals - body , body . YieldContext :
public fun yieldfun<T: Any>(body: YieldContext<T>.() -> Unit): Iterable<T> = YieldIterable<T>(body)

body .
? :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) yield(i); }
.. yieldfun YieldContext, YieldContext this :
this.yield(i)

, this ,
yield(i)
.. body , :
continuationSync.take() body() resultQueue.put(CompletedMessage())
, YieldIterator YieldContext, body - extension-.


. - yield :)

YieldContext ret :
public trait YieldContext<T> { fun yield(value: T): Unit val ret: YieldContext<T> }

YieldIterator this :
override val ret: YieldContext<T> = this
? :
ret.yield(i)
, yield(T) this , :
ret yield i
, .

, C# Kotlin:
fun fibonacci(): Iterable<Int> = yieldfun { var a = 0 var b = 1 while (true) { val sum = a + b; ret yield sum; a = b; b = sum; } }
, , generic- yieldfun - .


, , . . finalize() :
protected fun finalize() { thread?.interrupt() }
catch :
try { continuationSync.take() body() resultQueue.put(CompletedMessage()) } catch (e: InterruptedException) { // if not all items were iterated yield will wait for signal, so finalizer should // interrupt the thread to exit } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) }
, continuationSync , .
, , , yieldfun , InterruptedException , yield try-catch , .


, extension- .Net, .. , "" . , , Jetbrains. , Kotlin .

UPD. bitbucket.org/Nerzhul500/kyield/src
 Iterable.   yieldfun .       : 
fun Test() = yieldfun<Int> { (context) -> for (val i in 0 .. 3) context.yield(i); }

context - , yield.

yieldfun , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> { return YieldIterable<T>(body) }
YieldIterable body - -. , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> = YieldIterable<T>(body)


Iterable , Iterator, YieldIterable :
private class YieldIterable<T>(val body: (YieldContext<T>) -> Unit): Iterable<T>{ public override fun iterator(): Iterator<T> = YieldIterator<T>(body) }


body ( trait) YieldContext, :
trait YieldContext<T> { fun yield(value : T) }

, yield(T) .

yield - YieldIterator:
private class YieldIterator<T> (val body: (YieldContext<T>) -> Unit): Iterator<T>, YieldContext<T> { private var thread: Thread? = null private val resultQueue = SynchronousQueue<Message>() private val continuationSync = SynchronousQueue<Any>() private var currentMessage: Message? = null { val r = object : Runnable { public override fun run() { try { continuationSync.take() body(this@YieldIterator) resultQueue.put(CompletedMessage()) } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) } } } thread = Thread(r) thread!!.start() } override fun yield(value: T) { resultQueue.put(ValueMessage(value)) continuationSync.take() } public override fun next(): T { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception if (currentMessage !is ValueMessage) throw NoSuchElementException() val value = (currentMessage as ValueMessage).value as T currentMessage = null return value } public override fun hasNext(): Boolean { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception return currentMessage is ValueMessage } private val dummy = Any() private inline fun evaluateNext() { if (currentMessage == null) { continuationSync.put(dummy) currentMessage = resultQueue.take() } } }

, , :
, . , - , put take , ( ).

resultQueue thread , - continuationSync - , .

, continuationSync . :
1) next() hasNext() , , (. evaluateNext()), .
2) body, .. .
3) - yield(T), ( ) , . , .
4) body , , .

, :
private open class Message { } private class ValueMessage(public val value: Any): Message() { } private class CompletedMessage: Message() { } private class ExceptionThrownMessage(public val exception: Throwable): Message() { }

, Test() :
assertEquals(listOf(0, 1, 2, 3), Test().toList())


yieldfun .

context
, , . it :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) it.yield(i); }
.
.. Extension function literals - body , body . YieldContext :
public fun yieldfun<T: Any>(body: YieldContext<T>.() -> Unit): Iterable<T> = YieldIterable<T>(body)
body .
? :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) yield(i); }
.. yieldfun YieldContext, YieldContext this :
this.yield(i)

, this ,
yield(i)
.. body , :
continuationSync.take() body() resultQueue.put(CompletedMessage())
, YieldIterator YieldContext, body - extension-.


. - yield :)

YieldContext ret :
public trait YieldContext<T> { fun yield(value: T): Unit val ret: YieldContext<T> }

YieldIterator this :
override val ret: YieldContext<T> = this
? :
ret.yield(i)
, yield(T) this , :
ret yield i
, .

, C# Kotlin:
fun fibonacci(): Iterable<Int> = yieldfun { var a = 0 var b = 1 while (true) { val sum = a + b; ret yield sum; a = b; b = sum; } }
, , generic- yieldfun - .


, , . . finalize() :
protected fun finalize() { thread?.interrupt() }
catch :
try { continuationSync.take() body() resultQueue.put(CompletedMessage()) } catch (e: InterruptedException) { // if not all items were iterated yield will wait for signal, so finalizer should // interrupt the thread to exit } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) }
, continuationSync , .
, , , yieldfun , InterruptedException , yield try-catch , .


, extension- .Net, .. , "" . , , Jetbrains. , Kotlin .

UPD. bitbucket.org/Nerzhul500/kyield/src
Iterable. yieldfun . :
fun Test() = yieldfun<Int> { (context) -> for (val i in 0 .. 3) context.yield(i); }

context - , yield.

yieldfun , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> { return YieldIterable<T>(body) }
YieldIterable body - -. , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> = YieldIterable<T>(body)


Iterable , Iterator, YieldIterable :
private class YieldIterable<T>(val body: (YieldContext<T>) -> Unit): Iterable<T>{ public override fun iterator(): Iterator<T> = YieldIterator<T>(body) }


body ( trait) YieldContext, :
trait YieldContext<T> { fun yield(value : T) }

, yield(T) .

yield - YieldIterator:
private class YieldIterator<T> (val body: (YieldContext<T>) -> Unit): Iterator<T>, YieldContext<T> { private var thread: Thread? = null private val resultQueue = SynchronousQueue<Message>() private val continuationSync = SynchronousQueue<Any>() private var currentMessage: Message? = null { val r = object : Runnable { public override fun run() { try { continuationSync.take() body(this@YieldIterator) resultQueue.put(CompletedMessage()) } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) } } } thread = Thread(r) thread!!.start() } override fun yield(value: T) { resultQueue.put(ValueMessage(value)) continuationSync.take() } public override fun next(): T { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception if (currentMessage !is ValueMessage) throw NoSuchElementException() val value = (currentMessage as ValueMessage).value as T currentMessage = null return value } public override fun hasNext(): Boolean { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception return currentMessage is ValueMessage } private val dummy = Any() private inline fun evaluateNext() { if (currentMessage == null) { continuationSync.put(dummy) currentMessage = resultQueue.take() } } }

, , :
, . , - , put take , ( ).

resultQueue thread , - continuationSync - , .

, continuationSync . :
1) next() hasNext() , , (. evaluateNext()), .
2) body, .. .
3) - yield(T), ( ) , . , .
4) body , , .

, :
private open class Message { } private class ValueMessage(public val value: Any): Message() { } private class CompletedMessage: Message() { } private class ExceptionThrownMessage(public val exception: Throwable): Message() { }

, Test() :
assertEquals(listOf(0, 1, 2, 3), Test().toList())


yieldfun .

context
, , . it :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) it.yield(i); }
.
.. Extension function literals - body , body . YieldContext :
public fun yieldfun<T: Any>(body: YieldContext<T>.() -> Unit): Iterable<T> = YieldIterable<T>(body)

body .
? :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) yield(i); }
.. yieldfun YieldContext, YieldContext this :
this.yield(i)

, this ,
yield(i)
.. body , :
continuationSync.take() body() resultQueue.put(CompletedMessage())
, YieldIterator YieldContext, body - extension-.


. - yield :)

YieldContext ret :
public trait YieldContext<T> { fun yield(value: T): Unit val ret: YieldContext<T> }

YieldIterator this :
override val ret: YieldContext<T> = this
? :
ret.yield(i)
, yield(T) this , :
ret yield i
, .

, C# Kotlin:
fun fibonacci(): Iterable<Int> = yieldfun { var a = 0 var b = 1 while (true) { val sum = a + b; ret yield sum; a = b; b = sum; } }
, , generic- yieldfun - .


, , . . finalize() :
protected fun finalize() { thread?.interrupt() }
catch :
try { continuationSync.take() body() resultQueue.put(CompletedMessage()) } catch (e: InterruptedException) { // if not all items were iterated yield will wait for signal, so finalizer should // interrupt the thread to exit } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) }
, continuationSync , .
, , , yieldfun , InterruptedException , yield try-catch , .


, extension- .Net, .. , "" . , , Jetbrains. , Kotlin .

UPD. bitbucket.org/Nerzhul500/kyield/src
 Iterable.   yieldfun .       : 
fun Test() = yieldfun<Int> { (context) -> for (val i in 0 .. 3) context.yield(i); }

context - , yield.

yieldfun , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> { return YieldIterable<T>(body) }
YieldIterable body - -. , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> = YieldIterable<T>(body)


Iterable , Iterator, YieldIterable :
private class YieldIterable<T>(val body: (YieldContext<T>) -> Unit): Iterable<T>{ public override fun iterator(): Iterator<T> = YieldIterator<T>(body) }


body ( trait) YieldContext, :
trait YieldContext<T> { fun yield(value : T) }

, yield(T) .

yield - YieldIterator:
private class YieldIterator<T> (val body: (YieldContext<T>) -> Unit): Iterator<T>, YieldContext<T> { private var thread: Thread? = null private val resultQueue = SynchronousQueue<Message>() private val continuationSync = SynchronousQueue<Any>() private var currentMessage: Message? = null { val r = object : Runnable { public override fun run() { try { continuationSync.take() body(this@YieldIterator) resultQueue.put(CompletedMessage()) } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) } } } thread = Thread(r) thread!!.start() } override fun yield(value: T) { resultQueue.put(ValueMessage(value)) continuationSync.take() } public override fun next(): T { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception if (currentMessage !is ValueMessage) throw NoSuchElementException() val value = (currentMessage as ValueMessage).value as T currentMessage = null return value } public override fun hasNext(): Boolean { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception return currentMessage is ValueMessage } private val dummy = Any() private inline fun evaluateNext() { if (currentMessage == null) { continuationSync.put(dummy) currentMessage = resultQueue.take() } } }

, , :
, . , - , put take , ( ).

resultQueue thread , - continuationSync - , .

, continuationSync . :
1) next() hasNext() , , (. evaluateNext()), .
2) body, .. .
3) - yield(T), ( ) , . , .
4) body , , .

, :
private open class Message { } private class ValueMessage(public val value: Any): Message() { } private class CompletedMessage: Message() { } private class ExceptionThrownMessage(public val exception: Throwable): Message() { }

, Test() :
assertEquals(listOf(0, 1, 2, 3), Test().toList())


yieldfun .

context
, , . it :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) it.yield(i); }
.
.. Extension function literals - body , body . YieldContext :
public fun yieldfun<T: Any>(body: YieldContext<T>.() -> Unit): Iterable<T> = YieldIterable<T>(body)

body .
? :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) yield(i); }
.. yieldfun YieldContext, YieldContext this :
this.yield(i)

, this ,
yield(i)
.. body , :
continuationSync.take() body() resultQueue.put(CompletedMessage())
, YieldIterator YieldContext, body - extension-.


. - yield :)

YieldContext ret :
public trait YieldContext<T> { fun yield(value: T): Unit val ret: YieldContext<T> }

YieldIterator this :
override val ret: YieldContext<T> = this
? :
ret.yield(i)
, yield(T) this , :
ret yield i
, .

, C# Kotlin:
fun fibonacci(): Iterable<Int> = yieldfun { var a = 0 var b = 1 while (true) { val sum = a + b; ret yield sum; a = b; b = sum; } }
, , generic- yieldfun - .


, , . . finalize() :
protected fun finalize() { thread?.interrupt() }
catch :
try { continuationSync.take() body() resultQueue.put(CompletedMessage()) } catch (e: InterruptedException) { // if not all items were iterated yield will wait for signal, so finalizer should // interrupt the thread to exit } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) }
, continuationSync , .
, , , yieldfun , InterruptedException , yield try-catch , .


, extension- .Net, .. , "" . , , Jetbrains. , Kotlin .

UPD. bitbucket.org/Nerzhul500/kyield/src
Iterable. yieldfun . :
fun Test() = yieldfun<Int> { (context) -> for (val i in 0 .. 3) context.yield(i); }

context - , yield.

yieldfun , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> { return YieldIterable<T>(body) }
YieldIterable body - -. , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> = YieldIterable<T>(body)


Iterable , Iterator, YieldIterable :
private class YieldIterable<T>(val body: (YieldContext<T>) -> Unit): Iterable<T>{ public override fun iterator(): Iterator<T> = YieldIterator<T>(body) }


body ( trait) YieldContext, :
trait YieldContext<T> { fun yield(value : T) }

, yield(T) .

yield - YieldIterator:
private class YieldIterator<T> (val body: (YieldContext<T>) -> Unit): Iterator<T>, YieldContext<T> { private var thread: Thread? = null private val resultQueue = SynchronousQueue<Message>() private val continuationSync = SynchronousQueue<Any>() private var currentMessage: Message? = null { val r = object : Runnable { public override fun run() { try { continuationSync.take() body(this@YieldIterator) resultQueue.put(CompletedMessage()) } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) } } } thread = Thread(r) thread!!.start() } override fun yield(value: T) { resultQueue.put(ValueMessage(value)) continuationSync.take() } public override fun next(): T { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception if (currentMessage !is ValueMessage) throw NoSuchElementException() val value = (currentMessage as ValueMessage).value as T currentMessage = null return value } public override fun hasNext(): Boolean { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception return currentMessage is ValueMessage } private val dummy = Any() private inline fun evaluateNext() { if (currentMessage == null) { continuationSync.put(dummy) currentMessage = resultQueue.take() } } }

, , :
, . , - , put take , ( ).

resultQueue thread , - continuationSync - , .

, continuationSync . :
1) next() hasNext() , , (. evaluateNext()), .
2) body, .. .
3) - yield(T), ( ) , . , .
4) body , , .

, :
private open class Message { } private class ValueMessage(public val value: Any): Message() { } private class CompletedMessage: Message() { } private class ExceptionThrownMessage(public val exception: Throwable): Message() { }

, Test() :
assertEquals(listOf(0, 1, 2, 3), Test().toList())


yieldfun .

context
, , . it :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) it.yield(i); }
.
.. Extension function literals - body , body . YieldContext :
public fun yieldfun<T: Any>(body: YieldContext<T>.() -> Unit): Iterable<T> = YieldIterable<T>(body)

body .
? :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) yield(i); }
.. yieldfun YieldContext, YieldContext this :
this.yield(i)

, this ,
yield(i)
.. body , :
continuationSync.take() body() resultQueue.put(CompletedMessage())
, YieldIterator YieldContext, body - extension-.


. - yield :)

YieldContext ret :
public trait YieldContext<T> { fun yield(value: T): Unit val ret: YieldContext<T> }

YieldIterator this :
override val ret: YieldContext<T> = this
? :
ret.yield(i)
, yield(T) this , :
ret yield i
, .

, C# Kotlin:
fun fibonacci(): Iterable<Int> = yieldfun { var a = 0 var b = 1 while (true) { val sum = a + b; ret yield sum; a = b; b = sum; } }
, , generic- yieldfun - .


, , . . finalize() :
protected fun finalize() { thread?.interrupt() }
catch :
try { continuationSync.take() body() resultQueue.put(CompletedMessage()) } catch (e: InterruptedException) { // if not all items were iterated yield will wait for signal, so finalizer should // interrupt the thread to exit } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) }
, continuationSync , .
, , , yieldfun , InterruptedException , yield try-catch , .


, extension- .Net, .. , "" . , , Jetbrains. , Kotlin .

UPD. bitbucket.org/Nerzhul500/kyield/src
 Iterable.   yieldfun .       : 
fun Test() = yieldfun<Int> { (context) -> for (val i in 0 .. 3) context.yield(i); }

context - , yield.

yieldfun , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> { return YieldIterable<T>(body) }
YieldIterable body - -. , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> = YieldIterable<T>(body)


Iterable , Iterator, YieldIterable :
private class YieldIterable<T>(val body: (YieldContext<T>) -> Unit): Iterable<T>{ public override fun iterator(): Iterator<T> = YieldIterator<T>(body) }


body ( trait) YieldContext, :
trait YieldContext<T> { fun yield(value : T) }

, yield(T) .

yield - YieldIterator:
private class YieldIterator<T> (val body: (YieldContext<T>) -> Unit): Iterator<T>, YieldContext<T> { private var thread: Thread? = null private val resultQueue = SynchronousQueue<Message>() private val continuationSync = SynchronousQueue<Any>() private var currentMessage: Message? = null { val r = object : Runnable { public override fun run() { try { continuationSync.take() body(this@YieldIterator) resultQueue.put(CompletedMessage()) } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) } } } thread = Thread(r) thread!!.start() } override fun yield(value: T) { resultQueue.put(ValueMessage(value)) continuationSync.take() } public override fun next(): T { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception if (currentMessage !is ValueMessage) throw NoSuchElementException() val value = (currentMessage as ValueMessage).value as T currentMessage = null return value } public override fun hasNext(): Boolean { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception return currentMessage is ValueMessage } private val dummy = Any() private inline fun evaluateNext() { if (currentMessage == null) { continuationSync.put(dummy) currentMessage = resultQueue.take() } } }

, , :
, . , - , put take , ( ).

resultQueue thread , - continuationSync - , .

, continuationSync . :
1) next() hasNext() , , (. evaluateNext()), .
2) body, .. .
3) - yield(T), ( ) , . , .
4) body , , .

, :
private open class Message { } private class ValueMessage(public val value: Any): Message() { } private class CompletedMessage: Message() { } private class ExceptionThrownMessage(public val exception: Throwable): Message() { }

, Test() :
assertEquals(listOf(0, 1, 2, 3), Test().toList())


yieldfun .

context
, , . it :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) it.yield(i); }
.
.. Extension function literals - body , body . YieldContext :
public fun yieldfun<T: Any>(body: YieldContext<T>.() -> Unit): Iterable<T> = YieldIterable<T>(body)

body .
? :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) yield(i); }
.. yieldfun YieldContext, YieldContext this :
this.yield(i)
, this ,
yield(i)
.. body , :
continuationSync.take() body() resultQueue.put(CompletedMessage())
, YieldIterator YieldContext, body - extension-.


. - yield :)

YieldContext ret :
public trait YieldContext<T> { fun yield(value: T): Unit val ret: YieldContext<T> }

YieldIterator this :
override val ret: YieldContext<T> = this
? :
ret.yield(i)
, yield(T) this , :
ret yield i
, .

, C# Kotlin:
fun fibonacci(): Iterable<Int> = yieldfun { var a = 0 var b = 1 while (true) { val sum = a + b; ret yield sum; a = b; b = sum; } }
, , generic- yieldfun - .


, , . . finalize() :
protected fun finalize() { thread?.interrupt() }
catch :
try { continuationSync.take() body() resultQueue.put(CompletedMessage()) } catch (e: InterruptedException) { // if not all items were iterated yield will wait for signal, so finalizer should // interrupt the thread to exit } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) }
, continuationSync , .
, , , yieldfun , InterruptedException , yield try-catch , .


, extension- .Net, .. , "" . , , Jetbrains. , Kotlin .

UPD. bitbucket.org/Nerzhul500/kyield/src
Iterable. yieldfun . :
fun Test() = yieldfun<Int> { (context) -> for (val i in 0 .. 3) context.yield(i); }

context - , yield.

yieldfun , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> { return YieldIterable<T>(body) }
YieldIterable body - -. , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> = YieldIterable<T>(body)


Iterable , Iterator, YieldIterable :
private class YieldIterable<T>(val body: (YieldContext<T>) -> Unit): Iterable<T>{ public override fun iterator(): Iterator<T> = YieldIterator<T>(body) }


body ( trait) YieldContext, :
trait YieldContext<T> { fun yield(value : T) }

, yield(T) .

yield - YieldIterator:
private class YieldIterator<T> (val body: (YieldContext<T>) -> Unit): Iterator<T>, YieldContext<T> { private var thread: Thread? = null private val resultQueue = SynchronousQueue<Message>() private val continuationSync = SynchronousQueue<Any>() private var currentMessage: Message? = null { val r = object : Runnable { public override fun run() { try { continuationSync.take() body(this@YieldIterator) resultQueue.put(CompletedMessage()) } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) } } } thread = Thread(r) thread!!.start() } override fun yield(value: T) { resultQueue.put(ValueMessage(value)) continuationSync.take() } public override fun next(): T { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception if (currentMessage !is ValueMessage) throw NoSuchElementException() val value = (currentMessage as ValueMessage).value as T currentMessage = null return value } public override fun hasNext(): Boolean { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception return currentMessage is ValueMessage } private val dummy = Any() private inline fun evaluateNext() { if (currentMessage == null) { continuationSync.put(dummy) currentMessage = resultQueue.take() } } }

, , :
, . , - , put take , ( ).

resultQueue thread , - continuationSync - , .

, continuationSync . :
1) next() hasNext() , , (. evaluateNext()), .
2) body, .. .
3) - yield(T), ( ) , . , .
4) body , , .

, :
private open class Message { } private class ValueMessage(public val value: Any): Message() { } private class CompletedMessage: Message() { } private class ExceptionThrownMessage(public val exception: Throwable): Message() { }

, Test() :
assertEquals(listOf(0, 1, 2, 3), Test().toList())


yieldfun .

context
, , . it :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) it.yield(i); }
.
.. Extension function literals - body , body . YieldContext :
public fun yieldfun<T: Any>(body: YieldContext<T>.() -> Unit): Iterable<T> = YieldIterable<T>(body)

body .
? :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) yield(i); }
.. yieldfun YieldContext, YieldContext this :
this.yield(i)

, this ,
yield(i)
.. body , :
continuationSync.take() body() resultQueue.put(CompletedMessage())
, YieldIterator YieldContext, body - extension-.


. - yield :)

YieldContext ret :
public trait YieldContext<T> { fun yield(value: T): Unit val ret: YieldContext<T> }

YieldIterator this :
override val ret: YieldContext<T> = this
? :
ret.yield(i)
, yield(T) this , :
ret yield i
, .

, C# Kotlin:
fun fibonacci(): Iterable<Int> = yieldfun { var a = 0 var b = 1 while (true) { val sum = a + b; ret yield sum; a = b; b = sum; } }
, , generic- yieldfun - .


, , . . finalize() :
protected fun finalize() { thread?.interrupt() }
catch :
try { continuationSync.take() body() resultQueue.put(CompletedMessage()) } catch (e: InterruptedException) { // if not all items were iterated yield will wait for signal, so finalizer should // interrupt the thread to exit } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) }
, continuationSync , .
, , , yieldfun , InterruptedException , yield try-catch , .


, extension- .Net, .. , "" . , , Jetbrains. , Kotlin .

UPD. bitbucket.org/Nerzhul500/kyield/src
 Iterable.   yieldfun .       : 
fun Test() = yieldfun<Int> { (context) -> for (val i in 0 .. 3) context.yield(i); }

context - , yield.

yieldfun , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> { return YieldIterable<T>(body) }
YieldIterable body - -. , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> = YieldIterable<T>(body)


Iterable , Iterator, YieldIterable :
private class YieldIterable<T>(val body: (YieldContext<T>) -> Unit): Iterable<T>{ public override fun iterator(): Iterator<T> = YieldIterator<T>(body) }


body ( trait) YieldContext, :
trait YieldContext<T> { fun yield(value : T) }

, yield(T) .

yield - YieldIterator:
private class YieldIterator<T> (val body: (YieldContext<T>) -> Unit): Iterator<T>, YieldContext<T> { private var thread: Thread? = null private val resultQueue = SynchronousQueue<Message>() private val continuationSync = SynchronousQueue<Any>() private var currentMessage: Message? = null { val r = object : Runnable { public override fun run() { try { continuationSync.take() body(this@YieldIterator) resultQueue.put(CompletedMessage()) } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) } } } thread = Thread(r) thread!!.start() } override fun yield(value: T) { resultQueue.put(ValueMessage(value)) continuationSync.take() } public override fun next(): T { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception if (currentMessage !is ValueMessage) throw NoSuchElementException() val value = (currentMessage as ValueMessage).value as T currentMessage = null return value } public override fun hasNext(): Boolean { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception return currentMessage is ValueMessage } private val dummy = Any() private inline fun evaluateNext() { if (currentMessage == null) { continuationSync.put(dummy) currentMessage = resultQueue.take() } } }

, , :
, . , - , put take , ( ).

resultQueue thread , - continuationSync - , .

, continuationSync . :
1) next() hasNext() , , (. evaluateNext()), .
2) body, .. .
3) - yield(T), ( ) , . , .
4) body , , .

, :
private open class Message { } private class ValueMessage(public val value: Any): Message() { } private class CompletedMessage: Message() { } private class ExceptionThrownMessage(public val exception: Throwable): Message() { }

, Test() :
assertEquals(listOf(0, 1, 2, 3), Test().toList())


yieldfun .

context
, , . it :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) it.yield(i); }
.
.. Extension function literals - body , body . YieldContext :
public fun yieldfun<T: Any>(body: YieldContext<T>.() -> Unit): Iterable<T> = YieldIterable<T>(body)

body .
? :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) yield(i); }
.. yieldfun YieldContext, YieldContext this :
this.yield(i)

, this ,
yield(i)
.. body , :
continuationSync.take() body() resultQueue.put(CompletedMessage())
, YieldIterator YieldContext, body - extension-.


. - yield :)

YieldContext ret :
public trait YieldContext<T> { fun yield(value: T): Unit val ret: YieldContext<T> }

YieldIterator this :
override val ret: YieldContext<T> = this
? :
ret.yield(i)
, yield(T) this , :
ret yield i
, .

, C# Kotlin:
fun fibonacci(): Iterable<Int> = yieldfun { var a = 0 var b = 1 while (true) { val sum = a + b; ret yield sum; a = b; b = sum; } }
, , generic- yieldfun - .


, , . . finalize() :
protected fun finalize() { thread?.interrupt() }
catch :
try { continuationSync.take() body() resultQueue.put(CompletedMessage()) } catch (e: InterruptedException) { // if not all items were iterated yield will wait for signal, so finalizer should // interrupt the thread to exit } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) }
, continuationSync , .
, , , yieldfun , InterruptedException , yield try-catch , .


, extension- .Net, .. , "" . , , Jetbrains. , Kotlin .

UPD. bitbucket.org/Nerzhul500/kyield/src
Iterable. yieldfun . :
fun Test() = yieldfun<Int> { (context) -> for (val i in 0 .. 3) context.yield(i); }

context - , yield.

yieldfun , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> { return YieldIterable<T>(body) }
YieldIterable body - -. , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> = YieldIterable<T>(body)


Iterable , Iterator, YieldIterable :
private class YieldIterable<T>(val body: (YieldContext<T>) -> Unit): Iterable<T>{ public override fun iterator(): Iterator<T> = YieldIterator<T>(body) }


body ( trait) YieldContext, :
trait YieldContext<T> { fun yield(value : T) }

, yield(T) .

yield - YieldIterator:
private class YieldIterator<T> (val body: (YieldContext<T>) -> Unit): Iterator<T>, YieldContext<T> { private var thread: Thread? = null private val resultQueue = SynchronousQueue<Message>() private val continuationSync = SynchronousQueue<Any>() private var currentMessage: Message? = null { val r = object : Runnable { public override fun run() { try { continuationSync.take() body(this@YieldIterator) resultQueue.put(CompletedMessage()) } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) } } } thread = Thread(r) thread!!.start() } override fun yield(value: T) { resultQueue.put(ValueMessage(value)) continuationSync.take() } public override fun next(): T { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception if (currentMessage !is ValueMessage) throw NoSuchElementException() val value = (currentMessage as ValueMessage).value as T currentMessage = null return value } public override fun hasNext(): Boolean { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception return currentMessage is ValueMessage } private val dummy = Any() private inline fun evaluateNext() { if (currentMessage == null) { continuationSync.put(dummy) currentMessage = resultQueue.take() } } }

, , :
, . , - , put take , ( ).

resultQueue thread , - continuationSync - , .

, continuationSync . :
1) next() hasNext() , , (. evaluateNext()), .
2) body, .. .
3) - yield(T), ( ) , . , .
4) body , , .

, :
private open class Message { } private class ValueMessage(public val value: Any): Message() { } private class CompletedMessage: Message() { } private class ExceptionThrownMessage(public val exception: Throwable): Message() { }

, Test() :
assertEquals(listOf(0, 1, 2, 3), Test().toList())


yieldfun .

context
, , . it :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) it.yield(i); }
.
.. Extension function literals - body , body . YieldContext :
public fun yieldfun<T: Any>(body: YieldContext<T>.() -> Unit): Iterable<T> = YieldIterable<T>(body)

body .
? :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) yield(i); }
.. yieldfun YieldContext, YieldContext this :
this.yield(i)

, this ,
yield(i)
.. body , :
continuationSync.take() body() resultQueue.put(CompletedMessage())
, YieldIterator YieldContext, body - extension-.


. - yield :)

YieldContext ret :
public trait YieldContext<T> { fun yield(value: T): Unit val ret: YieldContext<T> }

YieldIterator this :
override val ret: YieldContext<T> = this
? :
ret.yield(i)
, yield(T) this , :
ret yield i
, .

, C# Kotlin:
fun fibonacci(): Iterable<Int> = yieldfun { var a = 0 var b = 1 while (true) { val sum = a + b; ret yield sum; a = b; b = sum; } }
, , generic- yieldfun - .


, , . . finalize() :
protected fun finalize() { thread?.interrupt() }
catch :
try { continuationSync.take() body() resultQueue.put(CompletedMessage()) } catch (e: InterruptedException) { // if not all items were iterated yield will wait for signal, so finalizer should // interrupt the thread to exit } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) }
, continuationSync , .
, , , yieldfun , InterruptedException , yield try-catch , .


, extension- .Net, .. , "" . , , Jetbrains. , Kotlin .

UPD. bitbucket.org/Nerzhul500/kyield/src
 Iterable.   yieldfun .       : 
fun Test() = yieldfun<Int> { (context) -> for (val i in 0 .. 3) context.yield(i); }

context - , yield.

yieldfun , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> { return YieldIterable<T>(body) }
YieldIterable body - -. , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> = YieldIterable<T>(body)


Iterable , Iterator, YieldIterable :
private class YieldIterable<T>(val body: (YieldContext<T>) -> Unit): Iterable<T>{ public override fun iterator(): Iterator<T> = YieldIterator<T>(body) }


body ( trait) YieldContext, :
trait YieldContext<T> { fun yield(value : T) }

, yield(T) .

yield - YieldIterator:
private class YieldIterator<T> (val body: (YieldContext<T>) -> Unit): Iterator<T>, YieldContext<T> { private var thread: Thread? = null private val resultQueue = SynchronousQueue<Message>() private val continuationSync = SynchronousQueue<Any>() private var currentMessage: Message? = null { val r = object : Runnable { public override fun run() { try { continuationSync.take() body(this@YieldIterator) resultQueue.put(CompletedMessage()) } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) } } } thread = Thread(r) thread!!.start() } override fun yield(value: T) { resultQueue.put(ValueMessage(value)) continuationSync.take() } public override fun next(): T { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception if (currentMessage !is ValueMessage) throw NoSuchElementException() val value = (currentMessage as ValueMessage).value as T currentMessage = null return value } public override fun hasNext(): Boolean { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception return currentMessage is ValueMessage } private val dummy = Any() private inline fun evaluateNext() { if (currentMessage == null) { continuationSync.put(dummy) currentMessage = resultQueue.take() } } }

, , :
, . , - , put take , ( ).

resultQueue thread , - continuationSync - , .

, continuationSync . :
1) next() hasNext() , , (. evaluateNext()), .
2) body, .. .
3) - yield(T), ( ) , . , .
4) body , , .

, :
private open class Message { } private class ValueMessage(public val value: Any): Message() { } private class CompletedMessage: Message() { } private class ExceptionThrownMessage(public val exception: Throwable): Message() { }

, Test() :
assertEquals(listOf(0, 1, 2, 3), Test().toList())


yieldfun .

context
, , . it :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) it.yield(i); }
.
.. Extension function literals - body , body . YieldContext :
public fun yieldfun<T: Any>(body: YieldContext<T>.() -> Unit): Iterable<T> = YieldIterable<T>(body)

body .
? :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) yield(i); }
.. yieldfun YieldContext, YieldContext this :
this.yield(i)

, this ,
yield(i)
.. body , :
continuationSync.take() body() resultQueue.put(CompletedMessage())
, YieldIterator YieldContext, body - extension-.


. - yield :)

YieldContext ret :
public trait YieldContext<T> { fun yield(value: T): Unit val ret: YieldContext<T> }

YieldIterator this :
override val ret: YieldContext<T> = this
? :
ret.yield(i)
, yield(T) this , :
ret yield i
, .

, C# Kotlin:
fun fibonacci(): Iterable<Int> = yieldfun { var a = 0 var b = 1 while (true) { val sum = a + b; ret yield sum; a = b; b = sum; } }
, , generic- yieldfun - .


, , . . finalize() :
protected fun finalize() { thread?.interrupt() }
catch :
try { continuationSync.take() body() resultQueue.put(CompletedMessage()) } catch (e: InterruptedException) { // if not all items were iterated yield will wait for signal, so finalizer should // interrupt the thread to exit } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) }
, continuationSync , .
, , , yieldfun , InterruptedException , yield try-catch , .


, extension- .Net, .. , "" . , , Jetbrains. , Kotlin .

UPD. bitbucket.org/Nerzhul500/kyield/src
Iterable. yieldfun . :
fun Test() = yieldfun<Int> { (context) -> for (val i in 0 .. 3) context.yield(i); }

context - , yield.

yieldfun , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> { return YieldIterable<T>(body) }
YieldIterable body - -. , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> = YieldIterable<T>(body)


Iterable , Iterator, YieldIterable :
private class YieldIterable<T>(val body: (YieldContext<T>) -> Unit): Iterable<T>{ public override fun iterator(): Iterator<T> = YieldIterator<T>(body) }


body ( trait) YieldContext, :
trait YieldContext<T> { fun yield(value : T) }

, yield(T) .

yield - YieldIterator:
private class YieldIterator<T> (val body: (YieldContext<T>) -> Unit): Iterator<T>, YieldContext<T> { private var thread: Thread? = null private val resultQueue = SynchronousQueue<Message>() private val continuationSync = SynchronousQueue<Any>() private var currentMessage: Message? = null { val r = object : Runnable { public override fun run() { try { continuationSync.take() body(this@YieldIterator) resultQueue.put(CompletedMessage()) } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) } } } thread = Thread(r) thread!!.start() } override fun yield(value: T) { resultQueue.put(ValueMessage(value)) continuationSync.take() } public override fun next(): T { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception if (currentMessage !is ValueMessage) throw NoSuchElementException() val value = (currentMessage as ValueMessage).value as T currentMessage = null return value } public override fun hasNext(): Boolean { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception return currentMessage is ValueMessage } private val dummy = Any() private inline fun evaluateNext() { if (currentMessage == null) { continuationSync.put(dummy) currentMessage = resultQueue.take() } } }

, , :
, . , - , put take , ( ).

resultQueue thread , - continuationSync - , .

, continuationSync . :
1) next() hasNext() , , (. evaluateNext()), .
2) body, .. .
3) - yield(T), ( ) , . , .
4) body , , .

, :
private open class Message { } private class ValueMessage(public val value: Any): Message() { } private class CompletedMessage: Message() { } private class ExceptionThrownMessage(public val exception: Throwable): Message() { }

, Test() :
assertEquals(listOf(0, 1, 2, 3), Test().toList())


yieldfun .

context
, , . it :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) it.yield(i); }
.
.. Extension function literals - body , body . YieldContext :
public fun yieldfun<T: Any>(body: YieldContext<T>.() -> Unit): Iterable<T> = YieldIterable<T>(body)

body .
? :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) yield(i); }
.. yieldfun YieldContext, YieldContext this :
this.yield(i)

, this ,
yield(i)
.. body , :
continuationSync.take() body() resultQueue.put(CompletedMessage())
, YieldIterator YieldContext, body - extension-.


. - yield :)

YieldContext ret :
public trait YieldContext<T> { fun yield(value: T): Unit val ret: YieldContext<T> }

YieldIterator this :
override val ret: YieldContext<T> = this
? :
ret.yield(i)
, yield(T) this , :
ret yield i
, .

, C# Kotlin:
fun fibonacci(): Iterable<Int> = yieldfun { var a = 0 var b = 1 while (true) { val sum = a + b; ret yield sum; a = b; b = sum; } }
, , generic- yieldfun - .


, , . . finalize() :
protected fun finalize() { thread?.interrupt() }
catch :
try { continuationSync.take() body() resultQueue.put(CompletedMessage()) } catch (e: InterruptedException) { // if not all items were iterated yield will wait for signal, so finalizer should // interrupt the thread to exit } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) }
, continuationSync , .
, , , yieldfun , InterruptedException , yield try-catch , .


, extension- .Net, .. , "" . , , Jetbrains. , Kotlin .

UPD. bitbucket.org/Nerzhul500/kyield/src
Iterable. yieldfun . :
fun Test() = yieldfun<Int> { (context) -> for (val i in 0 .. 3) context.yield(i); }

context - , yield.

yieldfun , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> { return YieldIterable<T>(body) }
YieldIterable body - -. , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> = YieldIterable<T>(body)


Iterable , Iterator, YieldIterable :
private class YieldIterable<T>(val body: (YieldContext<T>) -> Unit): Iterable<T>{ public override fun iterator(): Iterator<T> = YieldIterator<T>(body) }


body ( trait) YieldContext, :
trait YieldContext<T> { fun yield(value : T) }

, yield(T) .

yield - YieldIterator:
private class YieldIterator<T> (val body: (YieldContext<T>) -> Unit): Iterator<T>, YieldContext<T> { private var thread: Thread? = null private val resultQueue = SynchronousQueue<Message>() private val continuationSync = SynchronousQueue<Any>() private var currentMessage: Message? = null { val r = object : Runnable { public override fun run() { try { continuationSync.take() body(this@YieldIterator) resultQueue.put(CompletedMessage()) } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) } } } thread = Thread(r) thread!!.start() } override fun yield(value: T) { resultQueue.put(ValueMessage(value)) continuationSync.take() } public override fun next(): T { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception if (currentMessage !is ValueMessage) throw NoSuchElementException() val value = (currentMessage as ValueMessage).value as T currentMessage = null return value } public override fun hasNext(): Boolean { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception return currentMessage is ValueMessage } private val dummy = Any() private inline fun evaluateNext() { if (currentMessage == null) { continuationSync.put(dummy) currentMessage = resultQueue.take() } } }

, , :
, . , - , put take , ( ).

resultQueue thread , - continuationSync - , .

, continuationSync . :
1) next() hasNext() , , (. evaluateNext()), .
2) body, .. .
3) - yield(T), ( ) , . , .
4) body , , .

, :
private open class Message { } private class ValueMessage(public val value: Any): Message() { } private class CompletedMessage: Message() { } private class ExceptionThrownMessage(public val exception: Throwable): Message() { }

, Test() :
assertEquals(listOf(0, 1, 2, 3), Test().toList())


yieldfun .

context
, , . it :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) it.yield(i); }
.
.. Extension function literals - body , body . YieldContext :
public fun yieldfun<T: Any>(body: YieldContext<T>.() -> Unit): Iterable<T> = YieldIterable<T>(body)

body .
? :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) yield(i); }
.. yieldfun YieldContext, YieldContext this :
this.yield(i)

, this ,
yield(i)
.. body , :
continuationSync.take() body() resultQueue.put(CompletedMessage())
, YieldIterator YieldContext, body - extension-.


. - yield :)

YieldContext ret :
public trait YieldContext<T> { fun yield(value: T): Unit val ret: YieldContext<T> }

YieldIterator this :
override val ret: YieldContext<T> = this
? :
ret.yield(i)
, yield(T) this , :
ret yield i
, .

, C# Kotlin:
fun fibonacci(): Iterable<Int> = yieldfun { var a = 0 var b = 1 while (true) { val sum = a + b; ret yield sum; a = b; b = sum; } }
, , generic- yieldfun - .


, , . . finalize() :
protected fun finalize() { thread?.interrupt() }
catch :
try { continuationSync.take() body() resultQueue.put(CompletedMessage()) } catch (e: InterruptedException) { // if not all items were iterated yield will wait for signal, so finalizer should // interrupt the thread to exit } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) }
, continuationSync , .
, , , yieldfun , InterruptedException , yield try-catch , .


, extension- .Net, .. , "" . , , Jetbrains. , Kotlin .

UPD. bitbucket.org/Nerzhul500/kyield/src
Iterable. yieldfun . :
fun Test() = yieldfun<Int> { (context) -> for (val i in 0 .. 3) context.yield(i); }

context - , yield.

yieldfun , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> { return YieldIterable<T>(body) }
YieldIterable body - -. , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> = YieldIterable<T>(body)


Iterable , Iterator, YieldIterable :
private class YieldIterable<T>(val body: (YieldContext<T>) -> Unit): Iterable<T>{ public override fun iterator(): Iterator<T> = YieldIterator<T>(body) }


body ( trait) YieldContext, :
trait YieldContext<T> { fun yield(value : T) }

, yield(T) .

yield - YieldIterator:
private class YieldIterator<T> (val body: (YieldContext<T>) -> Unit): Iterator<T>, YieldContext<T> { private var thread: Thread? = null private val resultQueue = SynchronousQueue<Message>() private val continuationSync = SynchronousQueue<Any>() private var currentMessage: Message? = null { val r = object : Runnable { public override fun run() { try { continuationSync.take() body(this@YieldIterator) resultQueue.put(CompletedMessage()) } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) } } } thread = Thread(r) thread!!.start() } override fun yield(value: T) { resultQueue.put(ValueMessage(value)) continuationSync.take() } public override fun next(): T { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception if (currentMessage !is ValueMessage) throw NoSuchElementException() val value = (currentMessage as ValueMessage).value as T currentMessage = null return value } public override fun hasNext(): Boolean { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception return currentMessage is ValueMessage } private val dummy = Any() private inline fun evaluateNext() { if (currentMessage == null) { continuationSync.put(dummy) currentMessage = resultQueue.take() } } }

, , :
, . , - , put take , ( ).

resultQueue thread , - continuationSync - , .

, continuationSync . :
1) next() hasNext() , , (. evaluateNext()), .
2) body, .. .
3) - yield(T), ( ) , . , .
4) body , , .

, :
private open class Message { } private class ValueMessage(public val value: Any): Message() { } private class CompletedMessage: Message() { } private class ExceptionThrownMessage(public val exception: Throwable): Message() { }

, Test() :
assertEquals(listOf(0, 1, 2, 3), Test().toList())


yieldfun .

context
, , . it :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) it.yield(i); }
.
.. Extension function literals - body , body . YieldContext :
public fun yieldfun<T: Any>(body: YieldContext<T>.() -> Unit): Iterable<T> = YieldIterable<T>(body)

body .
? :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) yield(i); }
.. yieldfun YieldContext, YieldContext this :
this.yield(i)

, this ,
yield(i)
.. body , :
continuationSync.take() body() resultQueue.put(CompletedMessage())
, YieldIterator YieldContext, body - extension-.


. - yield :)

YieldContext ret :
public trait YieldContext<T> { fun yield(value: T): Unit val ret: YieldContext<T> }

YieldIterator this :
override val ret: YieldContext<T> = this
? :
ret.yield(i)
, yield(T) this , :
ret yield i
, .

, C# Kotlin:
fun fibonacci(): Iterable<Int> = yieldfun { var a = 0 var b = 1 while (true) { val sum = a + b; ret yield sum; a = b; b = sum; } }
, , generic- yieldfun - .


, , . . finalize() :
protected fun finalize() { thread?.interrupt() }
catch :
try { continuationSync.take() body() resultQueue.put(CompletedMessage()) } catch (e: InterruptedException) { // if not all items were iterated yield will wait for signal, so finalizer should // interrupt the thread to exit } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) }
, continuationSync , .
, , , yieldfun , InterruptedException , yield try-catch , .


, extension- .Net, .. , "" . , , Jetbrains. , Kotlin .

UPD. bitbucket.org/Nerzhul500/kyield/src
 Iterable.   yieldfun .       : 
fun Test() = yieldfun<Int> { (context) -> for (val i in 0 .. 3) context.yield(i); }

context - , yield.

yieldfun , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> { return YieldIterable<T>(body) }
YieldIterable body - -. , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> = YieldIterable<T>(body)


Iterable , Iterator, YieldIterable :
private class YieldIterable<T>(val body: (YieldContext<T>) -> Unit): Iterable<T>{ public override fun iterator(): Iterator<T> = YieldIterator<T>(body) }


body ( trait) YieldContext, :
trait YieldContext<T> { fun yield(value : T) }

, yield(T) .

yield - YieldIterator:
private class YieldIterator<T> (val body: (YieldContext<T>) -> Unit): Iterator<T>, YieldContext<T> { private var thread: Thread? = null private val resultQueue = SynchronousQueue<Message>() private val continuationSync = SynchronousQueue<Any>() private var currentMessage: Message? = null { val r = object : Runnable { public override fun run() { try { continuationSync.take() body(this@YieldIterator) resultQueue.put(CompletedMessage()) } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) } } } thread = Thread(r) thread!!.start() } override fun yield(value: T) { resultQueue.put(ValueMessage(value)) continuationSync.take() } public override fun next(): T { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception if (currentMessage !is ValueMessage) throw NoSuchElementException() val value = (currentMessage as ValueMessage).value as T currentMessage = null return value } public override fun hasNext(): Boolean { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception return currentMessage is ValueMessage } private val dummy = Any() private inline fun evaluateNext() { if (currentMessage == null) { continuationSync.put(dummy) currentMessage = resultQueue.take() } } }

, , :
, . , - , put take , ( ).

resultQueue thread , - continuationSync - , .

, continuationSync . :
1) next() hasNext() , , (. evaluateNext()), .
2) body, .. .
3) - yield(T), ( ) , . , .
4) body , , .

, :
private open class Message { } private class ValueMessage(public val value: Any): Message() { } private class CompletedMessage: Message() { } private class ExceptionThrownMessage(public val exception: Throwable): Message() { }

, Test() :
assertEquals(listOf(0, 1, 2, 3), Test().toList())


yieldfun .

context
, , . it :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) it.yield(i); }
.
.. Extension function literals - body , body . YieldContext :
public fun yieldfun<T: Any>(body: YieldContext<T>.() -> Unit): Iterable<T> = YieldIterable<T>(body)

body .
? :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) yield(i); }
.. yieldfun YieldContext, YieldContext this :
this.yield(i)

, this ,
yield(i)
.. body , :
continuationSync.take() body() resultQueue.put(CompletedMessage())
, YieldIterator YieldContext, body - extension-.


. - yield :)

YieldContext ret :
public trait YieldContext<T> { fun yield(value: T): Unit val ret: YieldContext<T> }

YieldIterator this :
override val ret: YieldContext<T> = this
? :
ret.yield(i)
, yield(T) this , :
ret yield i
, .

, C# Kotlin:
fun fibonacci(): Iterable<Int> = yieldfun { var a = 0 var b = 1 while (true) { val sum = a + b; ret yield sum; a = b; b = sum; } }
, , generic- yieldfun - .


, , . . finalize() :
protected fun finalize() { thread?.interrupt() }
catch :
try { continuationSync.take() body() resultQueue.put(CompletedMessage()) } catch (e: InterruptedException) { // if not all items were iterated yield will wait for signal, so finalizer should // interrupt the thread to exit } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) }
, continuationSync , .
, , , yieldfun , InterruptedException , yield try-catch , .


, extension- .Net, .. , "" . , , Jetbrains. , Kotlin .

UPD. bitbucket.org/Nerzhul500/kyield/src
Iterable. yieldfun . :
fun Test() = yieldfun<Int> { (context) -> for (val i in 0 .. 3) context.yield(i); }

context - , yield.

yieldfun , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> { return YieldIterable<T>(body) }
YieldIterable body - -. , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> = YieldIterable<T>(body)


Iterable , Iterator, YieldIterable :
private class YieldIterable<T>(val body: (YieldContext<T>) -> Unit): Iterable<T>{ public override fun iterator(): Iterator<T> = YieldIterator<T>(body) }


body ( trait) YieldContext, :
trait YieldContext<T> { fun yield(value : T) }

, yield(T) .

yield - YieldIterator:
private class YieldIterator<T> (val body: (YieldContext<T>) -> Unit): Iterator<T>, YieldContext<T> { private var thread: Thread? = null private val resultQueue = SynchronousQueue<Message>() private val continuationSync = SynchronousQueue<Any>() private var currentMessage: Message? = null { val r = object : Runnable { public override fun run() { try { continuationSync.take() body(this@YieldIterator) resultQueue.put(CompletedMessage()) } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) } } } thread = Thread(r) thread!!.start() } override fun yield(value: T) { resultQueue.put(ValueMessage(value)) continuationSync.take() } public override fun next(): T { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception if (currentMessage !is ValueMessage) throw NoSuchElementException() val value = (currentMessage as ValueMessage).value as T currentMessage = null return value } public override fun hasNext(): Boolean { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception return currentMessage is ValueMessage } private val dummy = Any() private inline fun evaluateNext() { if (currentMessage == null) { continuationSync.put(dummy) currentMessage = resultQueue.take() } } }

, , :
, . , - , put take , ( ).

resultQueue thread , - continuationSync - , .

, continuationSync . :
1) next() hasNext() , , (. evaluateNext()), .
2) body, .. .
3) - yield(T), ( ) , . , .
4) body , , .

, :
private open class Message { } private class ValueMessage(public val value: Any): Message() { } private class CompletedMessage: Message() { } private class ExceptionThrownMessage(public val exception: Throwable): Message() { }

, Test() :
assertEquals(listOf(0, 1, 2, 3), Test().toList())


yieldfun .

context
, , . it :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) it.yield(i); }
.
.. Extension function literals - body , body . YieldContext :
public fun yieldfun<T: Any>(body: YieldContext<T>.() -> Unit): Iterable<T> = YieldIterable<T>(body)

body .
? :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) yield(i); }
.. yieldfun YieldContext, YieldContext this :
this.yield(i)

, this ,
yield(i)
.. body , :
continuationSync.take() body() resultQueue.put(CompletedMessage())
, YieldIterator YieldContext, body - extension-.


. - yield :)

YieldContext ret :
public trait YieldContext<T> { fun yield(value: T): Unit val ret: YieldContext<T> }

YieldIterator this :
override val ret: YieldContext<T> = this
? :
ret.yield(i)
, yield(T) this , :
ret yield i
, .

, C# Kotlin:
fun fibonacci(): Iterable<Int> = yieldfun { var a = 0 var b = 1 while (true) { val sum = a + b; ret yield sum; a = b; b = sum; } }
, , generic- yieldfun - .


, , . . finalize() :
protected fun finalize() { thread?.interrupt() }
catch :
try { continuationSync.take() body() resultQueue.put(CompletedMessage()) } catch (e: InterruptedException) { // if not all items were iterated yield will wait for signal, so finalizer should // interrupt the thread to exit } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) }
, continuationSync , .
, , , yieldfun , InterruptedException , yield try-catch , .


, extension- .Net, .. , "" . , , Jetbrains. , Kotlin .

UPD. bitbucket.org/Nerzhul500/kyield/src
 Iterable.   yieldfun .       : 
fun Test() = yieldfun<Int> { (context) -> for (val i in 0 .. 3) context.yield(i); }

context - , yield.

yieldfun , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> { return YieldIterable<T>(body) }
YieldIterable body - -. , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> = YieldIterable<T>(body)


Iterable , Iterator, YieldIterable :
private class YieldIterable<T>(val body: (YieldContext<T>) -> Unit): Iterable<T>{ public override fun iterator(): Iterator<T> = YieldIterator<T>(body) }


body ( trait) YieldContext, :
trait YieldContext<T> { fun yield(value : T) }

, yield(T) .

yield - YieldIterator:
private class YieldIterator<T> (val body: (YieldContext<T>) -> Unit): Iterator<T>, YieldContext<T> { private var thread: Thread? = null private val resultQueue = SynchronousQueue<Message>() private val continuationSync = SynchronousQueue<Any>() private var currentMessage: Message? = null { val r = object : Runnable { public override fun run() { try { continuationSync.take() body(this@YieldIterator) resultQueue.put(CompletedMessage()) } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) } } } thread = Thread(r) thread!!.start() } override fun yield(value: T) { resultQueue.put(ValueMessage(value)) continuationSync.take() } public override fun next(): T { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception if (currentMessage !is ValueMessage) throw NoSuchElementException() val value = (currentMessage as ValueMessage).value as T currentMessage = null return value } public override fun hasNext(): Boolean { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception return currentMessage is ValueMessage } private val dummy = Any() private inline fun evaluateNext() { if (currentMessage == null) { continuationSync.put(dummy) currentMessage = resultQueue.take() } } }

, , :
, . , - , put take , ( ).

resultQueue thread , - continuationSync - , .

, continuationSync . :
1) next() hasNext() , , (. evaluateNext()), .
2) body, .. .
3) - yield(T), ( ) , . , .
4) body , , .

, :
private open class Message { } private class ValueMessage(public val value: Any): Message() { } private class CompletedMessage: Message() { } private class ExceptionThrownMessage(public val exception: Throwable): Message() { }

, Test() :
assertEquals(listOf(0, 1, 2, 3), Test().toList())


yieldfun .

context
, , . it :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) it.yield(i); }
.
.. Extension function literals - body , body . YieldContext :
public fun yieldfun<T: Any>(body: YieldContext<T>.() -> Unit): Iterable<T> = YieldIterable<T>(body)

body .
? :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) yield(i); }
.. yieldfun YieldContext, YieldContext this :
this.yield(i)

, this ,
yield(i)
.. body , :
continuationSync.take() body() resultQueue.put(CompletedMessage())
, YieldIterator YieldContext, body - extension-.


. - yield :)

YieldContext ret :
public trait YieldContext<T> { fun yield(value: T): Unit val ret: YieldContext<T> }

YieldIterator this :
override val ret: YieldContext<T> = this
? :
ret.yield(i)
, yield(T) this , :
ret yield i
, .

, C# Kotlin:
fun fibonacci(): Iterable<Int> = yieldfun { var a = 0 var b = 1 while (true) { val sum = a + b; ret yield sum; a = b; b = sum; } }
, , generic- yieldfun - .


, , . . finalize() :
protected fun finalize() { thread?.interrupt() }
catch :
try { continuationSync.take() body() resultQueue.put(CompletedMessage()) } catch (e: InterruptedException) { // if not all items were iterated yield will wait for signal, so finalizer should // interrupt the thread to exit } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) }
, continuationSync , .
, , , yieldfun , InterruptedException , yield try-catch , .


, extension- .Net, .. , "" . , , Jetbrains. , Kotlin .

UPD. bitbucket.org/Nerzhul500/kyield/src
Iterable. yieldfun . :
fun Test() = yieldfun<Int> { (context) -> for (val i in 0 .. 3) context.yield(i); }

context - , yield.

yieldfun , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> { return YieldIterable<T>(body) }
YieldIterable body - -. , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> = YieldIterable<T>(body)


Iterable , Iterator, YieldIterable :
private class YieldIterable<T>(val body: (YieldContext<T>) -> Unit): Iterable<T>{ public override fun iterator(): Iterator<T> = YieldIterator<T>(body) }


body ( trait) YieldContext, :
trait YieldContext<T> { fun yield(value : T) }

, yield(T) .

yield - YieldIterator:
private class YieldIterator<T> (val body: (YieldContext<T>) -> Unit): Iterator<T>, YieldContext<T> { private var thread: Thread? = null private val resultQueue = SynchronousQueue<Message>() private val continuationSync = SynchronousQueue<Any>() private var currentMessage: Message? = null { val r = object : Runnable { public override fun run() { try { continuationSync.take() body(this@YieldIterator) resultQueue.put(CompletedMessage()) } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) } } } thread = Thread(r) thread!!.start() } override fun yield(value: T) { resultQueue.put(ValueMessage(value)) continuationSync.take() } public override fun next(): T { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception if (currentMessage !is ValueMessage) throw NoSuchElementException() val value = (currentMessage as ValueMessage).value as T currentMessage = null return value } public override fun hasNext(): Boolean { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception return currentMessage is ValueMessage } private val dummy = Any() private inline fun evaluateNext() { if (currentMessage == null) { continuationSync.put(dummy) currentMessage = resultQueue.take() } } }

, , :
, . , - , put take , ( ).

resultQueue thread , - continuationSync - , .

, continuationSync . :
1) next() hasNext() , , (. evaluateNext()), .
2) body, .. .
3) - yield(T), ( ) , . , .
4) body , , .

, :
private open class Message { } private class ValueMessage(public val value: Any): Message() { } private class CompletedMessage: Message() { } private class ExceptionThrownMessage(public val exception: Throwable): Message() { }

, Test() :
assertEquals(listOf(0, 1, 2, 3), Test().toList())


yieldfun .

context
, , . it :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) it.yield(i); }
.
.. Extension function literals - body , body . YieldContext :
public fun yieldfun<T: Any>(body: YieldContext<T>.() -> Unit): Iterable<T> = YieldIterable<T>(body)

body .
? :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) yield(i); }
.. yieldfun YieldContext, YieldContext this :
this.yield(i)

, this ,
yield(i)
.. body , :
continuationSync.take() body() resultQueue.put(CompletedMessage())
, YieldIterator YieldContext, body - extension-.


. - yield :)

YieldContext ret :
public trait YieldContext<T> { fun yield(value: T): Unit val ret: YieldContext<T> }

YieldIterator this :
override val ret: YieldContext<T> = this
? :
ret.yield(i)
, yield(T) this , :
ret yield i
, .

, C# Kotlin:
fun fibonacci(): Iterable<Int> = yieldfun { var a = 0 var b = 1 while (true) { val sum = a + b; ret yield sum; a = b; b = sum; } }
, , generic- yieldfun - .


, , . . finalize() :
protected fun finalize() { thread?.interrupt() }
catch :
try { continuationSync.take() body() resultQueue.put(CompletedMessage()) } catch (e: InterruptedException) { // if not all items were iterated yield will wait for signal, so finalizer should // interrupt the thread to exit } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) }
, continuationSync , .
, , , yieldfun , InterruptedException , yield try-catch , .


, extension- .Net, .. , "" . , , Jetbrains. , Kotlin .

UPD. bitbucket.org/Nerzhul500/kyield/src
 Iterable.   yieldfun .       : 
fun Test() = yieldfun<Int> { (context) -> for (val i in 0 .. 3) context.yield(i); }

context - , yield.

yieldfun , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> { return YieldIterable<T>(body) }
YieldIterable body - -. , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> = YieldIterable<T>(body)


Iterable , Iterator, YieldIterable :
private class YieldIterable<T>(val body: (YieldContext<T>) -> Unit): Iterable<T>{ public override fun iterator(): Iterator<T> = YieldIterator<T>(body) }


body ( trait) YieldContext, :
trait YieldContext<T> { fun yield(value : T) }

, yield(T) .

yield - YieldIterator:
private class YieldIterator<T> (val body: (YieldContext<T>) -> Unit): Iterator<T>, YieldContext<T> { private var thread: Thread? = null private val resultQueue = SynchronousQueue<Message>() private val continuationSync = SynchronousQueue<Any>() private var currentMessage: Message? = null { val r = object : Runnable { public override fun run() { try { continuationSync.take() body(this@YieldIterator) resultQueue.put(CompletedMessage()) } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) } } } thread = Thread(r) thread!!.start() } override fun yield(value: T) { resultQueue.put(ValueMessage(value)) continuationSync.take() } public override fun next(): T { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception if (currentMessage !is ValueMessage) throw NoSuchElementException() val value = (currentMessage as ValueMessage).value as T currentMessage = null return value } public override fun hasNext(): Boolean { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception return currentMessage is ValueMessage } private val dummy = Any() private inline fun evaluateNext() { if (currentMessage == null) { continuationSync.put(dummy) currentMessage = resultQueue.take() } } }

, , :
, . , - , put take , ( ).

resultQueue thread , - continuationSync - , .

, continuationSync . :
1) next() hasNext() , , (. evaluateNext()), .
2) body, .. .
3) - yield(T), ( ) , . , .
4) body , , .

, :
private open class Message { } private class ValueMessage(public val value: Any): Message() { } private class CompletedMessage: Message() { } private class ExceptionThrownMessage(public val exception: Throwable): Message() { }

, Test() :
assertEquals(listOf(0, 1, 2, 3), Test().toList())


yieldfun .

context
, , . it :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) it.yield(i); }
.
.. Extension function literals - body , body . YieldContext :
public fun yieldfun<T: Any>(body: YieldContext<T>.() -> Unit): Iterable<T> = YieldIterable<T>(body)

body .
? :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) yield(i); }
.. yieldfun YieldContext, YieldContext this :
this.yield(i)

, this ,
yield(i)
.. body , :
continuationSync.take() body() resultQueue.put(CompletedMessage())
, YieldIterator YieldContext, body - extension-.


. - yield :)

YieldContext ret :
public trait YieldContext<T> { fun yield(value: T): Unit val ret: YieldContext<T> }

YieldIterator this :
override val ret: YieldContext<T> = this
? :
ret.yield(i)
, yield(T) this , :
ret yield i
, .

, C# Kotlin:
fun fibonacci(): Iterable<Int> = yieldfun { var a = 0 var b = 1 while (true) { val sum = a + b; ret yield sum; a = b; b = sum; } }
, , generic- yieldfun - .


, , . . finalize() :
protected fun finalize() { thread?.interrupt() }
catch :
try { continuationSync.take() body() resultQueue.put(CompletedMessage()) } catch (e: InterruptedException) { // if not all items were iterated yield will wait for signal, so finalizer should // interrupt the thread to exit } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) }
, continuationSync , .
, , , yieldfun , InterruptedException , yield try-catch , .


, extension- .Net, .. , "" . , , Jetbrains. , Kotlin .

UPD. bitbucket.org/Nerzhul500/kyield/src
Iterable. yieldfun . :
fun Test() = yieldfun<Int> { (context) -> for (val i in 0 .. 3) context.yield(i); }

context - , yield.

yieldfun , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> { return YieldIterable<T>(body) }
YieldIterable body - -. , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> = YieldIterable<T>(body)


Iterable , Iterator, YieldIterable :
private class YieldIterable<T>(val body: (YieldContext<T>) -> Unit): Iterable<T>{ public override fun iterator(): Iterator<T> = YieldIterator<T>(body) }


body ( trait) YieldContext, :
trait YieldContext<T> { fun yield(value : T) }

, yield(T) .

yield - YieldIterator:
private class YieldIterator<T> (val body: (YieldContext<T>) -> Unit): Iterator<T>, YieldContext<T> { private var thread: Thread? = null private val resultQueue = SynchronousQueue<Message>() private val continuationSync = SynchronousQueue<Any>() private var currentMessage: Message? = null { val r = object : Runnable { public override fun run() { try { continuationSync.take() body(this@YieldIterator) resultQueue.put(CompletedMessage()) } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) } } } thread = Thread(r) thread!!.start() } override fun yield(value: T) { resultQueue.put(ValueMessage(value)) continuationSync.take() } public override fun next(): T { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception if (currentMessage !is ValueMessage) throw NoSuchElementException() val value = (currentMessage as ValueMessage).value as T currentMessage = null return value } public override fun hasNext(): Boolean { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception return currentMessage is ValueMessage } private val dummy = Any() private inline fun evaluateNext() { if (currentMessage == null) { continuationSync.put(dummy) currentMessage = resultQueue.take() } } }

, , :
, . , - , put take , ( ).

resultQueue thread , - continuationSync - , .

, continuationSync . :
1) next() hasNext() , , (. evaluateNext()), .
2) body, .. .
3) - yield(T), ( ) , . , .
4) body , , .

, :
private open class Message { } private class ValueMessage(public val value: Any): Message() { } private class CompletedMessage: Message() { } private class ExceptionThrownMessage(public val exception: Throwable): Message() { }

, Test() :
assertEquals(listOf(0, 1, 2, 3), Test().toList())


yieldfun .

context
, , . it :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) it.yield(i); }
.
.. Extension function literals - body , body . YieldContext :
public fun yieldfun<T: Any>(body: YieldContext<T>.() -> Unit): Iterable<T> = YieldIterable<T>(body)

body .
? :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) yield(i); }
.. yieldfun YieldContext, YieldContext this :
this.yield(i)

, this ,
yield(i)
.. body , :
continuationSync.take() body() resultQueue.put(CompletedMessage())
, YieldIterator YieldContext, body - extension-.


. - yield :)

YieldContext ret :
public trait YieldContext<T> { fun yield(value: T): Unit val ret: YieldContext<T> }

YieldIterator this :
override val ret: YieldContext<T> = this
? :
ret.yield(i)
, yield(T) this , :
ret yield i
, .

, C# Kotlin:
fun fibonacci(): Iterable<Int> = yieldfun { var a = 0 var b = 1 while (true) { val sum = a + b; ret yield sum; a = b; b = sum; } }
, , generic- yieldfun - .


, , . . finalize() :
protected fun finalize() { thread?.interrupt() }
catch :
try { continuationSync.take() body() resultQueue.put(CompletedMessage()) } catch (e: InterruptedException) { // if not all items were iterated yield will wait for signal, so finalizer should // interrupt the thread to exit } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) }
, continuationSync , .
, , , yieldfun , InterruptedException , yield try-catch , .


, extension- .Net, .. , "" . , , Jetbrains. , Kotlin .

UPD. bitbucket.org/Nerzhul500/kyield/src
 Iterable.   yieldfun .       : 
fun Test() = yieldfun<Int> { (context) -> for (val i in 0 .. 3) context.yield(i); }

context - , yield.

yieldfun , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> { return YieldIterable<T>(body) }
YieldIterable body - -. , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> = YieldIterable<T>(body)


Iterable , Iterator, YieldIterable :
private class YieldIterable<T>(val body: (YieldContext<T>) -> Unit): Iterable<T>{ public override fun iterator(): Iterator<T> = YieldIterator<T>(body) }


body ( trait) YieldContext, :
trait YieldContext<T> { fun yield(value : T) }

, yield(T) .

yield - YieldIterator:
private class YieldIterator<T> (val body: (YieldContext<T>) -> Unit): Iterator<T>, YieldContext<T> { private var thread: Thread? = null private val resultQueue = SynchronousQueue<Message>() private val continuationSync = SynchronousQueue<Any>() private var currentMessage: Message? = null { val r = object : Runnable { public override fun run() { try { continuationSync.take() body(this@YieldIterator) resultQueue.put(CompletedMessage()) } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) } } } thread = Thread(r) thread!!.start() } override fun yield(value: T) { resultQueue.put(ValueMessage(value)) continuationSync.take() } public override fun next(): T { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception if (currentMessage !is ValueMessage) throw NoSuchElementException() val value = (currentMessage as ValueMessage).value as T currentMessage = null return value } public override fun hasNext(): Boolean { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception return currentMessage is ValueMessage } private val dummy = Any() private inline fun evaluateNext() { if (currentMessage == null) { continuationSync.put(dummy) currentMessage = resultQueue.take() } } }

, , :
, . , - , put take , ( ).

resultQueue thread , - continuationSync - , .

, continuationSync . :
1) next() hasNext() , , (. evaluateNext()), .
2) body, .. .
3) - yield(T), ( ) , . , .
4) body , , .

, :
private open class Message { } private class ValueMessage(public val value: Any): Message() { } private class CompletedMessage: Message() { } private class ExceptionThrownMessage(public val exception: Throwable): Message() { }

, Test() :
assertEquals(listOf(0, 1, 2, 3), Test().toList())


yieldfun .

context
, , . it :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) it.yield(i); }
.
.. Extension function literals - body , body . YieldContext :
public fun yieldfun<T: Any>(body: YieldContext<T>.() -> Unit): Iterable<T> = YieldIterable<T>(body)

body .
? :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) yield(i); }
.. yieldfun YieldContext, YieldContext this :
this.yield(i)

, this ,
yield(i)
.. body , :
continuationSync.take() body() resultQueue.put(CompletedMessage())
, YieldIterator YieldContext, body - extension-.


. - yield :)

YieldContext ret :
public trait YieldContext<T> { fun yield(value: T): Unit val ret: YieldContext<T> }

YieldIterator this :
override val ret: YieldContext<T> = this
? :
ret.yield(i)
, yield(T) this , :
ret yield i
, .

, C# Kotlin:
fun fibonacci(): Iterable<Int> = yieldfun { var a = 0 var b = 1 while (true) { val sum = a + b; ret yield sum; a = b; b = sum; } }
, , generic- yieldfun - .


, , . . finalize() :
protected fun finalize() { thread?.interrupt() }
catch :
try { continuationSync.take() body() resultQueue.put(CompletedMessage()) } catch (e: InterruptedException) { // if not all items were iterated yield will wait for signal, so finalizer should // interrupt the thread to exit } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) }
, continuationSync , .
, , , yieldfun , InterruptedException , yield try-catch , .


, extension- .Net, .. , "" . , , Jetbrains. , Kotlin .

UPD. bitbucket.org/Nerzhul500/kyield/src
Iterable. yieldfun . :
fun Test() = yieldfun<Int> { (context) -> for (val i in 0 .. 3) context.yield(i); }

context - , yield.

yieldfun , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> { return YieldIterable<T>(body) }
YieldIterable body - -. , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> = YieldIterable<T>(body)


Iterable , Iterator, YieldIterable :
private class YieldIterable<T>(val body: (YieldContext<T>) -> Unit): Iterable<T>{ public override fun iterator(): Iterator<T> = YieldIterator<T>(body) }


body ( trait) YieldContext, :
trait YieldContext<T> { fun yield(value : T) }

, yield(T) .

yield - YieldIterator:
private class YieldIterator<T> (val body: (YieldContext<T>) -> Unit): Iterator<T>, YieldContext<T> { private var thread: Thread? = null private val resultQueue = SynchronousQueue<Message>() private val continuationSync = SynchronousQueue<Any>() private var currentMessage: Message? = null { val r = object : Runnable { public override fun run() { try { continuationSync.take() body(this@YieldIterator) resultQueue.put(CompletedMessage()) } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) } } } thread = Thread(r) thread!!.start() } override fun yield(value: T) { resultQueue.put(ValueMessage(value)) continuationSync.take() } public override fun next(): T { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception if (currentMessage !is ValueMessage) throw NoSuchElementException() val value = (currentMessage as ValueMessage).value as T currentMessage = null return value } public override fun hasNext(): Boolean { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception return currentMessage is ValueMessage } private val dummy = Any() private inline fun evaluateNext() { if (currentMessage == null) { continuationSync.put(dummy) currentMessage = resultQueue.take() } } }

, , :
, . , - , put take , ( ).

resultQueue thread , - continuationSync - , .

, continuationSync . :
1) next() hasNext() , , (. evaluateNext()), .
2) body, .. .
3) - yield(T), ( ) , . , .
4) body , , .

, :
private open class Message { } private class ValueMessage(public val value: Any): Message() { } private class CompletedMessage: Message() { } private class ExceptionThrownMessage(public val exception: Throwable): Message() { }

, Test() :
assertEquals(listOf(0, 1, 2, 3), Test().toList())


yieldfun .

context
, , . it :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) it.yield(i); }
.
.. Extension function literals - body , body . YieldContext :
public fun yieldfun<T: Any>(body: YieldContext<T>.() -> Unit): Iterable<T> = YieldIterable<T>(body)

body .
? :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) yield(i); }
.. yieldfun YieldContext, YieldContext this :
this.yield(i)

, this ,
yield(i)
.. body , :
continuationSync.take() body() resultQueue.put(CompletedMessage())
, YieldIterator YieldContext, body - extension-.


. - yield :)

YieldContext ret :
public trait YieldContext<T> { fun yield(value: T): Unit val ret: YieldContext<T> }

YieldIterator this :
override val ret: YieldContext<T> = this
? :
ret.yield(i)
, yield(T) this , :
ret yield i
, .

, C# Kotlin:
fun fibonacci(): Iterable<Int> = yieldfun { var a = 0 var b = 1 while (true) { val sum = a + b; ret yield sum; a = b; b = sum; } }
, , generic- yieldfun - .


, , . . finalize() :
protected fun finalize() { thread?.interrupt() }
catch :
try { continuationSync.take() body() resultQueue.put(CompletedMessage()) } catch (e: InterruptedException) { // if not all items were iterated yield will wait for signal, so finalizer should // interrupt the thread to exit } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) }
, continuationSync , .
, , , yieldfun , InterruptedException , yield try-catch , .


, extension- .Net, .. , "" . , , Jetbrains. , Kotlin .

UPD. bitbucket.org/Nerzhul500/kyield/src
 Iterable.   yieldfun .       : 
fun Test() = yieldfun<Int> { (context) -> for (val i in 0 .. 3) context.yield(i); }

context - , yield.

yieldfun , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> { return YieldIterable<T>(body) }
YieldIterable body - -. , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> = YieldIterable<T>(body)


Iterable , Iterator, YieldIterable :
private class YieldIterable<T>(val body: (YieldContext<T>) -> Unit): Iterable<T>{ public override fun iterator(): Iterator<T> = YieldIterator<T>(body) }


body ( trait) YieldContext, :
trait YieldContext<T> { fun yield(value : T) }

, yield(T) .

yield - YieldIterator:
private class YieldIterator<T> (val body: (YieldContext<T>) -> Unit): Iterator<T>, YieldContext<T> { private var thread: Thread? = null private val resultQueue = SynchronousQueue<Message>() private val continuationSync = SynchronousQueue<Any>() private var currentMessage: Message? = null { val r = object : Runnable { public override fun run() { try { continuationSync.take() body(this@YieldIterator) resultQueue.put(CompletedMessage()) } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) } } } thread = Thread(r) thread!!.start() } override fun yield(value: T) { resultQueue.put(ValueMessage(value)) continuationSync.take() } public override fun next(): T { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception if (currentMessage !is ValueMessage) throw NoSuchElementException() val value = (currentMessage as ValueMessage).value as T currentMessage = null return value } public override fun hasNext(): Boolean { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception return currentMessage is ValueMessage } private val dummy = Any() private inline fun evaluateNext() { if (currentMessage == null) { continuationSync.put(dummy) currentMessage = resultQueue.take() } } }

, , :
, . , - , put take , ( ).

resultQueue thread , - continuationSync - , .

, continuationSync . :
1) next() hasNext() , , (. evaluateNext()), .
2) body, .. .
3) - yield(T), ( ) , . , .
4) body , , .

, :
private open class Message { } private class ValueMessage(public val value: Any): Message() { } private class CompletedMessage: Message() { } private class ExceptionThrownMessage(public val exception: Throwable): Message() { }

, Test() :
assertEquals(listOf(0, 1, 2, 3), Test().toList())


yieldfun .

context
, , . it :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) it.yield(i); }
.
.. Extension function literals - body , body . YieldContext :
public fun yieldfun<T: Any>(body: YieldContext<T>.() -> Unit): Iterable<T> = YieldIterable<T>(body)

body .
? :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) yield(i); }
.. yieldfun YieldContext, YieldContext this :
this.yield(i)

, this ,
yield(i)
.. body , :
continuationSync.take() body() resultQueue.put(CompletedMessage())
, YieldIterator YieldContext, body - extension-.


. - yield :)

YieldContext ret :
public trait YieldContext<T> { fun yield(value: T): Unit val ret: YieldContext<T> }

YieldIterator this :
override val ret: YieldContext<T> = this
? :
ret.yield(i)
, yield(T) this , :
ret yield i
, .

, C# Kotlin:
fun fibonacci(): Iterable<Int> = yieldfun { var a = 0 var b = 1 while (true) { val sum = a + b; ret yield sum; a = b; b = sum; } }
, , generic- yieldfun - .


, , . . finalize() :
protected fun finalize() { thread?.interrupt() }
catch :
try { continuationSync.take() body() resultQueue.put(CompletedMessage()) } catch (e: InterruptedException) { // if not all items were iterated yield will wait for signal, so finalizer should // interrupt the thread to exit } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) }
, continuationSync , .
, , , yieldfun , InterruptedException , yield try-catch , .


, extension- .Net, .. , "" . , , Jetbrains. , Kotlin .

UPD. bitbucket.org/Nerzhul500/kyield/src
Iterable. yieldfun . :
fun Test() = yieldfun<Int> { (context) -> for (val i in 0 .. 3) context.yield(i); }

context - , yield.

yieldfun , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> { return YieldIterable<T>(body) }
YieldIterable body - -. , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> = YieldIterable<T>(body)


Iterable , Iterator, YieldIterable :
private class YieldIterable<T>(val body: (YieldContext<T>) -> Unit): Iterable<T>{ public override fun iterator(): Iterator<T> = YieldIterator<T>(body) }


body ( trait) YieldContext, :
trait YieldContext<T> { fun yield(value : T) }

, yield(T) .

yield - YieldIterator:
private class YieldIterator<T> (val body: (YieldContext<T>) -> Unit): Iterator<T>, YieldContext<T> { private var thread: Thread? = null private val resultQueue = SynchronousQueue<Message>() private val continuationSync = SynchronousQueue<Any>() private var currentMessage: Message? = null { val r = object : Runnable { public override fun run() { try { continuationSync.take() body(this@YieldIterator) resultQueue.put(CompletedMessage()) } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) } } } thread = Thread(r) thread!!.start() } override fun yield(value: T) { resultQueue.put(ValueMessage(value)) continuationSync.take() } public override fun next(): T { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception if (currentMessage !is ValueMessage) throw NoSuchElementException() val value = (currentMessage as ValueMessage).value as T currentMessage = null return value } public override fun hasNext(): Boolean { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception return currentMessage is ValueMessage } private val dummy = Any() private inline fun evaluateNext() { if (currentMessage == null) { continuationSync.put(dummy) currentMessage = resultQueue.take() } } }

, , :
, . , - , put take , ( ).

resultQueue thread , - continuationSync - , .

, continuationSync . :
1) next() hasNext() , , (. evaluateNext()), .
2) body, .. .
3) - yield(T), ( ) , . , .
4) body , , .

, :
private open class Message { } private class ValueMessage(public val value: Any): Message() { } private class CompletedMessage: Message() { } private class ExceptionThrownMessage(public val exception: Throwable): Message() { }

, Test() :
assertEquals(listOf(0, 1, 2, 3), Test().toList())


yieldfun .

context
, , . it :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) it.yield(i); }
.
.. Extension function literals - body , body . YieldContext :
public fun yieldfun<T: Any>(body: YieldContext<T>.() -> Unit): Iterable<T> = YieldIterable<T>(body)

body .
? :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) yield(i); }
.. yieldfun YieldContext, YieldContext this :
this.yield(i)

, this ,
yield(i)
.. body , :
continuationSync.take() body() resultQueue.put(CompletedMessage())
, YieldIterator YieldContext, body - extension-.


. - yield :)

YieldContext ret :
public trait YieldContext<T> { fun yield(value: T): Unit val ret: YieldContext<T> }

YieldIterator this :
override val ret: YieldContext<T> = this
? :
ret.yield(i)
, yield(T) this , :
ret yield i
, .

, C# Kotlin:
fun fibonacci(): Iterable<Int> = yieldfun { var a = 0 var b = 1 while (true) { val sum = a + b; ret yield sum; a = b; b = sum; } }
, , generic- yieldfun - .


, , . . finalize() :
protected fun finalize() { thread?.interrupt() }
catch :
try { continuationSync.take() body() resultQueue.put(CompletedMessage()) } catch (e: InterruptedException) { // if not all items were iterated yield will wait for signal, so finalizer should // interrupt the thread to exit } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) }
, continuationSync , .
, , , yieldfun , InterruptedException , yield try-catch , .


, extension- .Net, .. , "" . , , Jetbrains. , Kotlin .

UPD. bitbucket.org/Nerzhul500/kyield/src

Iterable. yieldfun . :
fun Test() = yieldfun<Int> { (context) -> for (val i in 0 .. 3) context.yield(i); }

context - , yield.

yieldfun , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> { return YieldIterable<T>(body) }
YieldIterable body - -. , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> = YieldIterable<T>(body)


Iterable , Iterator, YieldIterable :
private class YieldIterable<T>(val body: (YieldContext<T>) -> Unit): Iterable<T>{ public override fun iterator(): Iterator<T> = YieldIterator<T>(body) }


body ( trait) YieldContext, :
trait YieldContext<T> { fun yield(value : T) }

, yield(T) .

yield - YieldIterator:
private class YieldIterator<T> (val body: (YieldContext<T>) -> Unit): Iterator<T>, YieldContext<T> { private var thread: Thread? = null private val resultQueue = SynchronousQueue<Message>() private val continuationSync = SynchronousQueue<Any>() private var currentMessage: Message? = null { val r = object : Runnable { public override fun run() { try { continuationSync.take() body(this@YieldIterator) resultQueue.put(CompletedMessage()) } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) } } } thread = Thread(r) thread!!.start() } override fun yield(value: T) { resultQueue.put(ValueMessage(value)) continuationSync.take() } public override fun next(): T { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception if (currentMessage !is ValueMessage) throw NoSuchElementException() val value = (currentMessage as ValueMessage).value as T currentMessage = null return value } public override fun hasNext(): Boolean { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception return currentMessage is ValueMessage } private val dummy = Any() private inline fun evaluateNext() { if (currentMessage == null) { continuationSync.put(dummy) currentMessage = resultQueue.take() } } }

, , :
, . , - , put take , ( ).

resultQueue thread , - continuationSync - , .

, continuationSync . :
1) next() hasNext() , , (. evaluateNext()), .
2) body, .. .
3) - yield(T), ( ) , . , .
4) body , , .

, :
private open class Message { } private class ValueMessage(public val value: Any): Message() { } private class CompletedMessage: Message() { } private class ExceptionThrownMessage(public val exception: Throwable): Message() { }

, Test() :
assertEquals(listOf(0, 1, 2, 3), Test().toList())


yieldfun .

context
, , . it :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) it.yield(i); }
.
.. Extension function literals - body , body . YieldContext :
public fun yieldfun<T: Any>(body: YieldContext<T>.() -> Unit): Iterable<T> = YieldIterable<T>(body)

body .
? :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) yield(i); }
.. yieldfun YieldContext, YieldContext this :
this.yield(i)

, this ,
yield(i)
.. body , :
continuationSync.take() body() resultQueue.put(CompletedMessage())
, YieldIterator YieldContext, body - extension-.


. - yield :)

YieldContext ret :
public trait YieldContext<T> { fun yield(value: T): Unit val ret: YieldContext<T> }

YieldIterator this :
override val ret: YieldContext<T> = this
? :
ret.yield(i)
, yield(T) this , :
ret yield i
, .

, C# Kotlin:
fun fibonacci(): Iterable<Int> = yieldfun { var a = 0 var b = 1 while (true) { val sum = a + b; ret yield sum; a = b; b = sum; } }
, , generic- yieldfun - .


, , . . finalize() :
protected fun finalize() { thread?.interrupt() }
catch :
try { continuationSync.take() body() resultQueue.put(CompletedMessage()) } catch (e: InterruptedException) { // if not all items were iterated yield will wait for signal, so finalizer should // interrupt the thread to exit } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) }
, continuationSync , .
, , , yieldfun , InterruptedException , yield try-catch , .


, extension- .Net, .. , "" . , , Jetbrains. , Kotlin .

UPD. bitbucket.org/Nerzhul500/kyield/src

Iterable. yieldfun . :
fun Test() = yieldfun<Int> { (context) -> for (val i in 0 .. 3) context.yield(i); }

context - , yield.

yieldfun , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> { return YieldIterable<T>(body) }
YieldIterable body - -. , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> = YieldIterable<T>(body)


Iterable , Iterator, YieldIterable :
private class YieldIterable<T>(val body: (YieldContext<T>) -> Unit): Iterable<T>{ public override fun iterator(): Iterator<T> = YieldIterator<T>(body) }


body ( trait) YieldContext, :
trait YieldContext<T> { fun yield(value : T) }

, yield(T) .

yield - YieldIterator:
private class YieldIterator<T> (val body: (YieldContext<T>) -> Unit): Iterator<T>, YieldContext<T> { private var thread: Thread? = null private val resultQueue = SynchronousQueue<Message>() private val continuationSync = SynchronousQueue<Any>() private var currentMessage: Message? = null { val r = object : Runnable { public override fun run() { try { continuationSync.take() body(this@YieldIterator) resultQueue.put(CompletedMessage()) } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) } } } thread = Thread(r) thread!!.start() } override fun yield(value: T) { resultQueue.put(ValueMessage(value)) continuationSync.take() } public override fun next(): T { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception if (currentMessage !is ValueMessage) throw NoSuchElementException() val value = (currentMessage as ValueMessage).value as T currentMessage = null return value } public override fun hasNext(): Boolean { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception return currentMessage is ValueMessage } private val dummy = Any() private inline fun evaluateNext() { if (currentMessage == null) { continuationSync.put(dummy) currentMessage = resultQueue.take() } } }

, , :
, . , - , put take , ( ).

resultQueue thread , - continuationSync - , .

, continuationSync . :
1) next() hasNext() , , (. evaluateNext()), .
2) body, .. .
3) - yield(T), ( ) , . , .
4) body , , .

, :
private open class Message { } private class ValueMessage(public val value: Any): Message() { } private class CompletedMessage: Message() { } private class ExceptionThrownMessage(public val exception: Throwable): Message() { }

, Test() :
assertEquals(listOf(0, 1, 2, 3), Test().toList())


yieldfun .

context
, , . it :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) it.yield(i); }
.
.. Extension function literals - body , body . YieldContext :
public fun yieldfun<T: Any>(body: YieldContext<T>.() -> Unit): Iterable<T> = YieldIterable<T>(body)

body .
? :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) yield(i); }
.. yieldfun YieldContext, YieldContext this :
this.yield(i)

, this ,
yield(i)
.. body , :
continuationSync.take() body() resultQueue.put(CompletedMessage())
, YieldIterator YieldContext, body - extension-.


. - yield :)

YieldContext ret :
public trait YieldContext<T> { fun yield(value: T): Unit val ret: YieldContext<T> }

YieldIterator this :
override val ret: YieldContext<T> = this
? :
ret.yield(i)
, yield(T) this , :
ret yield i
, .

, C# Kotlin:
fun fibonacci(): Iterable<Int> = yieldfun { var a = 0 var b = 1 while (true) { val sum = a + b; ret yield sum; a = b; b = sum; } }
, , generic- yieldfun - .


, , . . finalize() :
protected fun finalize() { thread?.interrupt() }
catch :
try { continuationSync.take() body() resultQueue.put(CompletedMessage()) } catch (e: InterruptedException) { // if not all items were iterated yield will wait for signal, so finalizer should // interrupt the thread to exit } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) }
, continuationSync , .
, , , yieldfun , InterruptedException , yield try-catch , .


, extension- .Net, .. , "" . , , Jetbrains. , Kotlin .

UPD. bitbucket.org/Nerzhul500/kyield/src
 Iterable.   yieldfun .       : 
fun Test() = yieldfun<Int> { (context) -> for (val i in 0 .. 3) context.yield(i); }

context - , yield.

yieldfun , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> { return YieldIterable<T>(body) }
YieldIterable body - -. , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> = YieldIterable<T>(body)


Iterable , Iterator, YieldIterable :
private class YieldIterable<T>(val body: (YieldContext<T>) -> Unit): Iterable<T>{ public override fun iterator(): Iterator<T> = YieldIterator<T>(body) }


body ( trait) YieldContext, :
trait YieldContext<T> { fun yield(value : T) }

, yield(T) .

yield - YieldIterator:
private class YieldIterator<T> (val body: (YieldContext<T>) -> Unit): Iterator<T>, YieldContext<T> { private var thread: Thread? = null private val resultQueue = SynchronousQueue<Message>() private val continuationSync = SynchronousQueue<Any>() private var currentMessage: Message? = null { val r = object : Runnable { public override fun run() { try { continuationSync.take() body(this@YieldIterator) resultQueue.put(CompletedMessage()) } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) } } } thread = Thread(r) thread!!.start() } override fun yield(value: T) { resultQueue.put(ValueMessage(value)) continuationSync.take() } public override fun next(): T { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception if (currentMessage !is ValueMessage) throw NoSuchElementException() val value = (currentMessage as ValueMessage).value as T currentMessage = null return value } public override fun hasNext(): Boolean { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception return currentMessage is ValueMessage } private val dummy = Any() private inline fun evaluateNext() { if (currentMessage == null) { continuationSync.put(dummy) currentMessage = resultQueue.take() } } }

, , :
, . , - , put take , ( ).

resultQueue thread , - continuationSync - , .

, continuationSync . :
1) next() hasNext() , , (. evaluateNext()), .
2) body, .. .
3) - yield(T), ( ) , . , .
4) body , , .

, :
private open class Message { } private class ValueMessage(public val value: Any): Message() { } private class CompletedMessage: Message() { } private class ExceptionThrownMessage(public val exception: Throwable): Message() { }

, Test() :
assertEquals(listOf(0, 1, 2, 3), Test().toList())


yieldfun .

context
, , . it :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) it.yield(i); }
.
.. Extension function literals - body , body . YieldContext :
public fun yieldfun<T: Any>(body: YieldContext<T>.() -> Unit): Iterable<T> = YieldIterable<T>(body)

body .
? :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) yield(i); }
.. yieldfun YieldContext, YieldContext this :
this.yield(i)

, this ,
yield(i)
.. body , :
continuationSync.take() body() resultQueue.put(CompletedMessage())
, YieldIterator YieldContext, body - extension-.


. - yield :)

YieldContext ret :
public trait YieldContext<T> { fun yield(value: T): Unit val ret: YieldContext<T> }

YieldIterator this :
override val ret: YieldContext<T> = this
? :
ret.yield(i)
, yield(T) this , :
ret yield i
, .

, C# Kotlin:
fun fibonacci(): Iterable<Int> = yieldfun { var a = 0 var b = 1 while (true) { val sum = a + b; ret yield sum; a = b; b = sum; } }
, , generic- yieldfun - .


, , . . finalize() :
protected fun finalize() { thread?.interrupt() }
catch :
try { continuationSync.take() body() resultQueue.put(CompletedMessage()) } catch (e: InterruptedException) { // if not all items were iterated yield will wait for signal, so finalizer should // interrupt the thread to exit } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) }
, continuationSync , .
, , , yieldfun , InterruptedException , yield try-catch , .


, extension- .Net, .. , "" . , , Jetbrains. , Kotlin .

UPD. bitbucket.org/Nerzhul500/kyield/src
Iterable. yieldfun . :
fun Test() = yieldfun<Int> { (context) -> for (val i in 0 .. 3) context.yield(i); }

context - , yield.

yieldfun , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> { return YieldIterable<T>(body) }
YieldIterable body - -. , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> = YieldIterable<T>(body)


Iterable , Iterator, YieldIterable :
private class YieldIterable<T>(val body: (YieldContext<T>) -> Unit): Iterable<T>{ public override fun iterator(): Iterator<T> = YieldIterator<T>(body) }


body ( trait) YieldContext, :
trait YieldContext<T> { fun yield(value : T) }

, yield(T) .

yield - YieldIterator:
private class YieldIterator<T> (val body: (YieldContext<T>) -> Unit): Iterator<T>, YieldContext<T> { private var thread: Thread? = null private val resultQueue = SynchronousQueue<Message>() private val continuationSync = SynchronousQueue<Any>() private var currentMessage: Message? = null { val r = object : Runnable { public override fun run() { try { continuationSync.take() body(this@YieldIterator) resultQueue.put(CompletedMessage()) } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) } } } thread = Thread(r) thread!!.start() } override fun yield(value: T) { resultQueue.put(ValueMessage(value)) continuationSync.take() } public override fun next(): T { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception if (currentMessage !is ValueMessage) throw NoSuchElementException() val value = (currentMessage as ValueMessage).value as T currentMessage = null return value } public override fun hasNext(): Boolean { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception return currentMessage is ValueMessage } private val dummy = Any() private inline fun evaluateNext() { if (currentMessage == null) { continuationSync.put(dummy) currentMessage = resultQueue.take() } } }

, , :
, . , - , put take , ( ).

resultQueue thread , - continuationSync - , .

, continuationSync . :
1) next() hasNext() , , (. evaluateNext()), .
2) body, .. .
3) - yield(T), ( ) , . , .
4) body , , .

, :
private open class Message { } private class ValueMessage(public val value: Any): Message() { } private class CompletedMessage: Message() { } private class ExceptionThrownMessage(public val exception: Throwable): Message() { }

, Test() :
assertEquals(listOf(0, 1, 2, 3), Test().toList())


yieldfun .

context
, , . it :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) it.yield(i); }
.
.. Extension function literals - body , body . YieldContext :
public fun yieldfun<T: Any>(body: YieldContext<T>.() -> Unit): Iterable<T> = YieldIterable<T>(body)

body .
? :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) yield(i); }
.. yieldfun YieldContext, YieldContext this :
this.yield(i)

, this ,
yield(i)
.. body , :
continuationSync.take() body() resultQueue.put(CompletedMessage())
, YieldIterator YieldContext, body - extension-.


. - yield :)

YieldContext ret :
public trait YieldContext<T> { fun yield(value: T): Unit val ret: YieldContext<T> }

YieldIterator this :
override val ret: YieldContext<T> = this
? :
ret.yield(i)
, yield(T) this , :
ret yield i
, .

, C# Kotlin:
fun fibonacci(): Iterable<Int> = yieldfun { var a = 0 var b = 1 while (true) { val sum = a + b; ret yield sum; a = b; b = sum; } }
, , generic- yieldfun - .


, , . . finalize() :
protected fun finalize() { thread?.interrupt() }
catch :
try { continuationSync.take() body() resultQueue.put(CompletedMessage()) } catch (e: InterruptedException) { // if not all items were iterated yield will wait for signal, so finalizer should // interrupt the thread to exit } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) }
, continuationSync , .
, , , yieldfun , InterruptedException , yield try-catch , .


, extension- .Net, .. , "" . , , Jetbrains. , Kotlin .

UPD. bitbucket.org/Nerzhul500/kyield/src
 Iterable.   yieldfun .       : 
fun Test() = yieldfun<Int> { (context) -> for (val i in 0 .. 3) context.yield(i); }

context - , yield.

yieldfun , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> { return YieldIterable<T>(body) }
YieldIterable body - -. , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> = YieldIterable<T>(body)


Iterable , Iterator, YieldIterable :
private class YieldIterable<T>(val body: (YieldContext<T>) -> Unit): Iterable<T>{ public override fun iterator(): Iterator<T> = YieldIterator<T>(body) }


body ( trait) YieldContext, :
trait YieldContext<T> { fun yield(value : T) }

, yield(T) .

yield - YieldIterator:
private class YieldIterator<T> (val body: (YieldContext<T>) -> Unit): Iterator<T>, YieldContext<T> { private var thread: Thread? = null private val resultQueue = SynchronousQueue<Message>() private val continuationSync = SynchronousQueue<Any>() private var currentMessage: Message? = null { val r = object : Runnable { public override fun run() { try { continuationSync.take() body(this@YieldIterator) resultQueue.put(CompletedMessage()) } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) } } } thread = Thread(r) thread!!.start() } override fun yield(value: T) { resultQueue.put(ValueMessage(value)) continuationSync.take() } public override fun next(): T { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception if (currentMessage !is ValueMessage) throw NoSuchElementException() val value = (currentMessage as ValueMessage).value as T currentMessage = null return value } public override fun hasNext(): Boolean { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception return currentMessage is ValueMessage } private val dummy = Any() private inline fun evaluateNext() { if (currentMessage == null) { continuationSync.put(dummy) currentMessage = resultQueue.take() } } }

, , :
, . , - , put take , ( ).

resultQueue thread , - continuationSync - , .

, continuationSync . :
1) next() hasNext() , , (. evaluateNext()), .
2) body, .. .
3) - yield(T), ( ) , . , .
4) body , , .

, :
private open class Message { } private class ValueMessage(public val value: Any): Message() { } private class CompletedMessage: Message() { } private class ExceptionThrownMessage(public val exception: Throwable): Message() { }

, Test() :
assertEquals(listOf(0, 1, 2, 3), Test().toList())


yieldfun .

context
, , . it :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) it.yield(i); }
.
.. Extension function literals - body , body . YieldContext :
public fun yieldfun<T: Any>(body: YieldContext<T>.() -> Unit): Iterable<T> = YieldIterable<T>(body)

body .
? :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) yield(i); }
.. yieldfun YieldContext, YieldContext this :
this.yield(i)

, this ,
yield(i)
.. body , :
continuationSync.take() body() resultQueue.put(CompletedMessage())
, YieldIterator YieldContext, body - extension-.


. - yield :)

YieldContext ret :
public trait YieldContext<T> { fun yield(value: T): Unit val ret: YieldContext<T> }

YieldIterator this :
override val ret: YieldContext<T> = this
? :
ret.yield(i)
, yield(T) this , :
ret yield i
, .

, C# Kotlin:
fun fibonacci(): Iterable<Int> = yieldfun { var a = 0 var b = 1 while (true) { val sum = a + b; ret yield sum; a = b; b = sum; } }
, , generic- yieldfun - .


, , . . finalize() :
protected fun finalize() { thread?.interrupt() }
catch :
try { continuationSync.take() body() resultQueue.put(CompletedMessage()) } catch (e: InterruptedException) { // if not all items were iterated yield will wait for signal, so finalizer should // interrupt the thread to exit } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) }
, continuationSync , .
, , , yieldfun , InterruptedException , yield try-catch , .


, extension- .Net, .. , "" . , , Jetbrains. , Kotlin .

UPD. bitbucket.org/Nerzhul500/kyield/src
Iterable. yieldfun . :
fun Test() = yieldfun<Int> { (context) -> for (val i in 0 .. 3) context.yield(i); }

context - , yield.

yieldfun , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> { return YieldIterable<T>(body) }
YieldIterable body - -. , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> = YieldIterable<T>(body)


Iterable , Iterator, YieldIterable :
private class YieldIterable<T>(val body: (YieldContext<T>) -> Unit): Iterable<T>{ public override fun iterator(): Iterator<T> = YieldIterator<T>(body) }


body ( trait) YieldContext, :
trait YieldContext<T> { fun yield(value : T) }

, yield(T) .

yield - YieldIterator:
private class YieldIterator<T> (val body: (YieldContext<T>) -> Unit): Iterator<T>, YieldContext<T> { private var thread: Thread? = null private val resultQueue = SynchronousQueue<Message>() private val continuationSync = SynchronousQueue<Any>() private var currentMessage: Message? = null { val r = object : Runnable { public override fun run() { try { continuationSync.take() body(this@YieldIterator) resultQueue.put(CompletedMessage()) } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) } } } thread = Thread(r) thread!!.start() } override fun yield(value: T) { resultQueue.put(ValueMessage(value)) continuationSync.take() } public override fun next(): T { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception if (currentMessage !is ValueMessage) throw NoSuchElementException() val value = (currentMessage as ValueMessage).value as T currentMessage = null return value } public override fun hasNext(): Boolean { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception return currentMessage is ValueMessage } private val dummy = Any() private inline fun evaluateNext() { if (currentMessage == null) { continuationSync.put(dummy) currentMessage = resultQueue.take() } } }

, , :
, . , - , put take , ( ).

resultQueue thread , - continuationSync - , .

, continuationSync . :
1) next() hasNext() , , (. evaluateNext()), .
2) body, .. .
3) - yield(T), ( ) , . , .
4) body , , .

, :
private open class Message { } private class ValueMessage(public val value: Any): Message() { } private class CompletedMessage: Message() { } private class ExceptionThrownMessage(public val exception: Throwable): Message() { }

, Test() :
assertEquals(listOf(0, 1, 2, 3), Test().toList())


yieldfun .

context
, , . it :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) it.yield(i); }
.
.. Extension function literals - body , body . YieldContext :
public fun yieldfun<T: Any>(body: YieldContext<T>.() -> Unit): Iterable<T> = YieldIterable<T>(body)

body .
? :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) yield(i); }
.. yieldfun YieldContext, YieldContext this :
this.yield(i)

, this ,
yield(i)
.. body , :
continuationSync.take() body() resultQueue.put(CompletedMessage())
, YieldIterator YieldContext, body - extension-.


. - yield :)

YieldContext ret :
public trait YieldContext<T> { fun yield(value: T): Unit val ret: YieldContext<T> }

YieldIterator this :
override val ret: YieldContext<T> = this
? :
ret.yield(i)
, yield(T) this , :
ret yield i
, .

, C# Kotlin:
fun fibonacci(): Iterable<Int> = yieldfun { var a = 0 var b = 1 while (true) { val sum = a + b; ret yield sum; a = b; b = sum; } }
, , generic- yieldfun - .


, , . . finalize() :
protected fun finalize() { thread?.interrupt() }
catch :
try { continuationSync.take() body() resultQueue.put(CompletedMessage()) } catch (e: InterruptedException) { // if not all items were iterated yield will wait for signal, so finalizer should // interrupt the thread to exit } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) }
, continuationSync , .
, , , yieldfun , InterruptedException , yield try-catch , .


, extension- .Net, .. , "" . , , Jetbrains. , Kotlin .

UPD. bitbucket.org/Nerzhul500/kyield/src

Iterable. yieldfun . :
fun Test() = yieldfun<Int> { (context) -> for (val i in 0 .. 3) context.yield(i); }

context - , yield.

yieldfun , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> { return YieldIterable<T>(body) }
YieldIterable body - -. , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> = YieldIterable<T>(body)


Iterable , Iterator, YieldIterable :
private class YieldIterable<T>(val body: (YieldContext<T>) -> Unit): Iterable<T>{ public override fun iterator(): Iterator<T> = YieldIterator<T>(body) }


body ( trait) YieldContext, :
trait YieldContext<T> { fun yield(value : T) }

, yield(T) .

yield - YieldIterator:
private class YieldIterator<T> (val body: (YieldContext<T>) -> Unit): Iterator<T>, YieldContext<T> { private var thread: Thread? = null private val resultQueue = SynchronousQueue<Message>() private val continuationSync = SynchronousQueue<Any>() private var currentMessage: Message? = null { val r = object : Runnable { public override fun run() { try { continuationSync.take() body(this@YieldIterator) resultQueue.put(CompletedMessage()) } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) } } } thread = Thread(r) thread!!.start() } override fun yield(value: T) { resultQueue.put(ValueMessage(value)) continuationSync.take() } public override fun next(): T { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception if (currentMessage !is ValueMessage) throw NoSuchElementException() val value = (currentMessage as ValueMessage).value as T currentMessage = null return value } public override fun hasNext(): Boolean { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception return currentMessage is ValueMessage } private val dummy = Any() private inline fun evaluateNext() { if (currentMessage == null) { continuationSync.put(dummy) currentMessage = resultQueue.take() } } }

, , :
, . , - , put take , ( ).

resultQueue thread , - continuationSync - , .

, continuationSync . :
1) next() hasNext() , , (. evaluateNext()), .
2) body, .. .
3) - yield(T), ( ) , . , .
4) body , , .

, :
private open class Message { } private class ValueMessage(public val value: Any): Message() { } private class CompletedMessage: Message() { } private class ExceptionThrownMessage(public val exception: Throwable): Message() { }

, Test() :
assertEquals(listOf(0, 1, 2, 3), Test().toList())


yieldfun .

context
, , . it :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) it.yield(i); }
.
.. Extension function literals - body , body . YieldContext :
public fun yieldfun<T: Any>(body: YieldContext<T>.() -> Unit): Iterable<T> = YieldIterable<T>(body)

body .
? :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) yield(i); }
.. yieldfun YieldContext, YieldContext this :
this.yield(i)

, this ,
yield(i)
.. body , :
continuationSync.take() body() resultQueue.put(CompletedMessage())
, YieldIterator YieldContext, body - extension-.


. - yield :)

YieldContext ret :
public trait YieldContext<T> { fun yield(value: T): Unit val ret: YieldContext<T> }

YieldIterator this :
override val ret: YieldContext<T> = this
? :
ret.yield(i)
, yield(T) this , :
ret yield i
, .

, C# Kotlin:
fun fibonacci(): Iterable<Int> = yieldfun { var a = 0 var b = 1 while (true) { val sum = a + b; ret yield sum; a = b; b = sum; } }
, , generic- yieldfun - .


, , . . finalize() :
protected fun finalize() { thread?.interrupt() }
catch :
try { continuationSync.take() body() resultQueue.put(CompletedMessage()) } catch (e: InterruptedException) { // if not all items were iterated yield will wait for signal, so finalizer should // interrupt the thread to exit } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) }
, continuationSync , .
, , , yieldfun , InterruptedException , yield try-catch , .


, extension- .Net, .. , "" . , , Jetbrains. , Kotlin .

UPD. bitbucket.org/Nerzhul500/kyield/src

Iterable. yieldfun . :
fun Test() = yieldfun<Int> { (context) -> for (val i in 0 .. 3) context.yield(i); }

context - , yield.

yieldfun , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> { return YieldIterable<T>(body) }
YieldIterable body - -. , :
fun yieldfun<T> (body : (YieldContext<T>) -> Unit) : Iterable<T> = YieldIterable<T>(body)


Iterable , Iterator, YieldIterable :
private class YieldIterable<T>(val body: (YieldContext<T>) -> Unit): Iterable<T>{ public override fun iterator(): Iterator<T> = YieldIterator<T>(body) }


body ( trait) YieldContext, :
trait YieldContext<T> { fun yield(value : T) }

, yield(T) .

yield - YieldIterator:
private class YieldIterator<T> (val body: (YieldContext<T>) -> Unit): Iterator<T>, YieldContext<T> { private var thread: Thread? = null private val resultQueue = SynchronousQueue<Message>() private val continuationSync = SynchronousQueue<Any>() private var currentMessage: Message? = null { val r = object : Runnable { public override fun run() { try { continuationSync.take() body(this@YieldIterator) resultQueue.put(CompletedMessage()) } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) } } } thread = Thread(r) thread!!.start() } override fun yield(value: T) { resultQueue.put(ValueMessage(value)) continuationSync.take() } public override fun next(): T { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception if (currentMessage !is ValueMessage) throw NoSuchElementException() val value = (currentMessage as ValueMessage).value as T currentMessage = null return value } public override fun hasNext(): Boolean { evaluateNext() if (currentMessage is ExceptionThrownMessage) throw (currentMessage as ExceptionThrownMessage).exception return currentMessage is ValueMessage } private val dummy = Any() private inline fun evaluateNext() { if (currentMessage == null) { continuationSync.put(dummy) currentMessage = resultQueue.take() } } }

, , :
, . , - , put take , ( ).

resultQueue thread , - continuationSync - , .

, continuationSync . :
1) next() hasNext() , , (. evaluateNext()), .
2) body, .. .
3) - yield(T), ( ) , . , .
4) body , , .

, :
private open class Message { } private class ValueMessage(public val value: Any): Message() { } private class CompletedMessage: Message() { } private class ExceptionThrownMessage(public val exception: Throwable): Message() { }

, Test() :
assertEquals(listOf(0, 1, 2, 3), Test().toList())


yieldfun .

context
, , . it :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) it.yield(i); }
.
.. Extension function literals - body , body . YieldContext :
public fun yieldfun<T: Any>(body: YieldContext<T>.() -> Unit): Iterable<T> = YieldIterable<T>(body)

body .
? :
fun Test() = yieldfun<Int> { for (val i in 0 .. 3) yield(i); }
.. yieldfun YieldContext, YieldContext this :
this.yield(i)

, this ,
yield(i)
.. body , :
continuationSync.take() body() resultQueue.put(CompletedMessage())
, YieldIterator YieldContext, body - extension-.


. - yield :)

YieldContext ret :
public trait YieldContext<T> { fun yield(value: T): Unit val ret: YieldContext<T> }

YieldIterator this :
override val ret: YieldContext<T> = this
? :
ret.yield(i)
, yield(T) this , :
ret yield i
, .

, C# Kotlin:
fun fibonacci(): Iterable<Int> = yieldfun { var a = 0 var b = 1 while (true) { val sum = a + b; ret yield sum; a = b; b = sum; } }
, , generic- yieldfun - .


, , . . finalize() :
protected fun finalize() { thread?.interrupt() }
catch :
try { continuationSync.take() body() resultQueue.put(CompletedMessage()) } catch (e: InterruptedException) { // if not all items were iterated yield will wait for signal, so finalizer should // interrupt the thread to exit } catch (e: Throwable) { resultQueue.put(ExceptionThrownMessage(e)) }
, continuationSync , .
, , , yieldfun , InterruptedException , yield try-catch , .


, extension- .Net, .. , "" . , , Jetbrains. , Kotlin .

UPD. bitbucket.org/Nerzhul500/kyield/src

Source: https://habr.com/ru/post/In168571/


All Articles