public
Last active

code with scalac -print output at the end

  • Download Gist
Yield.scala
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
// The continuation code is from an answer by Rich posted at StackOverflow
// http://stackoverflow.com/questions/2201882/implementing-yield-yield-return-using-scala-continuations/2215182#2215182
 
import scala.continuations._
import scala.continuations.ControlContext.{shift,reset}
 
 
sealed trait Iteration[+R]
case class Yield[+R](result: R, next: () => Iteration[R]) extends Iteration[R]
case object Done extends Iteration[Nothing]
 
object YieldTest {
 
def trampoline[R](body: => Iteration[R]): Iterator[R] = {
def loop(thunk: () => Iteration[R]): Stream[R] = {
thunk.apply match {
case Yield(result, next) => Stream.cons(result, loop(next))
case Done => Stream.empty
}
}
loop(() => body).iterator
}
 
def iterator[R](body: => Unit @cps[Iteration[R],Iteration[R]]): Iterator[R] =
trampoline {
reset[Iteration[R],Iteration[R]] { body ; Done }
}
 
def yld[R](result: R): Unit @cps[Iteration[R],Iteration[R]] =
shift((k: Unit => Iteration[R]) => Yield(result, () => k(())))
 
def main(args:Array[String]): Unit = {
 
val itr2 = iterator[Int] {
yld(1)
yld(2)
yld(3)
}
for (i <- itr2) { println(i) }
 
}
 
}
 
 
/** OUTPUT of scalac -print *************************************/
 
[[syntax trees at end of cleanup]]// Scala source: Yield.scala
package <empty> {
sealed abstract trait Iteration extends java.lang.Object;
@serializable
case class Yield extends java.lang.Object with Iteration with ScalaObject with Product {
def productIterator(): Iterator = scala.Product$class.productIterator(Yield.this);
@deprecated("use productIterator instead")
def productElements(): Iterator = scala.Product$class.productElements(Yield.this);
<synthetic> def copy$default$2(): Function0 = Yield.this.next();
<synthetic> def copy$default$1(): java.lang.Object = Yield.this.result();
<caseaccessor> <paramaccessor> private[this] val result: java.lang.Object = _;
<stable> <caseaccessor> <accessor> <paramaccessor> def result(): java.lang.Object = Yield.this.result;
<caseaccessor> <paramaccessor> private[this] val next: Function0 = _;
<stable> <caseaccessor> <accessor> <paramaccessor> def next(): Function0 = Yield.this.next;
<synthetic> def copy(result: java.lang.Object = result, next: Function0 = next): Yield = new Yield(result, next);
override def hashCode(): Int = ScalaRunTime.this._hashCode(Yield.this);
override def toString(): java.lang.String = ScalaRunTime.this._toString(Yield.this);
override def equals(x$1: java.lang.Object): Boolean = Yield.this.eq(x$1).||({
{
var temp1: java.lang.Object = x$1;
if (temp1.$isInstanceOf[Yield]())
{
{
var temp2: Yield = temp1.$asInstanceOf[Yield]();
{
var temp3: java.lang.Object = temp2.result();
{
var temp4: Function0 = temp2.next();
{
{
val next$1: Function0 = temp4;
{
val result$1: java.lang.Object = temp3;
if (Yield.this.gd1$1(result$1, next$1))
{
{
val result$1: java.lang.Object = temp3;
{
{
x$1.$asInstanceOf[Yield]().canEqual(Yield.this)
}
}
}
}
else
{
false
}
}
}
}
}
}
}
}
else
{
false
}
}
});
override def productPrefix(): java.lang.String = "Yield";
override def productArity(): Int = 2;
override def productElement(x$1: Int): java.lang.Object = {
var temp5: Int = x$1;
(temp5: Int) match {
case 0 => {
Yield.this.result()
}
case 1 => {
Yield.this.next()
}
case _ => {
throw new java.lang.IndexOutOfBoundsException(scala.Int.box(x$1).toString())
}
}
};
override def canEqual(x$1: java.lang.Object): Boolean = x$1.$isInstanceOf[Yield]();
final <synthetic> private[this] def gd1$1(x$1: java.lang.Object, x$2: Function0): Boolean = x$1.==(Yield.this.result()).&&(x$2.==(Yield.this.next()));
def this(result: java.lang.Object, next: Function0): Yield = {
Yield.this.result = result;
Yield.this.next = next;
Yield.super.this();
scala.Product$class./*Product$class*/$init$(Yield.this);
()
}
};
@serializable
final case class Done extends java.lang.Object with Iteration with ScalaObject with Product {
def productIterator(): Iterator = scala.Product$class.productIterator(Done.this);
@deprecated("use productIterator instead")
def productElements(): Iterator = scala.Product$class.productElements(Done.this);
final override def toString(): java.lang.String = "Done";
override def productPrefix(): java.lang.String = "Done";
override def productArity(): Int = 0;
override def productElement(x$1: Int): java.lang.Object = {
var temp6: Int = x$1;
{
throw new java.lang.IndexOutOfBoundsException(scala.Int.box(x$1).toString())
}
};
override def canEqual(x$1: java.lang.Object): Boolean = x$1.$isInstanceOf[object Done]();
protected def readResolve(): java.lang.Object = Done;
def this(): object Done = {
Done.super.this();
scala.Product$class./*Product$class*/$init$(Done.this);
()
}
};
final class YieldTest extends java.lang.Object with ScalaObject {
def trampoline(body: Function0): Iterator = YieldTest.this.loop$1(body).iterator();
def iterator(body$1: Function0): Iterator = YieldTest.this.trampoline({
(new YieldTest$$anonfun$iterator$1(body$1): Function0)
});
def yld(result$2: java.lang.Object): scala.continuations.ControlContext = ControlContext.this.shiftR({
(new YieldTest$$anonfun$yld$1(result$2): Function1)
});
def main(args: Array[java.lang.String]): Unit = {
val itr2: Iterator = YieldTest.this.iterator({
(new YieldTest$$anonfun$1(): Function0)
});
itr2.foreach({
(new YieldTest$$anonfun$main$1(): Function1)
})
};
final def loop$1(thunk: Function0): scala.collection.immutable.Stream = {
val temp7: Iteration = thunk.apply().$asInstanceOf[Iteration]();
if (temp7.$isInstanceOf[Yield]())
{
{
var temp8: Yield = temp7.$asInstanceOf[Yield]();
{
var temp9: java.lang.Object = temp8.result();
{
var temp10: Function0 = temp8.next();
{
{
val result: java.lang.Object = temp9;
{
{
val next$2: Function0 = temp10;
scala.collection.immutable.Stream$cons.apply(result, {
(new YieldTest$$anonfun$loop$1$1(next$2): Function0)
})
}
}
}
}
}
}
}
}
else
if (Done.==(temp7))
{
{
{
scala.package.Stream().empty()
}
}
}
else
throw new MatchError(temp7.toString())
};
def this(): object YieldTest = {
YieldTest.super.this();
()
}
};
final <synthetic> class Yield extends java.lang.Object with ScalaObject {
case <synthetic> def unapply(x$0: Yield): Some = new Some(new Tuple2(x$0.result(), x$0.next()));
case <synthetic> def apply(result: java.lang.Object, next: Function0): Yield = new Yield(result, next);
def this(): object Yield = {
Yield.super.this();
()
}
};
@SerialVersionUID(0) @serializable
final <synthetic> class YieldTest$$anonfun$loop$1$1 extends scala.runtime.AbstractFunction0 {
final def apply(): scala.collection.immutable.Stream = YieldTest.loop$1(YieldTest$$anonfun$loop$1$1.this.next$2);
final <bridge> def apply(): java.lang.Object = YieldTest$$anonfun$loop$1$1.this.apply();
<synthetic> <paramaccessor> private[this] val next$2: Function0 = _;
def this(next$2: Function0): YieldTest$$anonfun$loop$1$1 = {
YieldTest$$anonfun$loop$1$1.this.next$2 = next$2;
YieldTest$$anonfun$loop$1$1.super.this();
()
}
};
@SerialVersionUID(0) @serializable
final <synthetic> class YieldTest$$anonfun$iterator$1$$anonfun$apply$1$$anonfun$apply$2 extends scala.runtime.AbstractFunction1 {
final def apply(tmp1: scala.runtime.BoxedUnit): object Done = {
tmp1;
Done
};
final <bridge> def apply(v1: java.lang.Object): java.lang.Object = YieldTest$$anonfun$iterator$1$$anonfun$apply$1$$anonfun$apply$2.this.apply(v1.$asInstanceOf[scala.runtime.BoxedUnit]());
def this($outer: YieldTest$$anonfun$iterator$1$$anonfun$apply$1): YieldTest$$anonfun$iterator$1$$anonfun$apply$1$$anonfun$apply$2 = {
YieldTest$$anonfun$iterator$1$$anonfun$apply$1$$anonfun$apply$2.super.this();
()
}
};
@SerialVersionUID(0) @serializable
final <synthetic> class YieldTest$$anonfun$iterator$1$$anonfun$apply$1 extends scala.runtime.AbstractFunction0 {
final def apply(): scala.continuations.ControlContext = YieldTest$$anonfun$iterator$1$$anonfun$apply$1.this.$outer.body$1.apply().$asInstanceOf[scala.continuations.ControlContext]().map({
(new YieldTest$$anonfun$iterator$1$$anonfun$apply$1$$anonfun$apply$2(YieldTest$$anonfun$iterator$1$$anonfun$apply$1.this): Function1)
});
<synthetic> <paramaccessor> private[this] val $outer: YieldTest$$anonfun$iterator$1 = _;
final <bridge> def apply(): java.lang.Object = YieldTest$$anonfun$iterator$1$$anonfun$apply$1.this.apply();
def this($outer: YieldTest$$anonfun$iterator$1): YieldTest$$anonfun$iterator$1$$anonfun$apply$1 = {
if ($outer.eq(null))
throw new java.lang.NullPointerException()
else
YieldTest$$anonfun$iterator$1$$anonfun$apply$1.this.$outer = $outer;
YieldTest$$anonfun$iterator$1$$anonfun$apply$1.super.this();
()
}
};
@SerialVersionUID(0) @serializable
final <synthetic> class YieldTest$$anonfun$iterator$1 extends scala.runtime.AbstractFunction0 {
final def apply(): Iteration = scala.continuations.ControlContext.reset({
(new YieldTest$$anonfun$iterator$1$$anonfun$apply$1(YieldTest$$anonfun$iterator$1.this): Function0)
}).$asInstanceOf[Iteration]();
final <bridge> def apply(): java.lang.Object = YieldTest$$anonfun$iterator$1.this.apply();
<synthetic> <paramaccessor> val body$1: Function0 = _;
def this(body$1: Function0): YieldTest$$anonfun$iterator$1 = {
YieldTest$$anonfun$iterator$1.this.body$1 = body$1;
YieldTest$$anonfun$iterator$1.super.this();
()
}
};
@SerialVersionUID(0) @serializable
final <synthetic> class YieldTest$$anonfun$yld$1$$anonfun$apply$3 extends scala.runtime.AbstractFunction0 {
final def apply(): Iteration = YieldTest$$anonfun$yld$1$$anonfun$apply$3.this.k$1.apply(scala.runtime.BoxedUnit.UNIT).$asInstanceOf[Iteration]();
final <bridge> def apply(): java.lang.Object = YieldTest$$anonfun$yld$1$$anonfun$apply$3.this.apply();
<synthetic> <paramaccessor> private[this] val k$1: Function1 = _;
def this($outer: YieldTest$$anonfun$yld$1, k$1: Function1): YieldTest$$anonfun$yld$1$$anonfun$apply$3 = {
YieldTest$$anonfun$yld$1$$anonfun$apply$3.this.k$1 = k$1;
YieldTest$$anonfun$yld$1$$anonfun$apply$3.super.this();
()
}
};
@SerialVersionUID(0) @serializable
final <synthetic> class YieldTest$$anonfun$yld$1 extends scala.runtime.AbstractFunction1 {
final def apply(k$1: Function1): Yield = new Yield(YieldTest$$anonfun$yld$1.this.result$2, {
(new YieldTest$$anonfun$yld$1$$anonfun$apply$3(YieldTest$$anonfun$yld$1.this, k$1): Function0)
});
final <bridge> def apply(v1: java.lang.Object): java.lang.Object = YieldTest$$anonfun$yld$1.this.apply(v1.$asInstanceOf[Function1]());
<synthetic> <paramaccessor> private[this] val result$2: java.lang.Object = _;
def this(result$2: java.lang.Object): YieldTest$$anonfun$yld$1 = {
YieldTest$$anonfun$yld$1.this.result$2 = result$2;
YieldTest$$anonfun$yld$1.super.this();
()
}
};
@SerialVersionUID(0) @serializable
final <synthetic> class YieldTest$$anonfun$1$$anonfun$apply$4$$anonfun$apply$5 extends scala.runtime.AbstractFunction1 {
final def apply(tmp3: scala.runtime.BoxedUnit): scala.continuations.ControlContext = {
tmp3;
YieldTest.this.yld(scala.Int.box(3))
};
final <bridge> def apply(v1: java.lang.Object): java.lang.Object = YieldTest$$anonfun$1$$anonfun$apply$4$$anonfun$apply$5.this.apply(v1.$asInstanceOf[scala.runtime.BoxedUnit]());
def this($outer: YieldTest$$anonfun$1$$anonfun$apply$4): YieldTest$$anonfun$1$$anonfun$apply$4$$anonfun$apply$5 = {
YieldTest$$anonfun$1$$anonfun$apply$4$$anonfun$apply$5.super.this();
()
}
};
@SerialVersionUID(0) @serializable
final <synthetic> class YieldTest$$anonfun$1$$anonfun$apply$4 extends scala.runtime.AbstractFunction1 {
final def apply(tmp2: scala.runtime.BoxedUnit): scala.continuations.ControlContext = {
tmp2;
YieldTest.this.yld(scala.Int.box(2)).flatMap({
(new YieldTest$$anonfun$1$$anonfun$apply$4$$anonfun$apply$5(YieldTest$$anonfun$1$$anonfun$apply$4.this): Function1)
})
};
final <bridge> def apply(v1: java.lang.Object): java.lang.Object = YieldTest$$anonfun$1$$anonfun$apply$4.this.apply(v1.$asInstanceOf[scala.runtime.BoxedUnit]());
def this($outer: YieldTest$$anonfun$1): YieldTest$$anonfun$1$$anonfun$apply$4 = {
YieldTest$$anonfun$1$$anonfun$apply$4.super.this();
()
}
};
@SerialVersionUID(0) @serializable
final <synthetic> class YieldTest$$anonfun$1 extends scala.runtime.AbstractFunction0 {
final def apply(): scala.continuations.ControlContext = YieldTest.this.yld(scala.Int.box(1)).flatMap({
(new YieldTest$$anonfun$1$$anonfun$apply$4(YieldTest$$anonfun$1.this): Function1)
});
final <bridge> def apply(): java.lang.Object = YieldTest$$anonfun$1.this.apply();
def this(): YieldTest$$anonfun$1 = {
YieldTest$$anonfun$1.super.this();
()
}
};
@SerialVersionUID(0) @serializable
final <synthetic> class YieldTest$$anonfun$main$1 extends scala.runtime.AbstractFunction1 {
final def apply(i: Int): Unit = scala.this.Predef.println(scala.Int.box(i));
final <bridge> def apply(v1: java.lang.Object): java.lang.Object = {
YieldTest$$anonfun$main$1.this.apply(scala.Int.unbox(v1));
scala.runtime.BoxedUnit.UNIT
};
def this(): YieldTest$$anonfun$main$1 = {
YieldTest$$anonfun$main$1.super.this();
()
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.