avoid custom copy/paste on map methods for arrays

This commit is contained in:
James Strachan
2012-02-29 14:30:39 +00:00
parent 59460c7afa
commit 6b51c20858
6 changed files with 48 additions and 25 deletions
-13
View File
@@ -106,16 +106,3 @@ inline fun <T> Array<T>.notEmpty() : Boolean = this.size > 0
/** Returns true if the array is empty */
inline fun <T> Array<T>.isEmpty() : Boolean = this.size == 0
/** Returns a new List containing the results of applying the given function to each element in this collection */
inline fun <T, R> Array<T>.map(transform : (T) -> R) : java.util.List<R> {
return mapTo(java.util.ArrayList<R>(this.size), transform)
}
/** Transforms each element of this collection with the given function then adds the results to the given collection */
inline fun <T, R, C: java.util.Collection<in R>> Array<T>.mapTo(result: C, transform : (T) -> R) : C {
for (item in this)
result.add(transform(item))
return result
}
+12
View File
@@ -1,3 +1,15 @@
package std.util
import java.util.*
/** Returns a new List containing the results of applying the given function to each element in this collection */
inline fun <T, R> java.util.Collection<T>.map(transform : (T) -> R) : java.util.List<R> {
return mapTo(java.util.ArrayList<R>(this.size), transform)
}
/** Transforms each element of this collection with the given function then adds the results to the given collection */
inline fun <T, R, C: Collection<in R>> java.util.Collection<T>.mapTo(result: C, transform : (T) -> R) : C {
for (item in this)
result.add(transform(item))
return result
}
-12
View File
@@ -103,15 +103,3 @@ inline fun <T> java.util.Collection<T>.notEmpty() : Boolean = !this.isEmpty()
inline fun <T> java.util.Collection<T>?.notNull() : Collection<T>
= if (this != null) this else Collections.EMPTY_LIST as Collection<T>
/** Returns a new List containing the results of applying the given function to each element in this collection */
inline fun <T, R> java.util.Collection<T>.map(transform : (T) -> R) : java.util.List<R> {
return mapTo(java.util.ArrayList<R>(this.size), transform)
}
/** Transforms each element of this collection with the given function then adds the results to the given collection */
inline fun <T, R, C: Collection<in R>> java.util.Collection<T>.mapTo(result: C, transform : (T) -> R) : C {
for (item in this)
result.add(transform(item))
return result
}
@@ -2,3 +2,15 @@
package std
import java.util.*
/** Returns a new List containing the results of applying the given function to each element in this collection */
inline fun <T, R> Array<T>.map(transform : (T) -> R) : java.util.List<R> {
return mapTo(java.util.ArrayList<R>(this.size), transform)
}
/** Transforms each element of this collection with the given function then adds the results to the given collection */
inline fun <T, R, C: Collection<in R>> Array<T>.mapTo(result: C, transform : (T) -> R) : C {
for (item in this)
result.add(transform(item))
return result
}
@@ -2,3 +2,15 @@
package std.util
import java.util.*
/** Returns a new List containing the results of applying the given function to each element in this collection */
inline fun <T, R> java.lang.Iterable<T>.map(transform : (T) -> R) : java.util.List<R> {
return mapTo(java.util.ArrayList<R>(), transform)
}
/** Transforms each element of this collection with the given function then adds the results to the given collection */
inline fun <T, R, C: Collection<in R>> java.lang.Iterable<T>.mapTo(result: C, transform : (T) -> R) : C {
for (item in this)
result.add(transform(item))
return result
}
@@ -2,3 +2,15 @@
package std
import java.util.*
/** Returns a new List containing the results of applying the given function to each element in this collection */
inline fun <T, R> Iterable<T>.map(transform : (T) -> R) : java.util.List<R> {
return mapTo(java.util.ArrayList<R>(), transform)
}
/** Transforms each element of this collection with the given function then adds the results to the given collection */
inline fun <T, R, C: Collection<in R>> Iterable<T>.mapTo(result: C, transform : (T) -> R) : C {
for (item in this)
result.add(transform(item))
return result
}