split map() with default arguments into map() and mapTo(resultCollection) to simplify IDE/completion/compiler error messages

This commit is contained in:
James Strachan
2012-02-29 14:25:11 +00:00
parent fe78ac5e67
commit 59460c7afa
10 changed files with 195 additions and 31 deletions
+14
View File
@@ -151,6 +151,20 @@
</java>
</target>
<target name="generateStdlib" depends="compileTestlib" description="Generates the stdlib APIs for arrays and kotlin collections">
<java classname="kotlin.tools.namespace" failonerror="true" fork="true">
<classpath>
<pathelement location="${kotlin-home}/lib/kotlin-runtime.jar"/>
<pathelement location="${kotlin-home}/lib/kotlin-test.jar"/>
<pathelement location="${basedir}/testlib/lib/junit-4.9.jar"/>
<fileset dir="${basedir}/testlib/lib">
<include name="**/*.jar"/>
</fileset>
<pathelement path="${output}/classes/testlib"/>
</classpath>
</java>
</target>
<target name="testlib" depends="compileTestlib">
<mkdir dir="${output}/test-reports"/>
+15
View File
@@ -104,3 +104,18 @@ inline fun ByteArray.inputStream(offset: Int, length: Int) = ByteArrayInputStrea
/** Returns true if the array is not empty */
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
}
-14
View File
@@ -1,17 +1,3 @@
package std.util
import java.util.*
/** Returns a new collection containing the results of applying the given function to each element in this collection */
inline fun <T, R> java.util.Collection<T>.map(result: Collection<R> = ArrayList<R>(this.size), transform : (T) -> R) : Collection<R> {
for (item in this)
result.add(transform(item))
return result
}
/** Returns true if the collection is not empty */
inline fun <T> java.util.Collection<T>.notEmpty() : Boolean = !this.isEmpty()
/** Converts the nullable collection into an empty collection if its null */
inline fun <T> java.util.Collection<T>?.notNull() : Collection<T>
= if (this != null) this else Collections.EMPTY_LIST as Collection<T>
+18
View File
@@ -96,4 +96,22 @@ val <T> List<T>.last : T?
get() = this.tail
/** Returns true if the collection is not empty */
inline fun <T> java.util.Collection<T>.notEmpty() : Boolean = !this.isEmpty()
/** Converts the nullable collection into an empty collection if its null */
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
}
@@ -1,6 +1,8 @@
// NOTE this file is auto-generated from stdlib/ktSrc/JavaIterables.kt
package std
import std.util.*
import java.util.*
/** Returns true if any elements in the collection match the given predicate */
@@ -23,6 +25,16 @@ inline fun <T> Array<T>.all(predicate: (T)-> Boolean) : Boolean {
return true
}
/** Returns the number of items which match the given predicate */
inline fun <T> Array<T>.count(predicate: (T)-> Boolean) : Int {
var answer = 0
for (elem in this) {
if (predicate(elem))
answer += 1
}
return answer
}
/** Returns the first item in the collection which matches the given predicate or null if none matched */
inline fun <T> Array<T>.find(predicate: (T)-> Boolean) : T? {
for (elem in this) {
@@ -32,8 +44,11 @@ inline fun <T> Array<T>.find(predicate: (T)-> Boolean) : T? {
return null
}
/** Returns a new collection containing all elements in this collection which match the given predicate */
inline fun <T> Array<T>.filter(result: Collection<T> = ArrayList<T>(), predicate: (T)-> Boolean) : Collection<T> {
/** Returns a new List containing all elements in this collection which match the given predicate */
inline fun <T> Array<T>.filter(predicate: (T)-> Boolean) : Collection<T> = filterTo(java.util.ArrayList<T>(), predicate)
/** Filters all elements in this collection which match the given predicate into the given result collection */
inline fun <T, C: Collection<in T>> Array<T>.filterTo(result: C, predicate: (T)-> Boolean) : C {
for (elem in this) {
if (predicate(elem))
result.add(elem)
@@ -42,7 +57,10 @@ inline fun <T> Array<T>.filter(result: Collection<T> = ArrayList<T>(), predicate
}
/** Returns a new collection containing all elements in this collection which do not match the given predicate */
inline fun <T> Array<T>.filterNot(result: Collection<T> = ArrayList<T>(), predicate: (T)-> Boolean) : Collection<T> {
inline fun <T> Array<T>.filterNot(predicate: (T)-> Boolean) : Collection<T> = filterNotTo(ArrayList<T>(), predicate)
/** Returns a new collection containing all elements in this collection which do not match the given predicate */
inline fun <T, C: Collection<in T>> Array<T>.filterNotTo(result: C, predicate: (T)-> Boolean) : C {
for (elem in this) {
if (!predicate(elem))
result.add(elem)
@@ -72,6 +90,42 @@ inline fun <T> Array<T>.foreach(operation: (element: T) -> Unit) {
operation(elem)
}
/**
* Folds all the values from from left to right with the initial value to perform the operation on sequential pairs of values
*
* For example to sum together all numeric values in a collection of numbers it would be
* {code}val total = numbers.fold(0){(a, b) -> a + b}{code}
*/
inline fun <T> Array<T>.fold(initial: T, operation: (it: T, it2: T) -> T): T {
var answer = initial
for (elem in this) {
answer = operation(answer, elem)
}
return answer
}
/**
* Folds all the values from right to left with the initial value to perform the operation on sequential pairs of values
*/
inline fun <T> Array<T>.foldRight(initial: T, operation: (it: T, it2: T) -> T): T {
val reversed = this.reverse()
return reversed.fold(initial, operation)
}
/**
* Iterates through the collection performing the transformation on each element and using the result
* as the key in a map to group elements by the result
*/
inline fun <T,K> Array<T>.groupBy(result: Map<K,List<T>> = HashMap<K,List<T>>(), toKey: (T)-> K) : Map<K,List<T>> {
for (elem in this) {
val key = toKey(elem)
val list = result.getOrPut(key){ ArrayList<T>() }
list.add(elem)
}
return result
}
/** Creates a String from all the elements in the collection, using the seperator between them and using the given prefix and postfix if supplied */
inline fun <T> Array<T>.join(separator: String, prefix: String = "", postfix: String = "") : String {
val buffer = StringBuilder(prefix)
@@ -87,18 +141,33 @@ inline fun <T> Array<T>.join(separator: String, prefix: String = "", postfix: St
return buffer.toString().sure()
}
/** Returns a reversed List of this collection */
inline fun <T> Array<T>.reverse() : List<T> {
val answer = LinkedList<T>()
for (elem in this) {
answer.addFirst(elem)
}
return answer
}
/* Copies the collection into the given collection */
inline fun <T, C: Collection<T>> Array<T>.to(result: C) : C {
for (elem in this)
result.add(elem)
return result
}
/* Converts the collection into a LinkedList */
inline fun <T> Array<T>.toLinkedList() : LinkedList<T> = this.to(LinkedList<T>())
/* Converts the collection into a List */
inline fun <T> Array<T>.toList() : List<T> = this.to(ArrayList<T>())
/* Converts the collection into a Set */
inline fun <T> Array<T>.toSet() : Set<T> = this.to(HashSet<T>())
/* Converts the collection into a SortedSet */
inline fun <T> Array<T>.toSortedSet() : SortedSet<T> = this.to(TreeSet<T>())
/**
TODO figure out necessary variance/generics ninja stuff... :)
inline fun <in T> Array<T>.toSortedList(transform: fun(T) : java.lang.Comparable<*>) : List<T> {
@@ -2,4 +2,3 @@
package std.util
import java.util.*
@@ -2,10 +2,3 @@
package std
import java.util.*
/** Returns a new collection containing the results of applying the given function to each element in this collection */
inline fun <T, R> Iterable<T>.map(result: Collection<R> = ArrayList<R>(), transform : (T) -> R) : Collection<R> {
for (item in this)
result.add(transform(item))
return result
}
@@ -1,6 +1,8 @@
// NOTE this file is auto-generated from stdlib/ktSrc/JavaIterables.kt
package std
import std.util.*
import java.util.*
/** Returns true if any elements in the collection match the given predicate */
@@ -23,6 +25,16 @@ inline fun <T> Iterable<T>.all(predicate: (T)-> Boolean) : Boolean {
return true
}
/** Returns the number of items which match the given predicate */
inline fun <T> Iterable<T>.count(predicate: (T)-> Boolean) : Int {
var answer = 0
for (elem in this) {
if (predicate(elem))
answer += 1
}
return answer
}
/** Returns the first item in the collection which matches the given predicate or null if none matched */
inline fun <T> Iterable<T>.find(predicate: (T)-> Boolean) : T? {
for (elem in this) {
@@ -32,8 +44,11 @@ inline fun <T> Iterable<T>.find(predicate: (T)-> Boolean) : T? {
return null
}
/** Returns a new collection containing all elements in this collection which match the given predicate */
inline fun <T> Iterable<T>.filter(result: Collection<T> = ArrayList<T>(), predicate: (T)-> Boolean) : Collection<T> {
/** Returns a new List containing all elements in this collection which match the given predicate */
inline fun <T> Iterable<T>.filter(predicate: (T)-> Boolean) : Collection<T> = filterTo(java.util.ArrayList<T>(), predicate)
/** Filters all elements in this collection which match the given predicate into the given result collection */
inline fun <T, C: Collection<in T>> Iterable<T>.filterTo(result: C, predicate: (T)-> Boolean) : C {
for (elem in this) {
if (predicate(elem))
result.add(elem)
@@ -42,7 +57,10 @@ inline fun <T> Iterable<T>.filter(result: Collection<T> = ArrayList<T>(), predic
}
/** Returns a new collection containing all elements in this collection which do not match the given predicate */
inline fun <T> Iterable<T>.filterNot(result: Collection<T> = ArrayList<T>(), predicate: (T)-> Boolean) : Collection<T> {
inline fun <T> Iterable<T>.filterNot(predicate: (T)-> Boolean) : Collection<T> = filterNotTo(ArrayList<T>(), predicate)
/** Returns a new collection containing all elements in this collection which do not match the given predicate */
inline fun <T, C: Collection<in T>> Iterable<T>.filterNotTo(result: C, predicate: (T)-> Boolean) : C {
for (elem in this) {
if (!predicate(elem))
result.add(elem)
@@ -72,6 +90,42 @@ inline fun <T> Iterable<T>.foreach(operation: (element: T) -> Unit) {
operation(elem)
}
/**
* Folds all the values from from left to right with the initial value to perform the operation on sequential pairs of values
*
* For example to sum together all numeric values in a collection of numbers it would be
* {code}val total = numbers.fold(0){(a, b) -> a + b}{code}
*/
inline fun <T> Iterable<T>.fold(initial: T, operation: (it: T, it2: T) -> T): T {
var answer = initial
for (elem in this) {
answer = operation(answer, elem)
}
return answer
}
/**
* Folds all the values from right to left with the initial value to perform the operation on sequential pairs of values
*/
inline fun <T> Iterable<T>.foldRight(initial: T, operation: (it: T, it2: T) -> T): T {
val reversed = this.reverse()
return reversed.fold(initial, operation)
}
/**
* Iterates through the collection performing the transformation on each element and using the result
* as the key in a map to group elements by the result
*/
inline fun <T,K> Iterable<T>.groupBy(result: Map<K,List<T>> = HashMap<K,List<T>>(), toKey: (T)-> K) : Map<K,List<T>> {
for (elem in this) {
val key = toKey(elem)
val list = result.getOrPut(key){ ArrayList<T>() }
list.add(elem)
}
return result
}
/** Creates a String from all the elements in the collection, using the seperator between them and using the given prefix and postfix if supplied */
inline fun <T> Iterable<T>.join(separator: String, prefix: String = "", postfix: String = "") : String {
val buffer = StringBuilder(prefix)
@@ -87,18 +141,33 @@ inline fun <T> Iterable<T>.join(separator: String, prefix: String = "", postfix:
return buffer.toString().sure()
}
/** Returns a reversed List of this collection */
inline fun <T> Iterable<T>.reverse() : List<T> {
val answer = LinkedList<T>()
for (elem in this) {
answer.addFirst(elem)
}
return answer
}
/* Copies the collection into the given collection */
inline fun <T, C: Collection<T>> Iterable<T>.to(result: C) : C {
for (elem in this)
result.add(elem)
return result
}
/* Converts the collection into a LinkedList */
inline fun <T> Iterable<T>.toLinkedList() : LinkedList<T> = this.to(LinkedList<T>())
/* Converts the collection into a List */
inline fun <T> Iterable<T>.toList() : List<T> = this.to(ArrayList<T>())
/* Converts the collection into a Set */
inline fun <T> Iterable<T>.toSet() : Set<T> = this.to(HashSet<T>())
/* Converts the collection into a SortedSet */
inline fun <T> Iterable<T>.toSortedSet() : SortedSet<T> = this.to(TreeSet<T>())
/**
TODO figure out necessary variance/generics ninja stuff... :)
inline fun <in T> Iterable<T>.toSortedList(transform: fun(T) : java.lang.Comparable<*>) : List<T> {
+2 -2
View File
@@ -58,11 +58,11 @@ fun main(args: Array<String>) {
// JavaIterables - Generic iterable stuff
generateFile(File(outDir, "ArraysFromJavaIterables.kt"), "package std", File(srcDir, "JavaIterables.kt")) {
generateFile(File(outDir, "ArraysFromJavaIterables.kt"), "package std\n\nimport std.util.*", File(srcDir, "JavaIterables.kt")) {
it.replaceAll("java.lang.Iterable<T>", "Array<T>")
}
generateFile(File(outDir, "StandardFromJavaIterables.kt"), "package std", File(srcDir, "JavaIterables.kt")) {
generateFile(File(outDir, "StandardFromJavaIterables.kt"), "package std\n\nimport std.util.*", File(srcDir, "JavaIterables.kt")) {
it.replaceAll("java.lang.Iterable<T>", "Iterable<T>")
}
+2 -1
View File
@@ -5,7 +5,8 @@ import junit.framework.*
import java.util.*
public fun scheduleRefresh(vararg files : Object) {
java.util.ArrayList<Object>(files.map{ it })
// TODO
// java.util.ArrayList<Object>(files.map{ it })
}
fun main(args : Array<String?>?) {