first cut of a little bit of code generation to make sure we mirror most of the java.util.Iterable<T> methods to Array<T>
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
// Note: this file is used to generate methods on Array<T> too
|
||||
package std.util
|
||||
|
||||
import java.util.*
|
||||
|
||||
/** Returns true if any elements in the collection match the given predicate */
|
||||
inline fun <T> java.lang.Iterable<T>.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 <T> java.lang.Iterable<T>.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 <T> java.lang.Iterable<T>.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 <T> java.lang.Iterable<T>.filter(result: Collection<T> = ArrayList<T>(), predicate: (T)-> Boolean) : Collection<T> {
|
||||
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 <T, out R> java.lang.Iterable<T>.flatMap(result: Collection<R> = ArrayList<R>(), transform: (T)-> Collection<R>) : Collection<R> {
|
||||
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 <T> java.lang.Iterable<T>.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 <T> java.lang.Iterable<T>.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()
|
||||
}
|
||||
|
||||
/** Returns a new collection containing the results of applying the given function to each element in this collection */
|
||||
/*
|
||||
inline fun <T, R> java.lang.Iterable<T>.map(result: Collection<R> = ArrayList<R>(), transform : (T) -> R) : Collection<R> {
|
||||
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 <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
|
||||
}
|
||||
|
||||
inline fun <T, C: Collection<T>> java.lang.Iterable<T>.to(result: C) : C {
|
||||
for (elem in this)
|
||||
result.add(elem)
|
||||
return result
|
||||
}
|
||||
|
||||
inline fun <T> java.lang.Iterable<T>.toLinkedList() : LinkedList<T> = this.to(LinkedList<T>())
|
||||
|
||||
inline fun <T> java.lang.Iterable<T>.toList() : List<T> = this.to(ArrayList<T>())
|
||||
|
||||
inline fun <T> java.lang.Iterable<T>.toSet() : Set<T> = this.to(HashSet<T>())
|
||||
|
||||
/**
|
||||
TODO figure out necessary variance/generics ninja stuff... :)
|
||||
inline fun <in T> java.lang.Iterable<T>.toSortedList(transform: fun(T) : java.lang.Comparable<*>) : List<T> {
|
||||
val answer = this.toList()
|
||||
answer.sort(transform)
|
||||
return answer
|
||||
}
|
||||
*/
|
||||
+6
-121
@@ -19,127 +19,6 @@ inline fun linkedList<T>(vararg values: T) : LinkedList<T> = values.to(LinkedLi
|
||||
/** Returns a new HashSet with a variable number of initial elements */
|
||||
inline fun hashSet<T>(vararg values: T) : HashSet<T> = values.to(HashSet<T>(values.size))
|
||||
|
||||
/** Returns true if any elements in the collection match the given predicate */
|
||||
inline fun <T> java.lang.Iterable<T>.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 <T> java.lang.Iterable<T>.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 <T> java.lang.Iterable<T>.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 <T> java.lang.Iterable<T>.filter(result: Collection<T> = ArrayList<T>(), predicate: (T)-> Boolean) : Collection<T> {
|
||||
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 <T, out R> java.lang.Iterable<T>.flatMap(result: Collection<R> = ArrayList<R>(), transform: (T)-> Collection<R>) : Collection<R> {
|
||||
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 <T> java.lang.Iterable<T>.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 <T> java.lang.Iterable<T>.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()
|
||||
}
|
||||
|
||||
/** Returns a new collection containing the results of applying the given function to each element in this collection */
|
||||
inline fun <T, R> java.lang.Iterable<T>.map(result: Collection<R> = ArrayList<R>(), transform : (T) -> R) : Collection<R> {
|
||||
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 <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
|
||||
}
|
||||
|
||||
// TODO would be nice to not have to write extension methods for Array, Iterable and Iterator
|
||||
|
||||
inline fun <T, C: Collection<T>> Array<T>.to(result: C) : C {
|
||||
for (elem in this)
|
||||
result.add(elem)
|
||||
return result
|
||||
}
|
||||
|
||||
inline fun <T, C: Collection<T>> java.lang.Iterable<T>.to(result: C) : C {
|
||||
for (elem in this)
|
||||
result.add(elem)
|
||||
return result
|
||||
}
|
||||
|
||||
inline fun <T> java.lang.Iterable<T>.toLinkedList() : LinkedList<T> = this.to(LinkedList<T>())
|
||||
|
||||
inline fun <T> java.lang.Iterable<T>.toList() : List<T> = this.to(ArrayList<T>())
|
||||
|
||||
inline fun <T> java.lang.Iterable<T>.toSet() : Set<T> = this.to(HashSet<T>())
|
||||
|
||||
inline fun <in T: java.lang.Comparable<T>> java.lang.Iterable<T>.toSortedList() : List<T> = toList().sort()
|
||||
|
||||
inline fun <in T: java.lang.Comparable<T>> java.lang.Iterable<T>.toSortedList(comparator: java.util.Comparator<T>) : List<T> = toList().sort(comparator)
|
||||
|
||||
/**
|
||||
TODO figure out necessary variance/generics ninja stuff... :)
|
||||
inline fun <in T> java.lang.Iterable<T>.toSortedList(transform: fun(T) : java.lang.Comparable<*>) : List<T> {
|
||||
val answer = this.toList()
|
||||
answer.sort(transform)
|
||||
return answer
|
||||
}
|
||||
*/
|
||||
|
||||
inline fun <T> java.util.Collection<T>.toArray() : Array<T> {
|
||||
val answer = Array<T>(this.size)
|
||||
@@ -149,6 +28,12 @@ inline fun <T> java.util.Collection<T>.toArray() : Array<T> {
|
||||
return answer as Array<T>
|
||||
}
|
||||
|
||||
/** TODO these functions don't work when they generate the Array<T> versions when they are in JavaIterables */
|
||||
inline fun <in T: java.lang.Comparable<T>> java.lang.Iterable<T>.toSortedList() : List<T> = toList().sort()
|
||||
|
||||
inline fun <in T: java.lang.Comparable<T>> java.lang.Iterable<T>.toSortedList(comparator: java.util.Comparator<T>) : List<T> = toList().sort(comparator)
|
||||
|
||||
|
||||
|
||||
// List APIs
|
||||
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
// NOTE this file is auto-generated from stdlib/ktSrc/JavaIterables.kt
|
||||
package std
|
||||
// Note: this file is used to generate methods on Array<T> too
|
||||
|
||||
import java.util.*
|
||||
|
||||
/** Returns true if any elements in the collection match the given predicate */
|
||||
inline fun <T> Array<T>.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 <T> Array<T>.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 <T> Array<T>.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 <T> Array<T>.filter(result: Collection<T> = ArrayList<T>(), predicate: (T)-> Boolean) : Collection<T> {
|
||||
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 <T, out R> Array<T>.flatMap(result: Collection<R> = ArrayList<R>(), transform: (T)-> Collection<R>) : Collection<R> {
|
||||
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 <T> Array<T>.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 <T> Array<T>.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()
|
||||
}
|
||||
|
||||
/** Returns a new collection containing the results of applying the given function to each element in this collection */
|
||||
/*
|
||||
inline fun <T, R> Array<T>.map(result: Collection<R> = ArrayList<R>(), transform : (T) -> R) : Collection<R> {
|
||||
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 <T, R> Array<T>.map(result: Collection<R> = ArrayList<R>(this.size), transform : (T) -> R) : Collection<R> {
|
||||
for (item in this)
|
||||
result.add(transform(item))
|
||||
return result
|
||||
}
|
||||
|
||||
inline fun <T, C: Collection<T>> Array<T>.to(result: C) : C {
|
||||
for (elem in this)
|
||||
result.add(elem)
|
||||
return result
|
||||
}
|
||||
|
||||
inline fun <T> Array<T>.toLinkedList() : LinkedList<T> = this.to(LinkedList<T>())
|
||||
|
||||
inline fun <T> Array<T>.toList() : List<T> = this.to(ArrayList<T>())
|
||||
|
||||
inline fun <T> Array<T>.toSet() : Set<T> = this.to(HashSet<T>())
|
||||
|
||||
/**
|
||||
TODO figure out necessary variance/generics ninja stuff... :)
|
||||
inline fun <in T> Array<T>.toSortedList(transform: fun(T) : java.lang.Comparable<*>) : List<T> {
|
||||
val answer = this.toList()
|
||||
answer.sort(transform)
|
||||
return answer
|
||||
}
|
||||
*/
|
||||
@@ -0,0 +1,59 @@
|
||||
namespace kotlin.tools
|
||||
|
||||
import std.*
|
||||
import std.io.*
|
||||
import std.util.*
|
||||
import java.io.*
|
||||
import java.util.*
|
||||
|
||||
fun generateFile(outFile: File, header: String, typeName: String, inputFile: File) {
|
||||
println("Parsing $inputFile and writing $outFile")
|
||||
|
||||
outFile.getParentFile()?.mkdirs()
|
||||
val writer = PrintWriter(FileWriter(outFile))
|
||||
try {
|
||||
writer.println("// NOTE this file is auto-generated from $inputFile")
|
||||
writer.println(header)
|
||||
|
||||
val reader = FileReader(inputFile).buffered()
|
||||
try {
|
||||
// TODO ideally we'd use a filterNot() here :)
|
||||
val iter = reader.lineIterator()
|
||||
while (iter.hasNext) {
|
||||
val line = iter.next()
|
||||
|
||||
if (line.startsWith("package")) continue
|
||||
val xform = line.replaceAll("java.lang.Iterable<T>", typeName).replaceAll("java.util.Collection<T>", typeName)
|
||||
writer.println(xform)
|
||||
}
|
||||
} finally {
|
||||
reader.close()
|
||||
reader.close()
|
||||
}
|
||||
} finally {
|
||||
writer.close()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Generates methods in the standard library which are mostly identical
|
||||
* but just using a different input kind.
|
||||
*
|
||||
* Kinda like mimicking source macros here, but this avoids the inefficiency of type conversions
|
||||
* at runtime.
|
||||
*/
|
||||
fun main(args: Array<String>) {
|
||||
var stdlib = File("stdlib")
|
||||
if (!stdlib.exists()) {
|
||||
stdlib = File("../stdlib")
|
||||
if (!stdlib.exists()) {
|
||||
println("Cannot find stdlib!")
|
||||
return
|
||||
}
|
||||
}
|
||||
val srcDir = File(stdlib, "ktSrc")
|
||||
val input = File(srcDir, "JavaIterables.kt")
|
||||
val outDir = File(srcDir, "generated")
|
||||
generateFile(File(outDir, "ArraysGenerated.kt"), "package std", "Array<T>", input)
|
||||
}
|
||||
Reference in New Issue
Block a user