refactored foreach() -> forEach() as per Cedric's excellent suggestion http://devnet.jetbrains.net/message/5454105#5454105

This commit is contained in:
James Strachan
2012-03-12 12:07:38 +00:00
parent 5805165793
commit d469710c7c
9 changed files with 13 additions and 13 deletions
@@ -81,7 +81,7 @@ abstract class TextTemplate() : TemplateSupport(), Printer {
fun renderTo(os: OutputStream): Unit { fun renderTo(os: OutputStream): Unit {
// TODO compiler error // TODO compiler error
//OutputStreamWriter(os).foreach{ renderTo(it) } //OutputStreamWriter(os).forEach{ renderTo(it) }
val s = OutputStreamWriter(os) val s = OutputStreamWriter(os)
renderTo(s) renderTo(s)
s.close() s.close()
@@ -89,7 +89,7 @@ abstract class TextTemplate() : TemplateSupport(), Printer {
fun renderTo(file: File): Unit { fun renderTo(file: File): Unit {
// TODO compiler error // TODO compiler error
//FileWriter(file).foreach{ s -> renderTo(s) } //FileWriter(file).forEach{ s -> renderTo(s) }
val s = FileWriter(file) val s = FileWriter(file)
renderTo(s) renderTo(s)
s.close() s.close()
+1 -1
View File
@@ -199,7 +199,7 @@ inline fun Reader.buffered(): BufferedReader = if(this is BufferedReader) this e
inline fun Reader.buffered(bufferSize: Int) = BufferedReader(this, bufferSize) inline fun Reader.buffered(bufferSize: Int) = BufferedReader(this, bufferSize)
inline fun Reader.foreachLine(block: (String) -> Unit): Unit { inline fun Reader.forEachLine(block: (String) -> Unit): Unit {
this.use{ this.use{
val iter = buffered().lineIterator() val iter = buffered().lineIterator()
while (iter.hasNext) { while (iter.hasNext) {
+1 -1
View File
@@ -104,7 +104,7 @@ inline fun <T, R> java.lang.Iterable<T>.flatMapTo(result: Collection<R>, transfo
} }
/** Performs the given operation on each element inside the collection */ /** Performs the given operation on each element inside the collection */
inline fun <T> java.lang.Iterable<T>.foreach(operation: (element: T) -> Unit) { inline fun <T> java.lang.Iterable<T>.forEach(operation: (element: T) -> Unit) {
for (elem in this) for (elem in this)
operation(elem) operation(elem)
} }
@@ -107,7 +107,7 @@ inline fun <T, R> Array<T>.flatMapTo(result: Collection<R>, transform: (T)-> Col
} }
/** Performs the given operation on each element inside the collection */ /** Performs the given operation on each element inside the collection */
inline fun <T> Array<T>.foreach(operation: (element: T) -> Unit) { inline fun <T> Array<T>.forEach(operation: (element: T) -> Unit) {
for (elem in this) for (elem in this)
operation(elem) operation(elem)
} }
@@ -95,7 +95,7 @@ inline fun <T, R> Iterable<T>.flatMapTo(result: Collection<R>, transform: (T)->
} }
/** Performs the given operation on each element inside the collection */ /** Performs the given operation on each element inside the collection */
inline fun <T> Iterable<T>.foreach(operation: (element: T) -> Unit) { inline fun <T> Iterable<T>.forEach(operation: (element: T) -> Unit) {
for (elem in this) for (elem in this)
operation(elem) operation(elem)
} }
+1 -1
View File
@@ -20,7 +20,7 @@ trait Traversable<T> {
fun filter(predicate: (T)-> Boolean) : Collection<T> fun filter(predicate: (T)-> Boolean) : Collection<T>
/** Performs the given operation on each element inside the collection */ /** Performs the given operation on each element inside the collection */
fun foreach(operation: (element: T)-> Unit) fun forEach(operation: (element: T)-> Unit)
/** Returns a new collection containing the results of applying the given function to each element in this collection */ /** Returns a new collection containing the results of applying the given function to each element in this collection */
fun <T, R> java.lang.Iterable<T>.map(transform : (T)-> R) : Collection<R> fun <T, R> java.lang.Iterable<T>.map(transform : (T)-> R) : Collection<R>
+1 -1
View File
@@ -125,7 +125,7 @@ class CollectionTest() : TestCase() {
fun testForeach() { fun testForeach() {
var count = 0 var count = 0
data.foreach{ count += it.length } data.forEach{ count += it.length }
assertEquals(6, count) assertEquals(6, count)
} }
+4 -4
View File
@@ -55,17 +55,17 @@ class IoTest() : TestCase() {
val reader = sample() val reader = sample()
/* TODO would be nicer maybe to write this as /* TODO would be nicer maybe to write this as
reader.lines.foreach { ... } reader.lines.forEach { ... }
as we could one day maybe one day write that as as we could one day maybe one day write that as
for (line in reader.lines) for (line in reader.lines)
if the for(elem in thing) {...} statement could act as syntax sugar for if the for(elem in thing) {...} statement could act as syntax sugar for
thing.foreach{ elem -> ... } thing.forEach{ elem -> ... }
if thing is not an Iterable/array/Iterator but has a suitable foreach method if thing is not an Iterable/array/Iterator but has a suitable forEach method
*/ */
reader.foreachLine{ reader.forEachLine{
list.add(it) list.add(it)
} }
+1 -1
View File
@@ -17,7 +17,7 @@ class Kt1149Test() : TestCase() {
res.add("anonymous.foo()") res.add("anonymous.foo()")
} }
}) })
list.foreach{ it.foo() } list.forEach{ it.foo() }
Assert.assertEquals("anonymous.foo()", res[0]) Assert.assertEquals("anonymous.foo()", res[0])
} }
} }