Merge pull request #33 from franckrasolo/stdlib

#KT-1650 Fixed
This commit is contained in:
James Strachan
2012-03-29 06:18:06 -07:00
11 changed files with 527 additions and 675 deletions
@@ -1,4 +1,4 @@
// NOTE this file is auto-generated from src/kotlin/JavaCollections.kt // NOTE this file is auto-generated from stdlib/src/kotlin/JavaCollections.kt
package kotlin package kotlin
import java.util.* import java.util.*
@@ -1,4 +1,4 @@
// NOTE this file is auto-generated from src/kotlin/JavaIterables.kt // NOTE this file is auto-generated from stdlib/src/kotlin/JavaIterables.kt
package kotlin package kotlin
import kotlin.util.* import kotlin.util.*
@@ -6,229 +6,172 @@ import kotlin.util.*
import java.util.* import java.util.*
/** /**
* Returns true if any elements in the collection match the given predicate * Returns *true* if any elements match the given *predicate*
* *
* @includeFunction ../../test/CollectionTest.kt any * @includeFunction ../../test/CollectionTest.kt any
*/ */
inline fun <T> Array<T>.any(predicate: (T)-> Boolean) : Boolean { inline fun <T> Array<T>.any(predicate: (T) -> Boolean) : Boolean {
for (elem in this) { for (element in this) if (predicate(element)) return true
if (predicate(elem)) { return false
return true
}
}
return false
} }
/** /**
* Returns true if all elements in the collection match the given predicate * Returns *true* if all elements match the given *predicate*
* *
* @includeFunction ../../test/CollectionTest.kt all * @includeFunction ../../test/CollectionTest.kt all
*/ */
inline fun <T> Array<T>.all(predicate: (T)-> Boolean) : Boolean { inline fun <T> Array<T>.all(predicate: (T) -> Boolean) : Boolean {
for (elem in this) { for (element in this) if (!predicate(element)) return false
if (!predicate(elem)) { return true
return false
}
}
return true
} }
/** /**
* Returns the number of items which match the given predicate * Returns the number of elements which match the given *predicate*
* *
* @includeFunction ../../test/CollectionTest.kt count * @includeFunction ../../test/CollectionTest.kt count
*/ */
inline fun <T> Array<T>.count(predicate: (T)-> Boolean) : Int { inline fun <T> Array<T>.count(predicate: (T) -> Boolean) : Int {
var answer = 0 var count = 0
for (elem in this) { for (element in this) if (predicate(element)) count++
if (predicate(elem)) return count
answer += 1
}
return answer
} }
/** /**
* Returns the first item in the collection which matches the given predicate or null if none matched * Returns the first element which matches the given *predicate* or *null* if none matched
* *
* @includeFunction ../../test/CollectionTest.kt find * @includeFunction ../../test/CollectionTest.kt find
*/ */
inline fun <T> Array<T>.find(predicate: (T)-> Boolean) : T? { inline fun <T> Array<T>.find(predicate: (T) -> Boolean) : T? {
for (elem in this) { for (element in this) if (predicate(element)) return element
if (predicate(elem)) return null
return elem
}
return null
} }
/** /**
* Filters all elements in this collection which match the given predicate into the given result collection * Filters all elements which match the given predicate into the given list
* *
* @includeFunction ../../test/CollectionTest.kt filterIntoLinkedList * @includeFunction ../../test/CollectionTest.kt filterIntoLinkedList
*/ */
inline fun <T, C: Collection<in T>> Array<T>.filterTo(result: C, predicate: (T)-> Boolean) : C { inline fun <T, C: Collection<in T>> Array<T>.filterTo(result: C, predicate: (T) -> Boolean) : C {
for (elem in this) { for (element in this) if (predicate(element)) result.add(element)
if (predicate(elem)) return result
result.add(elem)
}
return result
} }
/** /**
* Filters all the null elements in this collection into the given result collection * Returns a list containing all elements which do not match the given *predicate*
*
* @includeFunction ../../test/CollectionTest.kt filterNotIntoLinkedList
*/
inline fun <T, L: List<in T>> Array<T>.filterNotTo(result: L, predicate: (T) -> Boolean) : L {
for (element in this) if (!predicate(element)) result.add(element)
return result
}
/**
* Filters all non-*null* elements into the given list
* *
* @includeFunction ../../test/CollectionTest.kt filterNotNullIntoLinkedList * @includeFunction ../../test/CollectionTest.kt filterNotNullIntoLinkedList
*/ */
inline fun <T, C: Collection<in T>> Array<T?>?.filterNotNullTo(result: C) : C { inline fun <T, L: List<in T>> Array<T?>?.filterNotNullTo(result: L) : L {
if (this != null) { if (this != null) {
for (elem in this) { for (element in this) if (element != null) result.add(element)
if (elem != null) { }
result.add(elem) return result
} }
/**
* Returns the result of transforming each element to one or more values which are concatenated together into a single list
*
* @includeFunction ../../test/CollectionTest.kt flatMap
*/
inline fun <T, R> Array<T>.flatMapTo(result: Collection<R>, transform: (T) -> Collection<R>) : Collection<R> {
for (element in this) {
val list = transform(element)
if (list != null) {
for (r in list) result.add(r)
} }
} }
return result return result
} }
/** /**
* Returns a new collection containing all elements in this collection which do not match the given predicate * Performs the given *operation* on each element
*
* @includeFunction ../../test/CollectionTest.kt filterNotIntoLinkedList
*/
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)
}
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
*/
// TODO * @includeFunction ../../test/CollectionTest.kt flatMapTo
inline fun <T, R> Array<T>.flatMapTo(result: Collection<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
* *
* @includeFunction ../../test/CollectionTest.kt forEach * @includeFunction ../../test/CollectionTest.kt forEach
*/ */
inline fun <T> Array<T>.forEach(operation: (element: T) -> Unit) { inline fun <T> Array<T>.forEach(operation: (T) -> Unit) = for (element in this) operation(element)
for (elem in this)
operation(elem)
}
/** /**
* Folds all the values from from left to right with the initial value to perform the operation on sequential pairs of values * Folds all elements from from left to right with the *initial* value to perform the operation on sequential pairs of elements
* *
* @includeFunction ../../test/CollectionTest.kt fold * @includeFunction ../../test/CollectionTest.kt fold
*/ */
inline fun <T> Array<T>.fold(initial: T, operation: (it: T, it2: T) -> T): T { inline fun <T> Array<T>.fold(initial: T, operation: (T, T) -> T): T {
var answer = initial var answer = initial
for (elem in this) { for (element in this) answer = operation(answer, element)
answer = operation(answer, elem) return answer
}
return answer
} }
/** /**
* Folds all the values from right to left with the initial value to perform the operation on sequential pairs of values * Folds all elements from right to left with the *initial* value to perform the operation on sequential pairs of elements
* *
* @includeFunction ../../test/CollectionTest.kt foldRight * @includeFunction ../../test/CollectionTest.kt foldRight
*/ */
inline fun <T> Array<T>.foldRight(initial: T, operation: (it: T, it2: T) -> T): T { inline fun <T> Array<T>.foldRight(initial: T, operation: (T, T) -> T): T = reverse().fold(initial, operation)
val reversed = this.reverse()
return reversed.fold(initial, operation)
}
/** /**
* Iterates through the collection performing the transformation on each element and using the result * Transforms each element using the result as the key in a map to group elements by the result
* as the key in a map to group elements by the result
* *
* @includeFunction ../../test/CollectionTest.kt groupBy * @includeFunction ../../test/CollectionTest.kt groupBy
*/ */
inline fun <T,K> Array<T>.groupBy(result: Map<K,List<T>> = HashMap<K,List<T>>(), toKey: (T)-> K) : Map<K,List<T>> { 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) { for (element in this) {
val key = toKey(elem) val key = toKey(element)
val list = result.getOrPut(key){ ArrayList<T>() } val list = result.getOrPut(key) { ArrayList<T>() }
list.add(elem) list.add(element)
} }
return result return result
} }
/** Returns a list containing the first elements that satisfy the given *predicate* */
/** inline fun <T, L: List<in T>> Array<T>.takeWhileTo(result: L, predicate: (T) -> Boolean) : L {
* Creates a String from all the elements in the collection, using the seperator between them and using the given prefix and postfix if supplied for (element in this) if (predicate(element)) result.add(element) else break
* return result
* @includeFunction ../../test/CollectionTest.kt join
*/
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 reversed List of this collection * Reverses the order the elements into a list
* *
* @includeFunction ../../test/CollectionTest.kt reverse * @includeFunction ../../test/CollectionTest.kt reverse
*/ */
inline fun <T> Array<T>.reverse() : List<T> { inline fun <T> Array<T>.reverse() : List<T> {
val answer = LinkedList<T>() val answer = LinkedList<T>()
for (elem in this) { for (element in this) answer.addFirst(element)
answer.addFirst(elem) return answer
}
return answer
} }
/** /** Copies all elements into the given collection */
* Copies the collection into the given collection
*
* @includeFunction ../../test/CollectionTest.kt reverse
*/
inline fun <T, C: Collection<T>> Array<T>.to(result: C) : C { inline fun <T, C: Collection<T>> Array<T>.to(result: C) : C {
for (elem in this) for (element in this) result.add(element)
result.add(elem) return result
return result
} }
/** /** Copies all elements into a [[LinkedList]] */
* Converts the collection into a LinkedList inline fun <T> Array<T>.toLinkedList() : LinkedList<T> = to(LinkedList<T>())
*/
inline fun <T> Array<T>.toLinkedList() : LinkedList<T> = this.to(LinkedList<T>())
/** Converts the collection into a List */ /** Copies all elements into a [[List]] */
inline fun <T> Array<T>.toList() : List<T> = this.to(ArrayList<T>()) inline fun <T> Array<T>.toList() : List<T> = to(ArrayList<T>())
/** Converts the collection into a Set */ /** Copies all elements into a [[Set]] */
inline fun <T> Array<T>.toSet() : Set<T> = this.to(HashSet<T>()) inline fun <T> Array<T>.toSet() : Set<T> = to(HashSet<T>())
/** Copies all elements into a [[SortedSet]] */
inline fun <T> Array<T>.toSortedSet() : SortedSet<T> = to(TreeSet<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... :) TODO figure out necessary variance/generics ninja stuff... :)
inline fun <in T> Array<T>.toSortedList(transform: fun(T) : java.lang.Comparable<*>) : List<T> { inline fun <in T> Array<T>.toSortedList(transform: fun(T) : java.lang.Comparable<*>) : List<T> {
val answer = this.toList() val answer = this.toList()
answer.sort(transform) answer.sort(transform)
return answer return answer
} }
*/ */
@@ -1,9 +1,11 @@
// NOTE this file is auto-generated from src/kotlin/JavaIterablesLazy.kt // NOTE this file is auto-generated from stdlib/src/kotlin/JavaIterablesLazy.kt
package kotlin package kotlin
import kotlin.util.* import kotlin.util.*
import java.util.* import java.util.ArrayList
import java.util.Collection
import java.util.List
// //
// This file contains methods which could have a lazy implementation for things like // This file contains methods which could have a lazy implementation for things like
@@ -13,33 +15,65 @@ import java.util.*
// //
/** /**
* Returns a new List containing all elements in this collection which match the given predicate * Returns a list containing all elements which match the given *predicate*
* *
* @includeFunction ../../test/CollectionTest.kt filter * @includeFunction ../../test/CollectionTest.kt filter
*/ */
inline fun <T> Array<T>.filter(predicate: (T)-> Boolean) : Collection<T> = filterTo(java.util.ArrayList<T>(), predicate) inline fun <T> Array<T>.filter(predicate: (T) -> Boolean) : List<T> = filterTo(ArrayList<T>(), predicate)
/** /**
* Returns a List containing all the non null elements in this collection * Returns a list containing all elements which do not match the given predicate
*
* @includeFunction ../../test/CollectionTest.kt filterNotNull
*/
inline fun <T> Array<T?>?.filterNotNull() : Collection<T> = filterNotNullTo<T, java.util.ArrayList<T>>(java.util.ArrayList<T>())
/**
* Returns a new collection containing all elements in this collection which do not match the given predicate
* *
* @includeFunction ../../test/CollectionTest.kt filterNot * @includeFunction ../../test/CollectionTest.kt filterNot
*/ */
inline fun <T> Array<T>.filterNot(predicate: (T)-> Boolean) : Collection<T> = filterNotTo(ArrayList<T>(), predicate) inline fun <T> Array<T>.filterNot(predicate: (T)-> Boolean) : List<T> = filterNotTo(ArrayList<T>(), predicate)
/** /**
* Returns the result of transforming each item in the collection to a one or more values which * Returns a list containing all the non-*null* elements
* are concatenated together into a single collection *
* @includeFunction ../../test/CollectionTest.kt filterNotNull
*/
inline fun <T> Array<T?>?.filterNotNull() : List<T> = filterNotNullTo<T, ArrayList<T>>(java.util.ArrayList<T>())
/**
* Returns the result of transforming each element to one or more values which are concatenated together into a single collection
* *
* @includeFunction ../../test/CollectionTest.kt flatMap * @includeFunction ../../test/CollectionTest.kt flatMap
*/ */
inline fun <T, R> Array<T>.flatMap(transform: (T)-> Collection<R>) : Collection<R> { inline fun <T, R> Array<T>.flatMap(transform: (T)-> Collection<R>) : Collection<R> = flatMapTo(ArrayList<R>(), transform)
return flatMapTo(ArrayList<R>(), transform)
/**
* Returns a list containing the first *n* elements
*
* @includeFunction ../../test/CollectionTest.kt take
*/
inline fun <T> Array<T>.take(n: Int): List<T> {
fun countTo(n: Int): (T) -> Boolean {
var count = 0
return { ++count; count <= n }
}
return takeWhile(countTo(n))
} }
/**
* Returns a list containing the first elements that satisfy the given *predicate*
*
* @includeFunction ../../test/CollectionTest.kt takeWhile
*/
inline fun <T> Array<T>.takeWhile(predicate: (T) -> Boolean): List<T> = takeWhileTo(ArrayList<T>(), predicate)
/**
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
*
* @includeFunction ../../test/CollectionTest.kt join
*/
inline fun <T> Array<T>.join(separator: String, prefix: String = "", postfix: String = "") : String {
val buffer = StringBuilder(prefix)
var first = true
for (element in this) {
if (first) first = false else buffer.append(separator)
buffer.append(element)
}
buffer.append(postfix)
return buffer.toString().sure()
}
@@ -1,4 +1,4 @@
// NOTE this file is auto-generated from src/kotlin/JavaCollections.kt // NOTE this file is auto-generated from stdlib/src/kotlin/JavaCollections.kt
package kotlin package kotlin
import java.util.* import java.util.*
@@ -1,232 +1,175 @@
// NOTE this file is auto-generated from src/kotlin/JavaIterables.kt // NOTE this file is auto-generated from stdlib/src/kotlin/JavaIterables.kt
package kotlin package kotlin
import java.util.* import java.util.*
/** /**
* Returns true if any elements in the collection match the given predicate * Returns *true* if any elements match the given *predicate*
* *
* @includeFunction ../../test/CollectionTest.kt any * @includeFunction ../../test/CollectionTest.kt any
*/ */
inline fun <T> java.util.Iterator<T>.any(predicate: (T)-> Boolean) : Boolean { inline fun <T> java.util.Iterator<T>.any(predicate: (T) -> Boolean) : Boolean {
for (elem in this) { for (element in this) if (predicate(element)) return true
if (predicate(elem)) { return false
return true
}
}
return false
} }
/** /**
* Returns true if all elements in the collection match the given predicate * Returns *true* if all elements match the given *predicate*
* *
* @includeFunction ../../test/CollectionTest.kt all * @includeFunction ../../test/CollectionTest.kt all
*/ */
inline fun <T> java.util.Iterator<T>.all(predicate: (T)-> Boolean) : Boolean { inline fun <T> java.util.Iterator<T>.all(predicate: (T) -> Boolean) : Boolean {
for (elem in this) { for (element in this) if (!predicate(element)) return false
if (!predicate(elem)) { return true
return false
}
}
return true
} }
/** /**
* Returns the number of items which match the given predicate * Returns the number of elements which match the given *predicate*
* *
* @includeFunction ../../test/CollectionTest.kt count * @includeFunction ../../test/CollectionTest.kt count
*/ */
inline fun <T> java.util.Iterator<T>.count(predicate: (T)-> Boolean) : Int { inline fun <T> java.util.Iterator<T>.count(predicate: (T) -> Boolean) : Int {
var answer = 0 var count = 0
for (elem in this) { for (element in this) if (predicate(element)) count++
if (predicate(elem)) return count
answer += 1
}
return answer
} }
/** /**
* Returns the first item in the collection which matches the given predicate or null if none matched * Returns the first element which matches the given *predicate* or *null* if none matched
* *
* @includeFunction ../../test/CollectionTest.kt find * @includeFunction ../../test/CollectionTest.kt find
*/ */
inline fun <T> java.util.Iterator<T>.find(predicate: (T)-> Boolean) : T? { inline fun <T> java.util.Iterator<T>.find(predicate: (T) -> Boolean) : T? {
for (elem in this) { for (element in this) if (predicate(element)) return element
if (predicate(elem)) return null
return elem
}
return null
} }
/** /**
* Filters all elements in this collection which match the given predicate into the given result collection * Filters all elements which match the given predicate into the given list
* *
* @includeFunction ../../test/CollectionTest.kt filterIntoLinkedList * @includeFunction ../../test/CollectionTest.kt filterIntoLinkedList
*/ */
inline fun <T, C: Collection<in T>> java.util.Iterator<T>.filterTo(result: C, predicate: (T)-> Boolean) : C { inline fun <T, C: Collection<in T>> java.util.Iterator<T>.filterTo(result: C, predicate: (T) -> Boolean) : C {
for (elem in this) { for (element in this) if (predicate(element)) result.add(element)
if (predicate(elem)) return result
result.add(elem)
}
return result
} }
/** /**
* Filters all the null elements in this collection into the given result collection * Returns a list containing all elements which do not match the given *predicate*
*
* @includeFunction ../../test/CollectionTest.kt filterNotIntoLinkedList
*/
inline fun <T, L: List<in T>> java.util.Iterator<T>.filterNotTo(result: L, predicate: (T) -> Boolean) : L {
for (element in this) if (!predicate(element)) result.add(element)
return result
}
/**
* Filters all non-*null* elements into the given list
* *
* @includeFunction ../../test/CollectionTest.kt filterNotNullIntoLinkedList * @includeFunction ../../test/CollectionTest.kt filterNotNullIntoLinkedList
*/ */
inline fun <T, C: Collection<in T>> java.util.Iterator<T?>?.filterNotNullTo(result: C) : C { inline fun <T, L: List<in T>> java.util.Iterator<T?>?.filterNotNullTo(result: L) : L {
if (this != null) { if (this != null) {
for (elem in this) { for (element in this) if (element != null) result.add(element)
if (elem != null) { }
result.add(elem) return result
} }
/**
* Returns the result of transforming each element to one or more values which are concatenated together into a single list
*
* @includeFunction ../../test/CollectionTest.kt flatMap
*/
inline fun <T, R> java.util.Iterator<T>.flatMapTo(result: Collection<R>, transform: (T) -> Collection<R>) : Collection<R> {
for (element in this) {
val list = transform(element)
if (list != null) {
for (r in list) result.add(r)
} }
} }
return result return result
} }
/** /**
* Returns a new collection containing all elements in this collection which do not match the given predicate * Performs the given *operation* on each element
*
* @includeFunction ../../test/CollectionTest.kt filterNotIntoLinkedList
*/
inline fun <T, C: Collection<in T>> java.util.Iterator<T>.filterNotTo(result: C, predicate: (T)-> Boolean) : C {
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
*/
// TODO * @includeFunction ../../test/CollectionTest.kt flatMapTo
inline fun <T, R> java.util.Iterator<T>.flatMapTo(result: Collection<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
* *
* @includeFunction ../../test/CollectionTest.kt forEach * @includeFunction ../../test/CollectionTest.kt forEach
*/ */
inline fun <T> java.util.Iterator<T>.forEach(operation: (element: T) -> Unit) { inline fun <T> java.util.Iterator<T>.forEach(operation: (T) -> Unit) = for (element in this) operation(element)
for (elem in this)
operation(elem)
}
/** /**
* Folds all the values from from left to right with the initial value to perform the operation on sequential pairs of values * Folds all elements from from left to right with the *initial* value to perform the operation on sequential pairs of elements
* *
* @includeFunction ../../test/CollectionTest.kt fold * @includeFunction ../../test/CollectionTest.kt fold
*/ */
inline fun <T> java.util.Iterator<T>.fold(initial: T, operation: (it: T, it2: T) -> T): T { inline fun <T> java.util.Iterator<T>.fold(initial: T, operation: (T, T) -> T): T {
var answer = initial var answer = initial
for (elem in this) { for (element in this) answer = operation(answer, element)
answer = operation(answer, elem) return answer
}
return answer
} }
/** /**
* Folds all the values from right to left with the initial value to perform the operation on sequential pairs of values * Folds all elements from right to left with the *initial* value to perform the operation on sequential pairs of elements
* *
* @includeFunction ../../test/CollectionTest.kt foldRight * @includeFunction ../../test/CollectionTest.kt foldRight
*/ */
inline fun <T> java.util.Iterator<T>.foldRight(initial: T, operation: (it: T, it2: T) -> T): T { inline fun <T> java.util.Iterator<T>.foldRight(initial: T, operation: (T, T) -> T): T = reverse().fold(initial, operation)
val reversed = this.reverse()
return reversed.fold(initial, operation)
}
/** /**
* Iterates through the collection performing the transformation on each element and using the result * Transforms each element using the result as the key in a map to group elements by the result
* as the key in a map to group elements by the result
* *
* @includeFunction ../../test/CollectionTest.kt groupBy * @includeFunction ../../test/CollectionTest.kt groupBy
*/ */
inline fun <T,K> java.util.Iterator<T>.groupBy(result: Map<K,List<T>> = HashMap<K,List<T>>(), toKey: (T)-> K) : Map<K,List<T>> { inline fun <T, K> java.util.Iterator<T>.groupBy(result: Map<K, List<T>> = HashMap<K, List<T>>(), toKey: (T) -> K) : Map<K, List<T>> {
for (elem in this) { for (element in this) {
val key = toKey(elem) val key = toKey(element)
val list = result.getOrPut(key){ ArrayList<T>() } val list = result.getOrPut(key) { ArrayList<T>() }
list.add(elem) list.add(element)
} }
return result return result
} }
/** Returns a list containing the first elements that satisfy the given *predicate* */
/** inline fun <T, L: List<in T>> java.util.Iterator<T>.takeWhileTo(result: L, predicate: (T) -> Boolean) : L {
* Creates a String from all the elements in the collection, using the seperator between them and using the given prefix and postfix if supplied for (element in this) if (predicate(element)) result.add(element) else break
* return result
* @includeFunction ../../test/CollectionTest.kt join
*/
inline fun <T> java.util.Iterator<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 reversed List of this collection * Reverses the order the elements into a list
* *
* @includeFunction ../../test/CollectionTest.kt reverse * @includeFunction ../../test/CollectionTest.kt reverse
*/ */
inline fun <T> java.util.Iterator<T>.reverse() : List<T> { inline fun <T> java.util.Iterator<T>.reverse() : List<T> {
val answer = LinkedList<T>() val answer = LinkedList<T>()
for (elem in this) { for (element in this) answer.addFirst(element)
answer.addFirst(elem) return answer
}
return answer
} }
/** /** Copies all elements into the given collection */
* Copies the collection into the given collection
*
* @includeFunction ../../test/CollectionTest.kt reverse
*/
inline fun <T, C: Collection<T>> java.util.Iterator<T>.to(result: C) : C { inline fun <T, C: Collection<T>> java.util.Iterator<T>.to(result: C) : C {
for (elem in this) for (element in this) result.add(element)
result.add(elem) return result
return result
} }
/** /** Copies all elements into a [[LinkedList]] */
* Converts the collection into a LinkedList inline fun <T> java.util.Iterator<T>.toLinkedList() : LinkedList<T> = to(LinkedList<T>())
*/
inline fun <T> java.util.Iterator<T>.toLinkedList() : LinkedList<T> = this.to(LinkedList<T>())
/** Converts the collection into a List */ /** Copies all elements into a [[List]] */
inline fun <T> java.util.Iterator<T>.toList() : List<T> = this.to(ArrayList<T>()) inline fun <T> java.util.Iterator<T>.toList() : List<T> = to(ArrayList<T>())
/** Converts the collection into a Set */ /** Copies all elements into a [[Set]] */
inline fun <T> java.util.Iterator<T>.toSet() : Set<T> = this.to(HashSet<T>()) inline fun <T> java.util.Iterator<T>.toSet() : Set<T> = to(HashSet<T>())
/** Copies all elements into a [[SortedSet]] */
inline fun <T> java.util.Iterator<T>.toSortedSet() : SortedSet<T> = to(TreeSet<T>())
/** Converts the collection into a SortedSet */
inline fun <T> java.util.Iterator<T>.toSortedSet() : SortedSet<T> = this.to(TreeSet<T>())
/** /**
TODO figure out necessary variance/generics ninja stuff... :) TODO figure out necessary variance/generics ninja stuff... :)
inline fun <in T> java.util.Iterator<T>.toSortedList(transform: fun(T) : java.lang.Comparable<*>) : List<T> { inline fun <in T> java.util.Iterator<T>.toSortedList(transform: fun(T) : java.lang.Comparable<*>) : List<T> {
val answer = this.toList() val answer = this.toList()
answer.sort(transform) answer.sort(transform)
return answer return answer
} }
*/ */
@@ -1,4 +1,4 @@
// NOTE this file is auto-generated from src/kotlin/JavaCollections.kt // NOTE this file is auto-generated from stdlib/src/kotlin/JavaCollections.kt
package kotlin package kotlin
import java.util.* import java.util.*
@@ -1,4 +1,4 @@
// NOTE this file is auto-generated from src/kotlin/JavaIterables.kt // NOTE this file is auto-generated from stdlib/src/kotlin/JavaIterables.kt
package kotlin package kotlin
import kotlin.util.* import kotlin.util.*
@@ -6,229 +6,172 @@ import kotlin.util.*
import java.util.* import java.util.*
/** /**
* Returns true if any elements in the collection match the given predicate * Returns *true* if any elements match the given *predicate*
* *
* @includeFunction ../../test/CollectionTest.kt any * @includeFunction ../../test/CollectionTest.kt any
*/ */
inline fun <T> Iterable<T>.any(predicate: (T)-> Boolean) : Boolean { inline fun <T> Iterable<T>.any(predicate: (T) -> Boolean) : Boolean {
for (elem in this) { for (element in this) if (predicate(element)) return true
if (predicate(elem)) { return false
return true
}
}
return false
} }
/** /**
* Returns true if all elements in the collection match the given predicate * Returns *true* if all elements match the given *predicate*
* *
* @includeFunction ../../test/CollectionTest.kt all * @includeFunction ../../test/CollectionTest.kt all
*/ */
inline fun <T> Iterable<T>.all(predicate: (T)-> Boolean) : Boolean { inline fun <T> Iterable<T>.all(predicate: (T) -> Boolean) : Boolean {
for (elem in this) { for (element in this) if (!predicate(element)) return false
if (!predicate(elem)) { return true
return false
}
}
return true
} }
/** /**
* Returns the number of items which match the given predicate * Returns the number of elements which match the given *predicate*
* *
* @includeFunction ../../test/CollectionTest.kt count * @includeFunction ../../test/CollectionTest.kt count
*/ */
inline fun <T> Iterable<T>.count(predicate: (T)-> Boolean) : Int { inline fun <T> Iterable<T>.count(predicate: (T) -> Boolean) : Int {
var answer = 0 var count = 0
for (elem in this) { for (element in this) if (predicate(element)) count++
if (predicate(elem)) return count
answer += 1
}
return answer
} }
/** /**
* Returns the first item in the collection which matches the given predicate or null if none matched * Returns the first element which matches the given *predicate* or *null* if none matched
* *
* @includeFunction ../../test/CollectionTest.kt find * @includeFunction ../../test/CollectionTest.kt find
*/ */
inline fun <T> Iterable<T>.find(predicate: (T)-> Boolean) : T? { inline fun <T> Iterable<T>.find(predicate: (T) -> Boolean) : T? {
for (elem in this) { for (element in this) if (predicate(element)) return element
if (predicate(elem)) return null
return elem
}
return null
} }
/** /**
* Filters all elements in this collection which match the given predicate into the given result collection * Filters all elements which match the given predicate into the given list
* *
* @includeFunction ../../test/CollectionTest.kt filterIntoLinkedList * @includeFunction ../../test/CollectionTest.kt filterIntoLinkedList
*/ */
inline fun <T, C: Collection<in T>> Iterable<T>.filterTo(result: C, predicate: (T)-> Boolean) : C { inline fun <T, C: Collection<in T>> Iterable<T>.filterTo(result: C, predicate: (T) -> Boolean) : C {
for (elem in this) { for (element in this) if (predicate(element)) result.add(element)
if (predicate(elem)) return result
result.add(elem)
}
return result
} }
/** /**
* Filters all the null elements in this collection into the given result collection * Returns a list containing all elements which do not match the given *predicate*
*
* @includeFunction ../../test/CollectionTest.kt filterNotIntoLinkedList
*/
inline fun <T, L: List<in T>> Iterable<T>.filterNotTo(result: L, predicate: (T) -> Boolean) : L {
for (element in this) if (!predicate(element)) result.add(element)
return result
}
/**
* Filters all non-*null* elements into the given list
* *
* @includeFunction ../../test/CollectionTest.kt filterNotNullIntoLinkedList * @includeFunction ../../test/CollectionTest.kt filterNotNullIntoLinkedList
*/ */
inline fun <T, C: Collection<in T>> Iterable<T?>?.filterNotNullTo(result: C) : C { inline fun <T, L: List<in T>> Iterable<T?>?.filterNotNullTo(result: L) : L {
if (this != null) { if (this != null) {
for (elem in this) { for (element in this) if (element != null) result.add(element)
if (elem != null) { }
result.add(elem) return result
} }
/**
* Returns the result of transforming each element to one or more values which are concatenated together into a single list
*
* @includeFunction ../../test/CollectionTest.kt flatMap
*/
inline fun <T, R> Iterable<T>.flatMapTo(result: Collection<R>, transform: (T) -> Collection<R>) : Collection<R> {
for (element in this) {
val list = transform(element)
if (list != null) {
for (r in list) result.add(r)
} }
} }
return result return result
} }
/** /**
* Returns a new collection containing all elements in this collection which do not match the given predicate * Performs the given *operation* on each element
*
* @includeFunction ../../test/CollectionTest.kt filterNotIntoLinkedList
*/
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)
}
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
*/
// TODO * @includeFunction ../../test/CollectionTest.kt flatMapTo
inline fun <T, R> Iterable<T>.flatMapTo(result: Collection<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
* *
* @includeFunction ../../test/CollectionTest.kt forEach * @includeFunction ../../test/CollectionTest.kt forEach
*/ */
inline fun <T> Iterable<T>.forEach(operation: (element: T) -> Unit) { inline fun <T> Iterable<T>.forEach(operation: (T) -> Unit) = for (element in this) operation(element)
for (elem in this)
operation(elem)
}
/** /**
* Folds all the values from from left to right with the initial value to perform the operation on sequential pairs of values * Folds all elements from from left to right with the *initial* value to perform the operation on sequential pairs of elements
* *
* @includeFunction ../../test/CollectionTest.kt fold * @includeFunction ../../test/CollectionTest.kt fold
*/ */
inline fun <T> Iterable<T>.fold(initial: T, operation: (it: T, it2: T) -> T): T { inline fun <T> Iterable<T>.fold(initial: T, operation: (T, T) -> T): T {
var answer = initial var answer = initial
for (elem in this) { for (element in this) answer = operation(answer, element)
answer = operation(answer, elem) return answer
}
return answer
} }
/** /**
* Folds all the values from right to left with the initial value to perform the operation on sequential pairs of values * Folds all elements from right to left with the *initial* value to perform the operation on sequential pairs of elements
* *
* @includeFunction ../../test/CollectionTest.kt foldRight * @includeFunction ../../test/CollectionTest.kt foldRight
*/ */
inline fun <T> Iterable<T>.foldRight(initial: T, operation: (it: T, it2: T) -> T): T { inline fun <T> Iterable<T>.foldRight(initial: T, operation: (T, T) -> T): T = reverse().fold(initial, operation)
val reversed = this.reverse()
return reversed.fold(initial, operation)
}
/** /**
* Iterates through the collection performing the transformation on each element and using the result * Transforms each element using the result as the key in a map to group elements by the result
* as the key in a map to group elements by the result
* *
* @includeFunction ../../test/CollectionTest.kt groupBy * @includeFunction ../../test/CollectionTest.kt groupBy
*/ */
inline fun <T,K> Iterable<T>.groupBy(result: Map<K,List<T>> = HashMap<K,List<T>>(), toKey: (T)-> K) : Map<K,List<T>> { 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) { for (element in this) {
val key = toKey(elem) val key = toKey(element)
val list = result.getOrPut(key){ ArrayList<T>() } val list = result.getOrPut(key) { ArrayList<T>() }
list.add(elem) list.add(element)
} }
return result return result
} }
/** Returns a list containing the first elements that satisfy the given *predicate* */
/** inline fun <T, L: List<in T>> Iterable<T>.takeWhileTo(result: L, predicate: (T) -> Boolean) : L {
* Creates a String from all the elements in the collection, using the seperator between them and using the given prefix and postfix if supplied for (element in this) if (predicate(element)) result.add(element) else break
* return result
* @includeFunction ../../test/CollectionTest.kt join
*/
inline fun <T> 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 reversed List of this collection * Reverses the order the elements into a list
* *
* @includeFunction ../../test/CollectionTest.kt reverse * @includeFunction ../../test/CollectionTest.kt reverse
*/ */
inline fun <T> Iterable<T>.reverse() : List<T> { inline fun <T> Iterable<T>.reverse() : List<T> {
val answer = LinkedList<T>() val answer = LinkedList<T>()
for (elem in this) { for (element in this) answer.addFirst(element)
answer.addFirst(elem) return answer
}
return answer
} }
/** /** Copies all elements into the given collection */
* Copies the collection into the given collection
*
* @includeFunction ../../test/CollectionTest.kt reverse
*/
inline fun <T, C: Collection<T>> Iterable<T>.to(result: C) : C { inline fun <T, C: Collection<T>> Iterable<T>.to(result: C) : C {
for (elem in this) for (element in this) result.add(element)
result.add(elem) return result
return result
} }
/** /** Copies all elements into a [[LinkedList]] */
* Converts the collection into a LinkedList inline fun <T> Iterable<T>.toLinkedList() : LinkedList<T> = to(LinkedList<T>())
*/
inline fun <T> Iterable<T>.toLinkedList() : LinkedList<T> = this.to(LinkedList<T>())
/** Converts the collection into a List */ /** Copies all elements into a [[List]] */
inline fun <T> Iterable<T>.toList() : List<T> = this.to(ArrayList<T>()) inline fun <T> Iterable<T>.toList() : List<T> = to(ArrayList<T>())
/** Converts the collection into a Set */ /** Copies all elements into a [[Set]] */
inline fun <T> Iterable<T>.toSet() : Set<T> = this.to(HashSet<T>()) inline fun <T> Iterable<T>.toSet() : Set<T> = to(HashSet<T>())
/** Copies all elements into a [[SortedSet]] */
inline fun <T> Iterable<T>.toSortedSet() : SortedSet<T> = to(TreeSet<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... :) TODO figure out necessary variance/generics ninja stuff... :)
inline fun <in T> Iterable<T>.toSortedList(transform: fun(T) : java.lang.Comparable<*>) : List<T> { inline fun <in T> Iterable<T>.toSortedList(transform: fun(T) : java.lang.Comparable<*>) : List<T> {
val answer = this.toList() val answer = this.toList()
answer.sort(transform) answer.sort(transform)
return answer return answer
} }
*/ */
@@ -1,9 +1,11 @@
// NOTE this file is auto-generated from src/kotlin/JavaIterablesLazy.kt // NOTE this file is auto-generated from stdlib/src/kotlin/JavaIterablesLazy.kt
package kotlin package kotlin
import kotlin.util.* import kotlin.util.*
import java.util.* import java.util.ArrayList
import java.util.Collection
import java.util.List
// //
// This file contains methods which could have a lazy implementation for things like // This file contains methods which could have a lazy implementation for things like
@@ -13,33 +15,65 @@ import java.util.*
// //
/** /**
* Returns a new List containing all elements in this collection which match the given predicate * Returns a list containing all elements which match the given *predicate*
* *
* @includeFunction ../../test/CollectionTest.kt filter * @includeFunction ../../test/CollectionTest.kt filter
*/ */
inline fun <T> Iterable<T>.filter(predicate: (T)-> Boolean) : Collection<T> = filterTo(java.util.ArrayList<T>(), predicate) inline fun <T> Iterable<T>.filter(predicate: (T) -> Boolean) : List<T> = filterTo(ArrayList<T>(), predicate)
/** /**
* Returns a List containing all the non null elements in this collection * Returns a list containing all elements which do not match the given predicate
*
* @includeFunction ../../test/CollectionTest.kt filterNotNull
*/
inline fun <T> Iterable<T?>?.filterNotNull() : Collection<T> = filterNotNullTo<T, java.util.ArrayList<T>>(java.util.ArrayList<T>())
/**
* Returns a new collection containing all elements in this collection which do not match the given predicate
* *
* @includeFunction ../../test/CollectionTest.kt filterNot * @includeFunction ../../test/CollectionTest.kt filterNot
*/ */
inline fun <T> Iterable<T>.filterNot(predicate: (T)-> Boolean) : Collection<T> = filterNotTo(ArrayList<T>(), predicate) inline fun <T> Iterable<T>.filterNot(predicate: (T)-> Boolean) : List<T> = filterNotTo(ArrayList<T>(), predicate)
/** /**
* Returns the result of transforming each item in the collection to a one or more values which * Returns a list containing all the non-*null* elements
* are concatenated together into a single collection *
* @includeFunction ../../test/CollectionTest.kt filterNotNull
*/
inline fun <T> Iterable<T?>?.filterNotNull() : List<T> = filterNotNullTo<T, ArrayList<T>>(java.util.ArrayList<T>())
/**
* Returns the result of transforming each element to one or more values which are concatenated together into a single collection
* *
* @includeFunction ../../test/CollectionTest.kt flatMap * @includeFunction ../../test/CollectionTest.kt flatMap
*/ */
inline fun <T, R> Iterable<T>.flatMap(transform: (T)-> Collection<R>) : Collection<R> { inline fun <T, R> Iterable<T>.flatMap(transform: (T)-> Collection<R>) : Collection<R> = flatMapTo(ArrayList<R>(), transform)
return flatMapTo(ArrayList<R>(), transform)
/**
* Returns a list containing the first *n* elements
*
* @includeFunction ../../test/CollectionTest.kt take
*/
inline fun <T> Iterable<T>.take(n: Int): List<T> {
fun countTo(n: Int): (T) -> Boolean {
var count = 0
return { ++count; count <= n }
}
return takeWhile(countTo(n))
} }
/**
* Returns a list containing the first elements that satisfy the given *predicate*
*
* @includeFunction ../../test/CollectionTest.kt takeWhile
*/
inline fun <T> Iterable<T>.takeWhile(predicate: (T) -> Boolean): List<T> = takeWhileTo(ArrayList<T>(), predicate)
/**
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
*
* @includeFunction ../../test/CollectionTest.kt join
*/
inline fun <T> Iterable<T>.join(separator: String, prefix: String = "", postfix: String = "") : String {
val buffer = StringBuilder(prefix)
var first = true
for (element in this) {
if (first) first = false else buffer.append(separator)
buffer.append(element)
}
buffer.append(postfix)
return buffer.toString().sure()
}
+88 -145
View File
@@ -3,229 +3,172 @@ package kotlin
import java.util.* import java.util.*
/** /**
* Returns true if any elements in the collection match the given predicate * Returns *true* if any elements match the given *predicate*
* *
* @includeFunction ../../test/CollectionTest.kt any * @includeFunction ../../test/CollectionTest.kt any
*/ */
inline fun <T> java.lang.Iterable<T>.any(predicate: (T)-> Boolean) : Boolean { inline fun <T> java.lang.Iterable<T>.any(predicate: (T) -> Boolean) : Boolean {
for (elem in this) { for (element in this) if (predicate(element)) return true
if (predicate(elem)) { return false
return true
}
}
return false
} }
/** /**
* Returns true if all elements in the collection match the given predicate * Returns *true* if all elements match the given *predicate*
* *
* @includeFunction ../../test/CollectionTest.kt all * @includeFunction ../../test/CollectionTest.kt all
*/ */
inline fun <T> java.lang.Iterable<T>.all(predicate: (T)-> Boolean) : Boolean { inline fun <T> java.lang.Iterable<T>.all(predicate: (T) -> Boolean) : Boolean {
for (elem in this) { for (element in this) if (!predicate(element)) return false
if (!predicate(elem)) { return true
return false
}
}
return true
} }
/** /**
* Returns the number of items which match the given predicate * Returns the number of elements which match the given *predicate*
* *
* @includeFunction ../../test/CollectionTest.kt count * @includeFunction ../../test/CollectionTest.kt count
*/ */
inline fun <T> java.lang.Iterable<T>.count(predicate: (T)-> Boolean) : Int { inline fun <T> java.lang.Iterable<T>.count(predicate: (T) -> Boolean) : Int {
var answer = 0 var count = 0
for (elem in this) { for (element in this) if (predicate(element)) count++
if (predicate(elem)) return count
answer += 1
}
return answer
} }
/** /**
* Returns the first item in the collection which matches the given predicate or null if none matched * Returns the first element which matches the given *predicate* or *null* if none matched
* *
* @includeFunction ../../test/CollectionTest.kt find * @includeFunction ../../test/CollectionTest.kt find
*/ */
inline fun <T> java.lang.Iterable<T>.find(predicate: (T)-> Boolean) : T? { inline fun <T> java.lang.Iterable<T>.find(predicate: (T) -> Boolean) : T? {
for (elem in this) { for (element in this) if (predicate(element)) return element
if (predicate(elem)) return null
return elem
}
return null
} }
/** /**
* Filters all elements in this collection which match the given predicate into the given result collection * Filters all elements which match the given predicate into the given list
* *
* @includeFunction ../../test/CollectionTest.kt filterIntoLinkedList * @includeFunction ../../test/CollectionTest.kt filterIntoLinkedList
*/ */
inline fun <T, C: Collection<in T>> java.lang.Iterable<T>.filterTo(result: C, predicate: (T)-> Boolean) : C { inline fun <T, C: Collection<in T>> java.lang.Iterable<T>.filterTo(result: C, predicate: (T) -> Boolean) : C {
for (elem in this) { for (element in this) if (predicate(element)) result.add(element)
if (predicate(elem)) return result
result.add(elem)
}
return result
} }
/** /**
* Filters all the null elements in this collection into the given result collection * Returns a list containing all elements which do not match the given *predicate*
*
* @includeFunction ../../test/CollectionTest.kt filterNotIntoLinkedList
*/
inline fun <T, L: List<in T>> java.lang.Iterable<T>.filterNotTo(result: L, predicate: (T) -> Boolean) : L {
for (element in this) if (!predicate(element)) result.add(element)
return result
}
/**
* Filters all non-*null* elements into the given list
* *
* @includeFunction ../../test/CollectionTest.kt filterNotNullIntoLinkedList * @includeFunction ../../test/CollectionTest.kt filterNotNullIntoLinkedList
*/ */
inline fun <T, C: Collection<in T>> java.lang.Iterable<T?>?.filterNotNullTo(result: C) : C { inline fun <T, L: List<in T>> java.lang.Iterable<T?>?.filterNotNullTo(result: L) : L {
if (this != null) { if (this != null) {
for (elem in this) { for (element in this) if (element != null) result.add(element)
if (elem != null) { }
result.add(elem) return result
} }
/**
* Returns the result of transforming each element to one or more values which are concatenated together into a single list
*
* @includeFunction ../../test/CollectionTest.kt flatMap
*/
inline fun <T, R> java.lang.Iterable<T>.flatMapTo(result: Collection<R>, transform: (T) -> Collection<R>) : Collection<R> {
for (element in this) {
val list = transform(element)
if (list != null) {
for (r in list) result.add(r)
} }
} }
return result return result
} }
/** /**
* Returns a new collection containing all elements in this collection which do not match the given predicate * Performs the given *operation* on each element
*
* @includeFunction ../../test/CollectionTest.kt filterNotIntoLinkedList
*/
inline fun <T, C: Collection<in T>> java.lang.Iterable<T>.filterNotTo(result: C, predicate: (T)-> Boolean) : C {
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
*/
// TODO * @includeFunction ../../test/CollectionTest.kt flatMapTo
inline fun <T, R> java.lang.Iterable<T>.flatMapTo(result: Collection<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
* *
* @includeFunction ../../test/CollectionTest.kt forEach * @includeFunction ../../test/CollectionTest.kt forEach
*/ */
inline fun <T> java.lang.Iterable<T>.forEach(operation: (element: T) -> Unit) { inline fun <T> java.lang.Iterable<T>.forEach(operation: (T) -> Unit) = for (element in this) operation(element)
for (elem in this)
operation(elem)
}
/** /**
* Folds all the values from from left to right with the initial value to perform the operation on sequential pairs of values * Folds all elements from from left to right with the *initial* value to perform the operation on sequential pairs of elements
* *
* @includeFunction ../../test/CollectionTest.kt fold * @includeFunction ../../test/CollectionTest.kt fold
*/ */
inline fun <T> java.lang.Iterable<T>.fold(initial: T, operation: (it: T, it2: T) -> T): T { inline fun <T> java.lang.Iterable<T>.fold(initial: T, operation: (T, T) -> T): T {
var answer = initial var answer = initial
for (elem in this) { for (element in this) answer = operation(answer, element)
answer = operation(answer, elem) return answer
}
return answer
} }
/** /**
* Folds all the values from right to left with the initial value to perform the operation on sequential pairs of values * Folds all elements from right to left with the *initial* value to perform the operation on sequential pairs of elements
* *
* @includeFunction ../../test/CollectionTest.kt foldRight * @includeFunction ../../test/CollectionTest.kt foldRight
*/ */
inline fun <T> java.lang.Iterable<T>.foldRight(initial: T, operation: (it: T, it2: T) -> T): T { inline fun <T> java.lang.Iterable<T>.foldRight(initial: T, operation: (T, T) -> T): T = reverse().fold(initial, operation)
val reversed = this.reverse()
return reversed.fold(initial, operation)
}
/** /**
* Iterates through the collection performing the transformation on each element and using the result * Transforms each element using the result as the key in a map to group elements by the result
* as the key in a map to group elements by the result
* *
* @includeFunction ../../test/CollectionTest.kt groupBy * @includeFunction ../../test/CollectionTest.kt groupBy
*/ */
inline fun <T,K> java.lang.Iterable<T>.groupBy(result: Map<K,List<T>> = HashMap<K,List<T>>(), toKey: (T)-> K) : Map<K,List<T>> { inline fun <T, K> java.lang.Iterable<T>.groupBy(result: Map<K, List<T>> = HashMap<K, List<T>>(), toKey: (T) -> K) : Map<K, List<T>> {
for (elem in this) { for (element in this) {
val key = toKey(elem) val key = toKey(element)
val list = result.getOrPut(key){ ArrayList<T>() } val list = result.getOrPut(key) { ArrayList<T>() }
list.add(elem) list.add(element)
} }
return result return result
} }
/** Returns a list containing the first elements that satisfy the given *predicate* */
/** inline fun <T, L: List<in T>> java.lang.Iterable<T>.takeWhileTo(result: L, predicate: (T) -> Boolean) : L {
* Creates a String from all the elements in the collection, using the seperator between them and using the given prefix and postfix if supplied for (element in this) if (predicate(element)) result.add(element) else break
* return result
* @includeFunction ../../test/CollectionTest.kt join
*/
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 reversed List of this collection * Reverses the order the elements into a list
* *
* @includeFunction ../../test/CollectionTest.kt reverse * @includeFunction ../../test/CollectionTest.kt reverse
*/ */
inline fun <T> java.lang.Iterable<T>.reverse() : List<T> { inline fun <T> java.lang.Iterable<T>.reverse() : List<T> {
val answer = LinkedList<T>() val answer = LinkedList<T>()
for (elem in this) { for (element in this) answer.addFirst(element)
answer.addFirst(elem) return answer
}
return answer
} }
/** /** Copies all elements into the given collection */
* Copies the collection into the given collection
*
* @includeFunction ../../test/CollectionTest.kt reverse
*/
inline fun <T, C: Collection<T>> java.lang.Iterable<T>.to(result: C) : C { inline fun <T, C: Collection<T>> java.lang.Iterable<T>.to(result: C) : C {
for (elem in this) for (element in this) result.add(element)
result.add(elem) return result
return result
} }
/** /** Copies all elements into a [[LinkedList]] */
* Converts the collection into a LinkedList inline fun <T> java.lang.Iterable<T>.toLinkedList() : LinkedList<T> = to(LinkedList<T>())
*/
inline fun <T> java.lang.Iterable<T>.toLinkedList() : LinkedList<T> = this.to(LinkedList<T>())
/** Converts the collection into a List */ /** Copies all elements into a [[List]] */
inline fun <T> java.lang.Iterable<T>.toList() : List<T> = this.to(ArrayList<T>()) inline fun <T> java.lang.Iterable<T>.toList() : List<T> = to(ArrayList<T>())
/** Converts the collection into a Set */ /** Copies all elements into a [[Set]] */
inline fun <T> java.lang.Iterable<T>.toSet() : Set<T> = this.to(HashSet<T>()) inline fun <T> java.lang.Iterable<T>.toSet() : Set<T> = to(HashSet<T>())
/** Copies all elements into a [[SortedSet]] */
inline fun <T> java.lang.Iterable<T>.toSortedSet() : SortedSet<T> = to(TreeSet<T>())
/** Converts the collection into a SortedSet */
inline fun <T> java.lang.Iterable<T>.toSortedSet() : SortedSet<T> = this.to(TreeSet<T>())
/** /**
TODO figure out necessary variance/generics ninja stuff... :) 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> { inline fun <in T> java.lang.Iterable<T>.toSortedList(transform: fun(T) : java.lang.Comparable<*>) : List<T> {
val answer = this.toList() val answer = this.toList()
answer.sort(transform) answer.sort(transform)
return answer return answer
} }
*/ */
@@ -1,6 +1,8 @@
package kotlin package kotlin
import java.util.* import java.util.ArrayList
import java.util.Collection
import java.util.List
// //
// This file contains methods which could have a lazy implementation for things like // This file contains methods which could have a lazy implementation for things like
@@ -10,33 +12,65 @@ import java.util.*
// //
/** /**
* Returns a new List containing all elements in this collection which match the given predicate * Returns a list containing all elements which match the given *predicate*
* *
* @includeFunction ../../test/CollectionTest.kt filter * @includeFunction ../../test/CollectionTest.kt filter
*/ */
inline fun <T> java.lang.Iterable<T>.filter(predicate: (T)-> Boolean) : Collection<T> = filterTo(java.util.ArrayList<T>(), predicate) inline fun <T> java.lang.Iterable<T>.filter(predicate: (T) -> Boolean) : List<T> = filterTo(ArrayList<T>(), predicate)
/** /**
* Returns a List containing all the non null elements in this collection * Returns a list containing all elements which do not match the given predicate
*
* @includeFunction ../../test/CollectionTest.kt filterNotNull
*/
inline fun <T> java.lang.Iterable<T?>?.filterNotNull() : Collection<T> = filterNotNullTo<T, java.util.ArrayList<T>>(java.util.ArrayList<T>())
/**
* Returns a new collection containing all elements in this collection which do not match the given predicate
* *
* @includeFunction ../../test/CollectionTest.kt filterNot * @includeFunction ../../test/CollectionTest.kt filterNot
*/ */
inline fun <T> java.lang.Iterable<T>.filterNot(predicate: (T)-> Boolean) : Collection<T> = filterNotTo(ArrayList<T>(), predicate) inline fun <T> java.lang.Iterable<T>.filterNot(predicate: (T)-> Boolean) : List<T> = filterNotTo(ArrayList<T>(), predicate)
/** /**
* Returns the result of transforming each item in the collection to a one or more values which * Returns a list containing all the non-*null* elements
* are concatenated together into a single collection *
* @includeFunction ../../test/CollectionTest.kt filterNotNull
*/
inline fun <T> java.lang.Iterable<T?>?.filterNotNull() : List<T> = filterNotNullTo<T, ArrayList<T>>(java.util.ArrayList<T>())
/**
* Returns the result of transforming each element to one or more values which are concatenated together into a single collection
* *
* @includeFunction ../../test/CollectionTest.kt flatMap * @includeFunction ../../test/CollectionTest.kt flatMap
*/ */
inline fun <T, R> java.lang.Iterable<T>.flatMap(transform: (T)-> Collection<R>) : Collection<R> { inline fun <T, R> java.lang.Iterable<T>.flatMap(transform: (T)-> Collection<R>) : Collection<R> = flatMapTo(ArrayList<R>(), transform)
return flatMapTo(ArrayList<R>(), transform)
/**
* Returns a list containing the first *n* elements
*
* @includeFunction ../../test/CollectionTest.kt take
*/
inline fun <T> java.lang.Iterable<T>.take(n: Int): List<T> {
fun countTo(n: Int): (T) -> Boolean {
var count = 0
return { ++count; count <= n }
}
return takeWhile(countTo(n))
} }
/**
* Returns a list containing the first elements that satisfy the given *predicate*
*
* @includeFunction ../../test/CollectionTest.kt takeWhile
*/
inline fun <T> java.lang.Iterable<T>.takeWhile(predicate: (T) -> Boolean): List<T> = takeWhileTo(ArrayList<T>(), predicate)
/**
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
*
* @includeFunction ../../test/CollectionTest.kt join
*/
inline fun <T> java.lang.Iterable<T>.join(separator: String, prefix: String = "", postfix: String = "") : String {
val buffer = StringBuilder(prefix)
var first = true
for (element in this) {
if (first) first = false else buffer.append(separator)
buffer.append(element)
}
buffer.append(postfix)
return buffer.toString().sure()
}
@@ -1,6 +1,6 @@
package kotlin.support package kotlin.support
import java.util.* import java.util.NoSuchElementException
enum class State { enum class State {
Ready Ready
@@ -10,7 +10,7 @@ enum class State {
} }
/** /**
* A base class to simplify implementing iterators so that implementations only have to implement [[#computeNext()]] * A base class to simplify implementing iterators so that implementations only have to implement [[computeNext()]]
* to implement the iterator, calling [[done()]] when the iteration is complete. * to implement the iterator, calling [[done()]] when the iteration is complete.
*/ */
public abstract class AbstractIterator<T>: java.util.Iterator<T> { public abstract class AbstractIterator<T>: java.util.Iterator<T> {
@@ -27,62 +27,40 @@ public abstract class AbstractIterator<T>: java.util.Iterator<T> {
} }
override fun next(): T { override fun next(): T {
if (!hasNext()) { if (!hasNext()) throw NoSuchElementException()
throw NoSuchElementException(); state = State.NotReady
} else { return next.sure()
state = State.NotReady
return next.sure()
}
} }
override fun remove() { override fun remove() { throw UnsupportedOperationException() }
throw UnsupportedOperationException()
}
/** /** Returns the next element in the iteration without advancing the iteration */
* Returns the next element in the iteration without advancing the iteration
*/
fun peek(): T { fun peek(): T {
if (!hasNext()) { if (!hasNext()) throw NoSuchElementException()
throw NoSuchElementException();
}
return next.sure(); return next.sure();
} }
private fun tryToComputeNext(): Boolean { private fun tryToComputeNext(): Boolean {
state = State.Failed state = State.Failed
next = computeNext(); next = computeNext();
return if (state != State.Done) { return if (state != State.Done) { state = State.Ready; true } else false
state = State.Ready
true
} else false
} }
/** /** Computes the next element in the iterator, calling [[done()]] when there are no more elements */
* Computes the next element in the iterator, calling endOfData() when
* there are no more elements
*/
abstract protected fun computeNext(): T? abstract protected fun computeNext(): T?
/** /** Sets the state to done so that the iteration terminates */
* Sets the state to done so that the iteration terminates
*/
protected fun done() { protected fun done() {
state = State.Done state = State.Done
} }
} }
/** /** An [[Iterator]] which invokes a function to calculate the next value in the iteration until the function returns *null* */
* An [[Iterator]] implementation which invokes a function to calculate the next value in the iteration class FunctionIterator<T>(val nextFunction : () -> T?) : AbstractIterator<T>() {
* until the function returns null
*/
class FunctionIterator<T>(val nextFn: () -> T?) : AbstractIterator<T>() {
override fun computeNext(): T? { override protected fun computeNext(): T? {
val next = (nextFn)() val next = (nextFunction)()
if (next == null) { if (next == null) done()
done()
}
return next return next
} }
} }