@@ -18,6 +18,8 @@ import reflect.ClassTag
18
18
class Lst [+ T ](val elems : Any ) extends AnyVal { self =>
19
19
import Lst ._
20
20
21
+ transparent def locally [T ](body : => T ): T = body
22
+
21
23
def length : Int = elems match {
22
24
case null => 0
23
25
case elems : Arr => elems.length
@@ -27,7 +29,7 @@ class Lst[+T](val elems: Any) extends AnyVal { self =>
27
29
def isEmpty = elems == null
28
30
def nonEmpty = elems != null
29
31
30
- transparent def foreach (op : => T => Unit ): Unit = {
32
+ transparent def foreach (op : => T => Unit ): Unit = locally {
31
33
def sharedOp (x : T ) = op(x)
32
34
elems match {
33
35
case null =>
@@ -38,7 +40,7 @@ class Lst[+T](val elems: Any) extends AnyVal { self =>
38
40
}
39
41
}
40
42
41
- transparent def foreachReversed (transparent op : T => Unit ): Unit = {
43
+ transparent def foreachReversed (transparent op : T => Unit ): Unit = locally {
42
44
def sharedOp (x : T ) = op(x)
43
45
elems match {
44
46
case null =>
@@ -52,12 +54,14 @@ class Lst[+T](val elems: Any) extends AnyVal { self =>
52
54
/** Like `foreach`, but completely inlines `op`, at the price of generating the code twice.
53
55
* Should be used only of `op` is small
54
56
*/
55
- transparent def foreachInlined (op : => T => Unit ): Unit = elems match {
56
- case null =>
57
- case elems : Arr => def elem (i : Int ) = elems(i).asInstanceOf [T ]
58
- var i = 0
59
- while (i < elems.length) { op(elem(i)); i += 1 }
60
- case elem : T @ unchecked => op(elem)
57
+ transparent def foreachInlined (op : => T => Unit ): Unit = locally {
58
+ elems match {
59
+ case null =>
60
+ case elems : Arr => def elem (i : Int ) = elems(i).asInstanceOf [T ]
61
+ var i = 0
62
+ while (i < elems.length) { op(elem(i)); i += 1 }
63
+ case elem : T @ unchecked => op(elem)
64
+ }
61
65
}
62
66
63
67
def iterator (): Iterator [T ] = elems match {
@@ -101,7 +105,7 @@ class Lst[+T](val elems: Any) extends AnyVal { self =>
101
105
}
102
106
103
107
/** `f` is pulled out, not duplicated */
104
- transparent def map [U ](f : => T => U ): Lst [U ] = {
108
+ transparent def map [U ](f : => T => U ): Lst [U ] = locally {
105
109
def op (x : T ) = f(x)
106
110
elems match {
107
111
case null => Empty
@@ -193,7 +197,7 @@ class Lst[+T](val elems: Any) extends AnyVal { self =>
193
197
buf.toLst
194
198
}
195
199
196
- transparent def exists (p : => T => Boolean ): Boolean = {
200
+ transparent def exists (p : => T => Boolean ): Boolean = locally {
197
201
def op (x : T ) = p(x)
198
202
elems match {
199
203
case null => false
@@ -206,7 +210,7 @@ class Lst[+T](val elems: Any) extends AnyVal { self =>
206
210
}
207
211
}
208
212
209
- transparent def forall (p : => T => Boolean ): Boolean = {
213
+ transparent def forall (p : => T => Boolean ): Boolean = locally {
210
214
def op (x : T ) = p(x)
211
215
elems match {
212
216
case null => true
@@ -229,7 +233,7 @@ class Lst[+T](val elems: Any) extends AnyVal { self =>
229
233
elem == x
230
234
}
231
235
232
- transparent def foldLeft [U ](z : U )(f : => (U , T ) => U ) = {
236
+ transparent def foldLeft [U ](z : U )(f : => (U , T ) => U ) = locally {
233
237
def op (x : U , y : T ) = f(x, y)
234
238
elems match {
235
239
case null => z
0 commit comments