From 877de17122b022b7b5f826aee70781721aa28d5b Mon Sep 17 00:00:00 2001 From: James Strachan Date: Fri, 23 Dec 2011 15:46:41 +0000 Subject: [PATCH] move coverage of the standard methods across Iterable, Array, java.util.Iterable (not quite complete but close) --- stdlib/ktSrc/JavaCollections.kt | 10 ++ stdlib/ktSrc/JavaIterables.kt | 17 --- .../generated/ArraysFromJavaCollections.kt | 11 ++ ...enerated.kt => ArraysFromJavaIterables.kt} | 17 --- .../JavaUtilIterablesFromJavaCollections.kt | 11 ++ .../generated/StandardFromJavaIterables.kt | 100 ++++++++++++++++++ testlib/test/GenerateStandardLib.kt | 24 ++++- 7 files changed, 152 insertions(+), 38 deletions(-) create mode 100644 stdlib/ktSrc/JavaCollections.kt create mode 100644 stdlib/ktSrc/generated/ArraysFromJavaCollections.kt rename stdlib/ktSrc/generated/{ArraysGenerated.kt => ArraysFromJavaIterables.kt} (80%) create mode 100644 stdlib/ktSrc/generated/JavaUtilIterablesFromJavaCollections.kt create mode 100644 stdlib/ktSrc/generated/StandardFromJavaIterables.kt diff --git a/stdlib/ktSrc/JavaCollections.kt b/stdlib/ktSrc/JavaCollections.kt new file mode 100644 index 00000000000..d788529d489 --- /dev/null +++ b/stdlib/ktSrc/JavaCollections.kt @@ -0,0 +1,10 @@ +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 java.util.Collection.map(result: Collection = ArrayList(this.size), transform : (T) -> R) : Collection { + for (item in this) + result.add(transform(item)) + return result +} diff --git a/stdlib/ktSrc/JavaIterables.kt b/stdlib/ktSrc/JavaIterables.kt index 1bfecd5e104..27bea581e02 100644 --- a/stdlib/ktSrc/JavaIterables.kt +++ b/stdlib/ktSrc/JavaIterables.kt @@ -1,4 +1,3 @@ -// Note: this file is used to generate methods on Array too package std.util import java.util.* @@ -78,22 +77,6 @@ inline fun java.lang.Iterable.join(separator: String, prefix: String = "" return buffer.toString().sure() } -/** Returns a new collection containing the results of applying the given function to each element in this collection */ -/* -inline fun java.lang.Iterable.map(result: Collection = ArrayList(), transform : (T) -> R) : Collection { - for (item in this) - result.add(transform(item)) - return result -} -*/ - -/** Returns a new collection containing the results of applying the given function to each element in this collection */ -inline fun java.util.Collection.map(result: Collection = ArrayList(this.size), transform : (T) -> R) : Collection { - for (item in this) - result.add(transform(item)) - return result -} - inline fun > java.lang.Iterable.to(result: C) : C { for (elem in this) result.add(elem) diff --git a/stdlib/ktSrc/generated/ArraysFromJavaCollections.kt b/stdlib/ktSrc/generated/ArraysFromJavaCollections.kt new file mode 100644 index 00000000000..325a815df87 --- /dev/null +++ b/stdlib/ktSrc/generated/ArraysFromJavaCollections.kt @@ -0,0 +1,11 @@ +// NOTE this file is auto-generated from stdlib/ktSrc/JavaCollections.kt +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 Array.map(result: Collection = ArrayList(this.size), transform : (T) -> R) : Collection { + for (item in this) + result.add(transform(item)) + return result +} diff --git a/stdlib/ktSrc/generated/ArraysGenerated.kt b/stdlib/ktSrc/generated/ArraysFromJavaIterables.kt similarity index 80% rename from stdlib/ktSrc/generated/ArraysGenerated.kt rename to stdlib/ktSrc/generated/ArraysFromJavaIterables.kt index 0e71d7b3739..bad6f8548ed 100644 --- a/stdlib/ktSrc/generated/ArraysGenerated.kt +++ b/stdlib/ktSrc/generated/ArraysFromJavaIterables.kt @@ -1,6 +1,5 @@ // NOTE this file is auto-generated from stdlib/ktSrc/JavaIterables.kt package std -// Note: this file is used to generate methods on Array too import java.util.* @@ -79,22 +78,6 @@ inline fun Array.join(separator: String, prefix: String = "", postfix: St return buffer.toString().sure() } -/** Returns a new collection containing the results of applying the given function to each element in this collection */ -/* -inline fun Array.map(result: Collection = ArrayList(), transform : (T) -> R) : Collection { - for (item in this) - result.add(transform(item)) - return result -} -*/ - -/** Returns a new collection containing the results of applying the given function to each element in this collection */ -inline fun Array.map(result: Collection = ArrayList(this.size), transform : (T) -> R) : Collection { - for (item in this) - result.add(transform(item)) - return result -} - inline fun > Array.to(result: C) : C { for (elem in this) result.add(elem) diff --git a/stdlib/ktSrc/generated/JavaUtilIterablesFromJavaCollections.kt b/stdlib/ktSrc/generated/JavaUtilIterablesFromJavaCollections.kt new file mode 100644 index 00000000000..e4592189fca --- /dev/null +++ b/stdlib/ktSrc/generated/JavaUtilIterablesFromJavaCollections.kt @@ -0,0 +1,11 @@ +// NOTE this file is auto-generated from stdlib/ktSrc/JavaCollections.kt +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 java.lang.Iterable.map(result: Collection = ArrayList(), transform : (T) -> R) : Collection { + for (item in this) + result.add(transform(item)) + return result +} diff --git a/stdlib/ktSrc/generated/StandardFromJavaIterables.kt b/stdlib/ktSrc/generated/StandardFromJavaIterables.kt new file mode 100644 index 00000000000..f56796318f4 --- /dev/null +++ b/stdlib/ktSrc/generated/StandardFromJavaIterables.kt @@ -0,0 +1,100 @@ +// NOTE this file is auto-generated from stdlib/ktSrc/JavaIterables.kt +package std + +import java.util.* + +/** Returns true if any elements in the collection match the given predicate */ +inline fun Iterable.any(predicate: (T)-> Boolean) : Boolean { + for (elem in this) { + if (predicate(elem)) { + return true + } + } + return false +} + +/** Returns true if all elements in the collection match the given predicate */ +inline fun Iterable.all(predicate: (T)-> Boolean) : Boolean { + for (elem in this) { + if (!predicate(elem)) { + return false + } + } + return true +} + +/** Returns the first item in the collection which matches the given predicate or null if none matched */ +inline fun Iterable.find(predicate: (T)-> Boolean) : T? { + for (elem in this) { + if (predicate(elem)) + return elem + } + return null +} + +/** Returns a new collection containing all elements in this collection which match the given predicate */ +inline fun Iterable.filter(result: Collection = ArrayList(), predicate: (T)-> Boolean) : Collection { + for (elem in this) { + if (predicate(elem)) + result.add(elem) + } + return result +} + +/** + * Returns the result of transforming each item in the collection to a one or more values which + * are concatenated together into a single collection + */ +inline fun Iterable.flatMap(result: Collection = ArrayList(), transform: (T)-> Collection) : Collection { + for (elem in this) { + val coll = transform(elem) + if (coll != null) { + for (r in coll) { + result.add(r) + } + } + } + return result +} + +/** Performs the given operation on each element inside the collection */ +inline fun Iterable.foreach(operation: (element: T) -> Unit) { + for (elem in this) + operation(elem) +} + +/** 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 Iterable.join(separator: String, prefix: String = "", postfix: String = "") : String { + val buffer = StringBuilder(prefix) + var first = true + for (elem in this) { + if (first) + first = false + else + buffer.append(separator) + buffer.append(elem) + } + buffer.append(postfix) + return buffer.toString().sure() +} + +inline fun > Iterable.to(result: C) : C { + for (elem in this) + result.add(elem) + return result +} + +inline fun Iterable.toLinkedList() : LinkedList = this.to(LinkedList()) + +inline fun Iterable.toList() : List = this.to(ArrayList()) + +inline fun Iterable.toSet() : Set = this.to(HashSet()) + +/** + TODO figure out necessary variance/generics ninja stuff... :) +inline fun Iterable.toSortedList(transform: fun(T) : java.lang.Comparable<*>) : List { + val answer = this.toList() + answer.sort(transform) + return answer +} +*/ diff --git a/testlib/test/GenerateStandardLib.kt b/testlib/test/GenerateStandardLib.kt index 49671f40885..f835ccdf493 100644 --- a/testlib/test/GenerateStandardLib.kt +++ b/testlib/test/GenerateStandardLib.kt @@ -6,7 +6,7 @@ import std.util.* import java.io.* import java.util.* -fun generateFile(outFile: File, header: String, typeName: String, inputFile: File) { +fun generateFile(outFile: File, header: String, inputFile: File, f: (String)-> String) { println("Parsing $inputFile and writing $outFile") outFile.getParentFile()?.mkdirs() @@ -23,7 +23,8 @@ fun generateFile(outFile: File, header: String, typeName: String, inputFile: Fil val line = iter.next() if (line.startsWith("package")) continue - val xform = line.replaceAll("java.lang.Iterable", typeName).replaceAll("java.util.Collection", typeName) + + val xform = f(line) writer.println(xform) } } finally { @@ -53,7 +54,22 @@ fun main(args: Array) { } } val srcDir = File(stdlib, "ktSrc") - val input = File(srcDir, "JavaIterables.kt") val outDir = File(srcDir, "generated") - generateFile(File(outDir, "ArraysGenerated.kt"), "package std", "Array", input) + + + generateFile(File(outDir, "ArraysFromJavaIterables.kt"), "package std", File(srcDir, "JavaIterables.kt")) { + it.replaceAll("java.lang.Iterable", "Array") + } + + generateFile(File(outDir, "StandardFromJavaIterables.kt"), "package std", File(srcDir, "JavaIterables.kt")) { + it.replaceAll("java.lang.Iterable", "Iterable") + } + + generateFile(File(outDir, "JavaUtilIterablesFromJavaCollections.kt"), "package std.util", File(srcDir, "JavaCollections.kt")) { + it.replaceAll("java.util.Collection", "java.lang.Iterable").replaceAll("(this.size)", "") + } + + generateFile(File(outDir, "ArraysFromJavaCollections.kt"), "package std.util", File(srcDir, "JavaCollections.kt")) { + it.replaceAll("java.util.Collection", "Array") + } } \ No newline at end of file