added 'public' annotation to library functions

This commit is contained in:
Svetlana Isakova
2012-03-30 13:38:22 +04:00
parent 4f30e12177
commit d4d177c0ed
10 changed files with 118 additions and 118 deletions
@@ -10,7 +10,7 @@ import java.util.*
* *
* @includeFunction ../../test/CollectionTest.kt any * @includeFunction ../../test/CollectionTest.kt any
*/ */
inline fun <T> Array<T>.any(predicate: (T) -> Boolean) : Boolean { public inline fun <T> Array<T>.any(predicate: (T) -> Boolean) : Boolean {
for (element in this) if (predicate(element)) return true for (element in this) if (predicate(element)) return true
return false return false
} }
@@ -20,7 +20,7 @@ inline fun <T> Array<T>.any(predicate: (T) -> Boolean) : Boolean {
* *
* @includeFunction ../../test/CollectionTest.kt all * @includeFunction ../../test/CollectionTest.kt all
*/ */
inline fun <T> Array<T>.all(predicate: (T) -> Boolean) : Boolean { public inline fun <T> Array<T>.all(predicate: (T) -> Boolean) : Boolean {
for (element in this) if (!predicate(element)) return false for (element in this) if (!predicate(element)) return false
return true return true
} }
@@ -30,7 +30,7 @@ inline fun <T> Array<T>.all(predicate: (T) -> Boolean) : Boolean {
* *
* @includeFunction ../../test/CollectionTest.kt count * @includeFunction ../../test/CollectionTest.kt count
*/ */
inline fun <T> Array<T>.count(predicate: (T) -> Boolean) : Int { public inline fun <T> Array<T>.count(predicate: (T) -> Boolean) : Int {
var count = 0 var count = 0
for (element in this) if (predicate(element)) count++ for (element in this) if (predicate(element)) count++
return count return count
@@ -41,7 +41,7 @@ inline fun <T> Array<T>.count(predicate: (T) -> Boolean) : Int {
* *
* @includeFunction ../../test/CollectionTest.kt find * @includeFunction ../../test/CollectionTest.kt find
*/ */
inline fun <T> Array<T>.find(predicate: (T) -> Boolean) : T? { public inline fun <T> Array<T>.find(predicate: (T) -> Boolean) : T? {
for (element in this) if (predicate(element)) return element for (element in this) if (predicate(element)) return element
return null return null
} }
@@ -51,7 +51,7 @@ inline fun <T> Array<T>.find(predicate: (T) -> Boolean) : T? {
* *
* @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 { public inline fun <T, C: Collection<in T>> Array<T>.filterTo(result: C, predicate: (T) -> Boolean) : C {
for (element in this) if (predicate(element)) result.add(element) for (element in this) if (predicate(element)) result.add(element)
return result return result
} }
@@ -61,7 +61,7 @@ inline fun <T, C: Collection<in T>> Array<T>.filterTo(result: C, predicate: (T)
* *
* @includeFunction ../../test/CollectionTest.kt filterNotIntoLinkedList * @includeFunction ../../test/CollectionTest.kt filterNotIntoLinkedList
*/ */
inline fun <T, L: List<in T>> Array<T>.filterNotTo(result: L, predicate: (T) -> Boolean) : L { public 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) for (element in this) if (!predicate(element)) result.add(element)
return result return result
} }
@@ -71,7 +71,7 @@ inline fun <T, L: List<in T>> Array<T>.filterNotTo(result: L, predicate: (T) ->
* *
* @includeFunction ../../test/CollectionTest.kt filterNotNullIntoLinkedList * @includeFunction ../../test/CollectionTest.kt filterNotNullIntoLinkedList
*/ */
inline fun <T, L: List<in T>> Array<T?>?.filterNotNullTo(result: L) : L { public inline fun <T, L: List<in T>> Array<T?>?.filterNotNullTo(result: L) : L {
if (this != null) { if (this != null) {
for (element in this) if (element != null) result.add(element) for (element in this) if (element != null) result.add(element)
} }
@@ -83,7 +83,7 @@ inline fun <T, L: List<in T>> Array<T?>?.filterNotNullTo(result: L) : L {
* *
* @includeFunction ../../test/CollectionTest.kt flatMap * @includeFunction ../../test/CollectionTest.kt flatMap
*/ */
inline fun <T, R> Array<T>.flatMapTo(result: Collection<R>, transform: (T) -> Collection<R>) : Collection<R> { public inline fun <T, R> Array<T>.flatMapTo(result: Collection<R>, transform: (T) -> Collection<R>) : Collection<R> {
for (element in this) { for (element in this) {
val list = transform(element) val list = transform(element)
if (list != null) { if (list != null) {
@@ -98,14 +98,14 @@ inline fun <T, R> Array<T>.flatMapTo(result: Collection<R>, transform: (T) -> Co
* *
* @includeFunction ../../test/CollectionTest.kt forEach * @includeFunction ../../test/CollectionTest.kt forEach
*/ */
inline fun <T> Array<T>.forEach(operation: (T) -> Unit) = for (element in this) operation(element) public inline fun <T> Array<T>.forEach(operation: (T) -> Unit) : Unit = for (element in this) operation(element)
/** /**
* Folds all elements from from left to right with the *initial* value to perform the operation on sequential pairs of elements * 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: (T, T) -> T): T { public inline fun <T> Array<T>.fold(initial: T, operation: (T, T) -> T): T {
var answer = initial var answer = initial
for (element in this) answer = operation(answer, element) for (element in this) answer = operation(answer, element)
return answer return answer
@@ -116,14 +116,14 @@ inline fun <T> Array<T>.fold(initial: T, operation: (T, T) -> T): T {
* *
* @includeFunction ../../test/CollectionTest.kt foldRight * @includeFunction ../../test/CollectionTest.kt foldRight
*/ */
inline fun <T> Array<T>.foldRight(initial: T, operation: (T, T) -> T): T = reverse().fold(initial, operation) public inline fun <T> Array<T>.foldRight(initial: T, operation: (T, T) -> T): T = reverse().fold(initial, operation)
/** /**
* Transforms each element using the result as the key in a map to group elements by the result * Transforms each element using 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>> { public inline fun <T, K> Array<T>.groupBy(result: Map<K, List<T>> = HashMap<K, List<T>>(), toKey: (T) -> K) : Map<K, List<T>> {
for (element in this) { for (element in this) {
val key = toKey(element) val key = toKey(element)
val list = result.getOrPut(key) { ArrayList<T>() } val list = result.getOrPut(key) { ArrayList<T>() }
@@ -133,7 +133,7 @@ inline fun <T, K> Array<T>.groupBy(result: Map<K, List<T>> = HashMap<K, List<T>>
} }
/** Returns a list containing the first elements that satisfy the given *predicate* */ /** 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 { public inline fun <T, L: List<in T>> Array<T>.takeWhileTo(result: L, predicate: (T) -> Boolean) : L {
for (element in this) if (predicate(element)) result.add(element) else break for (element in this) if (predicate(element)) result.add(element) else break
return result return result
} }
@@ -143,33 +143,33 @@ inline fun <T, L: List<in T>> Array<T>.takeWhileTo(result: L, predicate: (T) ->
* *
* @includeFunction ../../test/CollectionTest.kt reverse * @includeFunction ../../test/CollectionTest.kt reverse
*/ */
inline fun <T> Array<T>.reverse() : List<T> { public inline fun <T> Array<T>.reverse() : List<T> {
val answer = LinkedList<T>() val answer = LinkedList<T>()
for (element in this) answer.addFirst(element) for (element in this) answer.addFirst(element)
return answer return answer
} }
/** Copies all elements into the given collection */ /** Copies all elements into the given collection */
inline fun <T, C: Collection<T>> Array<T>.to(result: C) : C { public inline fun <T, C: Collection<T>> Array<T>.to(result: C) : C {
for (element in this) result.add(element) for (element in this) result.add(element)
return result return result
} }
/** Copies all elements into a [[LinkedList]] */ /** Copies all elements into a [[LinkedList]] */
inline fun <T> Array<T>.toLinkedList() : LinkedList<T> = to(LinkedList<T>()) public inline fun <T> Array<T>.toLinkedList() : LinkedList<T> = to(LinkedList<T>())
/** Copies all elements into a [[List]] */ /** Copies all elements into a [[List]] */
inline fun <T> Array<T>.toList() : List<T> = to(ArrayList<T>()) public inline fun <T> Array<T>.toList() : List<T> = to(ArrayList<T>())
/** Copies all elements into a [[Set]] */ /** Copies all elements into a [[Set]] */
inline fun <T> Array<T>.toSet() : Set<T> = to(HashSet<T>()) public inline fun <T> Array<T>.toSet() : Set<T> = to(HashSet<T>())
/** Copies all elements into a [[SortedSet]] */ /** Copies all elements into a [[SortedSet]] */
inline fun <T> Array<T>.toSortedSet() : SortedSet<T> = to(TreeSet<T>()) public inline fun <T> Array<T>.toSortedSet() : SortedSet<T> = 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> { public 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
@@ -19,35 +19,35 @@ import java.util.List
* *
* @includeFunction ../../test/CollectionTest.kt filter * @includeFunction ../../test/CollectionTest.kt filter
*/ */
inline fun <T> Array<T>.filter(predicate: (T) -> Boolean) : List<T> = filterTo(ArrayList<T>(), predicate) public inline fun <T> Array<T>.filter(predicate: (T) -> Boolean) : List<T> = filterTo(ArrayList<T>(), predicate)
/** /**
* Returns a list containing all elements which do not match the given predicate * Returns a list containing all elements 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) : List<T> = filterNotTo(ArrayList<T>(), predicate) public inline fun <T> Array<T>.filterNot(predicate: (T)-> Boolean) : List<T> = filterNotTo(ArrayList<T>(), predicate)
/** /**
* Returns a list containing all the non-*null* elements * Returns a list containing all the non-*null* elements
* *
* @includeFunction ../../test/CollectionTest.kt filterNotNull * @includeFunction ../../test/CollectionTest.kt filterNotNull
*/ */
inline fun <T> Array<T?>?.filterNotNull() : List<T> = filterNotNullTo<T, ArrayList<T>>(java.util.ArrayList<T>()) public 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 * 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> = flatMapTo(ArrayList<R>(), transform) public inline fun <T, R> Array<T>.flatMap(transform: (T)-> Collection<R>) : Collection<R> = flatMapTo(ArrayList<R>(), transform)
/** /**
* Returns a list containing the first *n* elements * Returns a list containing the first *n* elements
* *
* @includeFunction ../../test/CollectionTest.kt take * @includeFunction ../../test/CollectionTest.kt take
*/ */
inline fun <T> Array<T>.take(n: Int): List<T> { public inline fun <T> Array<T>.take(n: Int): List<T> {
fun countTo(n: Int): (T) -> Boolean { fun countTo(n: Int): (T) -> Boolean {
var count = 0 var count = 0
return { ++count; count <= n } return { ++count; count <= n }
@@ -60,14 +60,14 @@ inline fun <T> Array<T>.take(n: Int): List<T> {
* *
* @includeFunction ../../test/CollectionTest.kt takeWhile * @includeFunction ../../test/CollectionTest.kt takeWhile
*/ */
inline fun <T> Array<T>.takeWhile(predicate: (T) -> Boolean): List<T> = takeWhileTo(ArrayList<T>(), predicate) public 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 * 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 * @includeFunction ../../test/CollectionTest.kt join
*/ */
inline fun <T> Array<T>.join(separator: String = ", ", prefix: String = "", postfix: String = "") : String { public inline fun <T> Array<T>.join(separator: String = ", ", prefix: String = "", postfix: String = "") : String {
val buffer = StringBuilder(prefix) val buffer = StringBuilder(prefix)
var first = true var first = true
for (element in this) { for (element in this) {
@@ -8,7 +8,7 @@ import java.util.*
* *
* @includeFunction ../../test/CollectionTest.kt any * @includeFunction ../../test/CollectionTest.kt any
*/ */
inline fun <T> java.util.Iterator<T>.any(predicate: (T) -> Boolean) : Boolean { public inline fun <T> java.util.Iterator<T>.any(predicate: (T) -> Boolean) : Boolean {
for (element in this) if (predicate(element)) return true for (element in this) if (predicate(element)) return true
return false return false
} }
@@ -18,7 +18,7 @@ inline fun <T> java.util.Iterator<T>.any(predicate: (T) -> Boolean) : Boolean {
* *
* @includeFunction ../../test/CollectionTest.kt all * @includeFunction ../../test/CollectionTest.kt all
*/ */
inline fun <T> java.util.Iterator<T>.all(predicate: (T) -> Boolean) : Boolean { public inline fun <T> java.util.Iterator<T>.all(predicate: (T) -> Boolean) : Boolean {
for (element in this) if (!predicate(element)) return false for (element in this) if (!predicate(element)) return false
return true return true
} }
@@ -28,7 +28,7 @@ inline fun <T> java.util.Iterator<T>.all(predicate: (T) -> Boolean) : Boolean {
* *
* @includeFunction ../../test/CollectionTest.kt count * @includeFunction ../../test/CollectionTest.kt count
*/ */
inline fun <T> java.util.Iterator<T>.count(predicate: (T) -> Boolean) : Int { public inline fun <T> java.util.Iterator<T>.count(predicate: (T) -> Boolean) : Int {
var count = 0 var count = 0
for (element in this) if (predicate(element)) count++ for (element in this) if (predicate(element)) count++
return count return count
@@ -39,7 +39,7 @@ inline fun <T> java.util.Iterator<T>.count(predicate: (T) -> Boolean) : Int {
* *
* @includeFunction ../../test/CollectionTest.kt find * @includeFunction ../../test/CollectionTest.kt find
*/ */
inline fun <T> java.util.Iterator<T>.find(predicate: (T) -> Boolean) : T? { public inline fun <T> java.util.Iterator<T>.find(predicate: (T) -> Boolean) : T? {
for (element in this) if (predicate(element)) return element for (element in this) if (predicate(element)) return element
return null return null
} }
@@ -49,7 +49,7 @@ inline fun <T> java.util.Iterator<T>.find(predicate: (T) -> Boolean) : T? {
* *
* @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 { public inline fun <T, C: Collection<in T>> java.util.Iterator<T>.filterTo(result: C, predicate: (T) -> Boolean) : C {
for (element in this) if (predicate(element)) result.add(element) for (element in this) if (predicate(element)) result.add(element)
return result return result
} }
@@ -59,7 +59,7 @@ inline fun <T, C: Collection<in T>> java.util.Iterator<T>.filterTo(result: C, pr
* *
* @includeFunction ../../test/CollectionTest.kt filterNotIntoLinkedList * @includeFunction ../../test/CollectionTest.kt filterNotIntoLinkedList
*/ */
inline fun <T, L: List<in T>> java.util.Iterator<T>.filterNotTo(result: L, predicate: (T) -> Boolean) : L { public 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) for (element in this) if (!predicate(element)) result.add(element)
return result return result
} }
@@ -69,7 +69,7 @@ inline fun <T, L: List<in T>> java.util.Iterator<T>.filterNotTo(result: L, predi
* *
* @includeFunction ../../test/CollectionTest.kt filterNotNullIntoLinkedList * @includeFunction ../../test/CollectionTest.kt filterNotNullIntoLinkedList
*/ */
inline fun <T, L: List<in T>> java.util.Iterator<T?>?.filterNotNullTo(result: L) : L { public inline fun <T, L: List<in T>> java.util.Iterator<T?>?.filterNotNullTo(result: L) : L {
if (this != null) { if (this != null) {
for (element in this) if (element != null) result.add(element) for (element in this) if (element != null) result.add(element)
} }
@@ -81,7 +81,7 @@ inline fun <T, L: List<in T>> java.util.Iterator<T?>?.filterNotNullTo(result: L)
* *
* @includeFunction ../../test/CollectionTest.kt flatMap * @includeFunction ../../test/CollectionTest.kt flatMap
*/ */
inline fun <T, R> java.util.Iterator<T>.flatMapTo(result: Collection<R>, transform: (T) -> Collection<R>) : Collection<R> { public inline fun <T, R> java.util.Iterator<T>.flatMapTo(result: Collection<R>, transform: (T) -> Collection<R>) : Collection<R> {
for (element in this) { for (element in this) {
val list = transform(element) val list = transform(element)
if (list != null) { if (list != null) {
@@ -96,14 +96,14 @@ inline fun <T, R> java.util.Iterator<T>.flatMapTo(result: Collection<R>, transfo
* *
* @includeFunction ../../test/CollectionTest.kt forEach * @includeFunction ../../test/CollectionTest.kt forEach
*/ */
inline fun <T> java.util.Iterator<T>.forEach(operation: (T) -> Unit) = for (element in this) operation(element) public inline fun <T> java.util.Iterator<T>.forEach(operation: (T) -> Unit) : Unit = for (element in this) operation(element)
/** /**
* Folds all elements from from left to right with the *initial* value to perform the operation on sequential pairs of elements * 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: (T, T) -> T): T { public inline fun <T> java.util.Iterator<T>.fold(initial: T, operation: (T, T) -> T): T {
var answer = initial var answer = initial
for (element in this) answer = operation(answer, element) for (element in this) answer = operation(answer, element)
return answer return answer
@@ -114,14 +114,14 @@ inline fun <T> java.util.Iterator<T>.fold(initial: T, operation: (T, T) -> T): T
* *
* @includeFunction ../../test/CollectionTest.kt foldRight * @includeFunction ../../test/CollectionTest.kt foldRight
*/ */
inline fun <T> java.util.Iterator<T>.foldRight(initial: T, operation: (T, T) -> T): T = reverse().fold(initial, operation) public inline fun <T> java.util.Iterator<T>.foldRight(initial: T, operation: (T, T) -> T): T = reverse().fold(initial, operation)
/** /**
* Transforms each element using the result as the key in a map to group elements by the result * Transforms each element using 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>> { public 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 (element in this) { for (element in this) {
val key = toKey(element) val key = toKey(element)
val list = result.getOrPut(key) { ArrayList<T>() } val list = result.getOrPut(key) { ArrayList<T>() }
@@ -131,7 +131,7 @@ inline fun <T, K> java.util.Iterator<T>.groupBy(result: Map<K, List<T>> = HashMa
} }
/** Returns a list containing the first elements that satisfy the given *predicate* */ /** 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 { public inline fun <T, L: List<in T>> java.util.Iterator<T>.takeWhileTo(result: L, predicate: (T) -> Boolean) : L {
for (element in this) if (predicate(element)) result.add(element) else break for (element in this) if (predicate(element)) result.add(element) else break
return result return result
} }
@@ -141,33 +141,33 @@ inline fun <T, L: List<in T>> java.util.Iterator<T>.takeWhileTo(result: L, predi
* *
* @includeFunction ../../test/CollectionTest.kt reverse * @includeFunction ../../test/CollectionTest.kt reverse
*/ */
inline fun <T> java.util.Iterator<T>.reverse() : List<T> { public inline fun <T> java.util.Iterator<T>.reverse() : List<T> {
val answer = LinkedList<T>() val answer = LinkedList<T>()
for (element in this) answer.addFirst(element) for (element in this) answer.addFirst(element)
return answer return answer
} }
/** Copies all elements into the given collection */ /** Copies all elements into the given collection */
inline fun <T, C: Collection<T>> java.util.Iterator<T>.to(result: C) : C { public inline fun <T, C: Collection<T>> java.util.Iterator<T>.to(result: C) : C {
for (element in this) result.add(element) for (element in this) result.add(element)
return result return result
} }
/** Copies all elements into a [[LinkedList]] */ /** Copies all elements into a [[LinkedList]] */
inline fun <T> java.util.Iterator<T>.toLinkedList() : LinkedList<T> = to(LinkedList<T>()) public inline fun <T> java.util.Iterator<T>.toLinkedList() : LinkedList<T> = to(LinkedList<T>())
/** Copies all elements into a [[List]] */ /** Copies all elements into a [[List]] */
inline fun <T> java.util.Iterator<T>.toList() : List<T> = to(ArrayList<T>()) public inline fun <T> java.util.Iterator<T>.toList() : List<T> = to(ArrayList<T>())
/** Copies all elements into a [[Set]] */ /** Copies all elements into a [[Set]] */
inline fun <T> java.util.Iterator<T>.toSet() : Set<T> = to(HashSet<T>()) public inline fun <T> java.util.Iterator<T>.toSet() : Set<T> = to(HashSet<T>())
/** Copies all elements into a [[SortedSet]] */ /** Copies all elements into a [[SortedSet]] */
inline fun <T> java.util.Iterator<T>.toSortedSet() : SortedSet<T> = to(TreeSet<T>()) public inline fun <T> java.util.Iterator<T>.toSortedSet() : SortedSet<T> = 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> { public 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
@@ -10,7 +10,7 @@ import java.util.*
* *
* @includeFunction ../../test/CollectionTest.kt any * @includeFunction ../../test/CollectionTest.kt any
*/ */
inline fun <T> Iterable<T>.any(predicate: (T) -> Boolean) : Boolean { public inline fun <T> Iterable<T>.any(predicate: (T) -> Boolean) : Boolean {
for (element in this) if (predicate(element)) return true for (element in this) if (predicate(element)) return true
return false return false
} }
@@ -20,7 +20,7 @@ inline fun <T> Iterable<T>.any(predicate: (T) -> Boolean) : Boolean {
* *
* @includeFunction ../../test/CollectionTest.kt all * @includeFunction ../../test/CollectionTest.kt all
*/ */
inline fun <T> Iterable<T>.all(predicate: (T) -> Boolean) : Boolean { public inline fun <T> Iterable<T>.all(predicate: (T) -> Boolean) : Boolean {
for (element in this) if (!predicate(element)) return false for (element in this) if (!predicate(element)) return false
return true return true
} }
@@ -30,7 +30,7 @@ inline fun <T> Iterable<T>.all(predicate: (T) -> Boolean) : Boolean {
* *
* @includeFunction ../../test/CollectionTest.kt count * @includeFunction ../../test/CollectionTest.kt count
*/ */
inline fun <T> Iterable<T>.count(predicate: (T) -> Boolean) : Int { public inline fun <T> Iterable<T>.count(predicate: (T) -> Boolean) : Int {
var count = 0 var count = 0
for (element in this) if (predicate(element)) count++ for (element in this) if (predicate(element)) count++
return count return count
@@ -41,7 +41,7 @@ inline fun <T> Iterable<T>.count(predicate: (T) -> Boolean) : Int {
* *
* @includeFunction ../../test/CollectionTest.kt find * @includeFunction ../../test/CollectionTest.kt find
*/ */
inline fun <T> Iterable<T>.find(predicate: (T) -> Boolean) : T? { public inline fun <T> Iterable<T>.find(predicate: (T) -> Boolean) : T? {
for (element in this) if (predicate(element)) return element for (element in this) if (predicate(element)) return element
return null return null
} }
@@ -51,7 +51,7 @@ inline fun <T> Iterable<T>.find(predicate: (T) -> Boolean) : T? {
* *
* @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 { public inline fun <T, C: Collection<in T>> Iterable<T>.filterTo(result: C, predicate: (T) -> Boolean) : C {
for (element in this) if (predicate(element)) result.add(element) for (element in this) if (predicate(element)) result.add(element)
return result return result
} }
@@ -61,7 +61,7 @@ inline fun <T, C: Collection<in T>> Iterable<T>.filterTo(result: C, predicate: (
* *
* @includeFunction ../../test/CollectionTest.kt filterNotIntoLinkedList * @includeFunction ../../test/CollectionTest.kt filterNotIntoLinkedList
*/ */
inline fun <T, L: List<in T>> Iterable<T>.filterNotTo(result: L, predicate: (T) -> Boolean) : L { public 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) for (element in this) if (!predicate(element)) result.add(element)
return result return result
} }
@@ -71,7 +71,7 @@ inline fun <T, L: List<in T>> Iterable<T>.filterNotTo(result: L, predicate: (T)
* *
* @includeFunction ../../test/CollectionTest.kt filterNotNullIntoLinkedList * @includeFunction ../../test/CollectionTest.kt filterNotNullIntoLinkedList
*/ */
inline fun <T, L: List<in T>> Iterable<T?>?.filterNotNullTo(result: L) : L { public inline fun <T, L: List<in T>> Iterable<T?>?.filterNotNullTo(result: L) : L {
if (this != null) { if (this != null) {
for (element in this) if (element != null) result.add(element) for (element in this) if (element != null) result.add(element)
} }
@@ -83,7 +83,7 @@ inline fun <T, L: List<in T>> Iterable<T?>?.filterNotNullTo(result: L) : L {
* *
* @includeFunction ../../test/CollectionTest.kt flatMap * @includeFunction ../../test/CollectionTest.kt flatMap
*/ */
inline fun <T, R> Iterable<T>.flatMapTo(result: Collection<R>, transform: (T) -> Collection<R>) : Collection<R> { public inline fun <T, R> Iterable<T>.flatMapTo(result: Collection<R>, transform: (T) -> Collection<R>) : Collection<R> {
for (element in this) { for (element in this) {
val list = transform(element) val list = transform(element)
if (list != null) { if (list != null) {
@@ -98,14 +98,14 @@ inline fun <T, R> Iterable<T>.flatMapTo(result: Collection<R>, transform: (T) ->
* *
* @includeFunction ../../test/CollectionTest.kt forEach * @includeFunction ../../test/CollectionTest.kt forEach
*/ */
inline fun <T> Iterable<T>.forEach(operation: (T) -> Unit) = for (element in this) operation(element) public inline fun <T> Iterable<T>.forEach(operation: (T) -> Unit) : Unit = for (element in this) operation(element)
/** /**
* Folds all elements from from left to right with the *initial* value to perform the operation on sequential pairs of elements * 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: (T, T) -> T): T { public inline fun <T> Iterable<T>.fold(initial: T, operation: (T, T) -> T): T {
var answer = initial var answer = initial
for (element in this) answer = operation(answer, element) for (element in this) answer = operation(answer, element)
return answer return answer
@@ -116,14 +116,14 @@ inline fun <T> Iterable<T>.fold(initial: T, operation: (T, T) -> T): T {
* *
* @includeFunction ../../test/CollectionTest.kt foldRight * @includeFunction ../../test/CollectionTest.kt foldRight
*/ */
inline fun <T> Iterable<T>.foldRight(initial: T, operation: (T, T) -> T): T = reverse().fold(initial, operation) public inline fun <T> Iterable<T>.foldRight(initial: T, operation: (T, T) -> T): T = reverse().fold(initial, operation)
/** /**
* Transforms each element using the result as the key in a map to group elements by the result * Transforms each element using 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>> { public inline fun <T, K> Iterable<T>.groupBy(result: Map<K, List<T>> = HashMap<K, List<T>>(), toKey: (T) -> K) : Map<K, List<T>> {
for (element in this) { for (element in this) {
val key = toKey(element) val key = toKey(element)
val list = result.getOrPut(key) { ArrayList<T>() } val list = result.getOrPut(key) { ArrayList<T>() }
@@ -133,7 +133,7 @@ inline fun <T, K> Iterable<T>.groupBy(result: Map<K, List<T>> = HashMap<K, List<
} }
/** Returns a list containing the first elements that satisfy the given *predicate* */ /** 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 { public inline fun <T, L: List<in T>> Iterable<T>.takeWhileTo(result: L, predicate: (T) -> Boolean) : L {
for (element in this) if (predicate(element)) result.add(element) else break for (element in this) if (predicate(element)) result.add(element) else break
return result return result
} }
@@ -143,33 +143,33 @@ inline fun <T, L: List<in T>> Iterable<T>.takeWhileTo(result: L, predicate: (T)
* *
* @includeFunction ../../test/CollectionTest.kt reverse * @includeFunction ../../test/CollectionTest.kt reverse
*/ */
inline fun <T> Iterable<T>.reverse() : List<T> { public inline fun <T> Iterable<T>.reverse() : List<T> {
val answer = LinkedList<T>() val answer = LinkedList<T>()
for (element in this) answer.addFirst(element) for (element in this) answer.addFirst(element)
return answer return answer
} }
/** Copies all elements into the given collection */ /** Copies all elements into the given collection */
inline fun <T, C: Collection<T>> Iterable<T>.to(result: C) : C { public inline fun <T, C: Collection<T>> Iterable<T>.to(result: C) : C {
for (element in this) result.add(element) for (element in this) result.add(element)
return result return result
} }
/** Copies all elements into a [[LinkedList]] */ /** Copies all elements into a [[LinkedList]] */
inline fun <T> Iterable<T>.toLinkedList() : LinkedList<T> = to(LinkedList<T>()) public inline fun <T> Iterable<T>.toLinkedList() : LinkedList<T> = to(LinkedList<T>())
/** Copies all elements into a [[List]] */ /** Copies all elements into a [[List]] */
inline fun <T> Iterable<T>.toList() : List<T> = to(ArrayList<T>()) public inline fun <T> Iterable<T>.toList() : List<T> = to(ArrayList<T>())
/** Copies all elements into a [[Set]] */ /** Copies all elements into a [[Set]] */
inline fun <T> Iterable<T>.toSet() : Set<T> = to(HashSet<T>()) public inline fun <T> Iterable<T>.toSet() : Set<T> = to(HashSet<T>())
/** Copies all elements into a [[SortedSet]] */ /** Copies all elements into a [[SortedSet]] */
inline fun <T> Iterable<T>.toSortedSet() : SortedSet<T> = to(TreeSet<T>()) public inline fun <T> Iterable<T>.toSortedSet() : SortedSet<T> = 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> { public 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
@@ -19,35 +19,35 @@ import java.util.List
* *
* @includeFunction ../../test/CollectionTest.kt filter * @includeFunction ../../test/CollectionTest.kt filter
*/ */
inline fun <T> Iterable<T>.filter(predicate: (T) -> Boolean) : List<T> = filterTo(ArrayList<T>(), predicate) public inline fun <T> Iterable<T>.filter(predicate: (T) -> Boolean) : List<T> = filterTo(ArrayList<T>(), predicate)
/** /**
* Returns a list containing all elements which do not match the given predicate * Returns a list containing all elements 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) : List<T> = filterNotTo(ArrayList<T>(), predicate) public inline fun <T> Iterable<T>.filterNot(predicate: (T)-> Boolean) : List<T> = filterNotTo(ArrayList<T>(), predicate)
/** /**
* Returns a list containing all the non-*null* elements * Returns a list containing all the non-*null* elements
* *
* @includeFunction ../../test/CollectionTest.kt filterNotNull * @includeFunction ../../test/CollectionTest.kt filterNotNull
*/ */
inline fun <T> Iterable<T?>?.filterNotNull() : List<T> = filterNotNullTo<T, ArrayList<T>>(java.util.ArrayList<T>()) public 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 * 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> = flatMapTo(ArrayList<R>(), transform) public inline fun <T, R> Iterable<T>.flatMap(transform: (T)-> Collection<R>) : Collection<R> = flatMapTo(ArrayList<R>(), transform)
/** /**
* Returns a list containing the first *n* elements * Returns a list containing the first *n* elements
* *
* @includeFunction ../../test/CollectionTest.kt take * @includeFunction ../../test/CollectionTest.kt take
*/ */
inline fun <T> Iterable<T>.take(n: Int): List<T> { public inline fun <T> Iterable<T>.take(n: Int): List<T> {
fun countTo(n: Int): (T) -> Boolean { fun countTo(n: Int): (T) -> Boolean {
var count = 0 var count = 0
return { ++count; count <= n } return { ++count; count <= n }
@@ -60,14 +60,14 @@ inline fun <T> Iterable<T>.take(n: Int): List<T> {
* *
* @includeFunction ../../test/CollectionTest.kt takeWhile * @includeFunction ../../test/CollectionTest.kt takeWhile
*/ */
inline fun <T> Iterable<T>.takeWhile(predicate: (T) -> Boolean): List<T> = takeWhileTo(ArrayList<T>(), predicate) public 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 * 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 * @includeFunction ../../test/CollectionTest.kt join
*/ */
inline fun <T> Iterable<T>.join(separator: String = ", ", prefix: String = "", postfix: String = "") : String { public inline fun <T> Iterable<T>.join(separator: String = ", ", prefix: String = "", postfix: String = "") : String {
val buffer = StringBuilder(prefix) val buffer = StringBuilder(prefix)
var first = true var first = true
for (element in this) { for (element in this) {
+1 -1
View File
@@ -99,7 +99,7 @@ public inline fun CharArray.copyOfRange(from: Int, to: Int) : CharArray =
public inline fun <T> Array<T>.copyOfRange(from: Int, to: Int) : Array<T> = Arrays.copyOfRange(this as Array<T?>, from, to) as Array<T> public inline fun <T> Array<T>.copyOfRange(from: Int, to: Int) : Array<T> = Arrays.copyOfRange(this as Array<T?>, from, to) as Array<T>
inline val ByteArray.inputStream : ByteArrayInputStream public inline val ByteArray.inputStream : ByteArrayInputStream
get() = ByteArrayInputStream(this) get() = ByteArrayInputStream(this)
public inline fun ByteArray.inputStream(offset: Int, length: Int) : ByteArrayInputStream = ByteArrayInputStream(this, offset, length) public inline fun ByteArray.inputStream(offset: Int, length: Int) : ByteArrayInputStream = ByteArrayInputStream(this, offset, length)
+10 -10
View File
@@ -8,10 +8,10 @@ import kotlin.support.FunctionIterator
* *
* @includeFunction ../../test/iterators/IteratorsTest.kt fibonacci * @includeFunction ../../test/iterators/IteratorsTest.kt fibonacci
*/ */
inline fun <T> iterate(nextFunction: () -> T?) : java.util.Iterator<T> = FunctionIterator(nextFunction) public inline fun <T> iterate(nextFunction: () -> T?) : java.util.Iterator<T> = FunctionIterator(nextFunction)
/** Returns an iterator over elements that are instances of a given type *R* which is a subclass of *T* */ /** Returns an iterator over elements that are instances of a given type *R* which is a subclass of *T* */
inline fun <T, R: T> java.util.Iterator<T>.filterIsInstance(klass: Class<R>): java.util.Iterator<R> = FilterIsIterator<T,R>(this, klass) public inline fun <T, R: T> java.util.Iterator<T>.filterIsInstance(klass: Class<R>): java.util.Iterator<R> = FilterIsIterator<T,R>(this, klass)
private class FilterIsIterator<T, R :T>(val iterator : java.util.Iterator<T>, val klass: Class<R>) : AbstractIterator<R>() { private class FilterIsIterator<T, R :T>(val iterator : java.util.Iterator<T>, val klass: Class<R>) : AbstractIterator<R>() {
override protected fun computeNext(): R? { override protected fun computeNext(): R? {
@@ -29,7 +29,7 @@ private class FilterIsIterator<T, R :T>(val iterator : java.util.Iterator<T>, va
* *
* @includeFunction ../../test/iterators/IteratorsTest.kt filterAndTakeWhileExtractTheElementsWithinRange * @includeFunction ../../test/iterators/IteratorsTest.kt filterAndTakeWhileExtractTheElementsWithinRange
*/ */
inline fun <T> java.util.Iterator<T>.filter(predicate: (T) -> Boolean) : java.util.Iterator<T> = FilterIterator<T>(this, predicate) public inline fun <T> java.util.Iterator<T>.filter(predicate: (T) -> Boolean) : java.util.Iterator<T> = FilterIterator<T>(this, predicate)
private class FilterIterator<T>(val iterator : java.util.Iterator<T>, val predicate: (T)-> Boolean) : AbstractIterator<T>() { private class FilterIterator<T>(val iterator : java.util.Iterator<T>, val predicate: (T)-> Boolean) : AbstractIterator<T>() {
override protected fun computeNext(): T? { override protected fun computeNext(): T? {
@@ -43,10 +43,10 @@ private class FilterIterator<T>(val iterator : java.util.Iterator<T>, val predic
} }
/** Returns an iterator over elements which do not match the given *predicate* */ /** Returns an iterator over elements which do not match the given *predicate* */
inline fun <T> java.util.Iterator<T>.filterNot(predicate: (T) -> Boolean) : java.util.Iterator<T> = filter { !predicate(it) } public inline fun <T> java.util.Iterator<T>.filterNot(predicate: (T) -> Boolean) : java.util.Iterator<T> = filter { !predicate(it) }
/** Returns an iterator over non-*null* elements */ /** Returns an iterator over non-*null* elements */
inline fun <T> java.util.Iterator<T?>?.filterNotNull() : java.util.Iterator<T> = FilterNotNullIterator(this) public inline fun <T> java.util.Iterator<T?>?.filterNotNull() : java.util.Iterator<T> = FilterNotNullIterator(this)
private class FilterNotNullIterator<T>(val iterator : java.util.Iterator<T?>?) : AbstractIterator<T>() { private class FilterNotNullIterator<T>(val iterator : java.util.Iterator<T?>?) : AbstractIterator<T>() {
override protected fun computeNext(): T? { override protected fun computeNext(): T? {
@@ -66,7 +66,7 @@ private class FilterNotNullIterator<T>(val iterator : java.util.Iterator<T?>?) :
* *
* @includeFunction ../../test/iterators/IteratorsTest.kt mapAndTakeWhileExtractTheTransformedElements * @includeFunction ../../test/iterators/IteratorsTest.kt mapAndTakeWhileExtractTheTransformedElements
*/ */
inline fun <T, R> java.util.Iterator<T>.map(transform: (T) -> R): java.util.Iterator<R> = MapIterator<T, R>(this, transform) public inline fun <T, R> java.util.Iterator<T>.map(transform: (T) -> R): java.util.Iterator<R> = MapIterator<T, R>(this, transform)
private class MapIterator<T, R>(val iterator : java.util.Iterator<T>, val transform: (T) -> R) : AbstractIterator<R>() { private class MapIterator<T, R>(val iterator : java.util.Iterator<T>, val transform: (T) -> R) : AbstractIterator<R>() {
override protected fun computeNext() : R? = if (iterator.hasNext()) (transform)(iterator.next()) else { done(); null } override protected fun computeNext() : R? = if (iterator.hasNext()) (transform)(iterator.next()) else { done(); null }
@@ -77,7 +77,7 @@ private class MapIterator<T, R>(val iterator : java.util.Iterator<T>, val transf
* *
* @includeFunction ../../test/iterators/IteratorsTest.kt flatMapAndTakeExtractTheTransformedElements * @includeFunction ../../test/iterators/IteratorsTest.kt flatMapAndTakeExtractTheTransformedElements
*/ */
inline fun <T, R> java.util.Iterator<T>.flatMap(transform: (T) -> java.util.Iterator<R>): java.util.Iterator<R> = FlatMapIterator<T, R>(this, transform) public inline fun <T, R> java.util.Iterator<T>.flatMap(transform: (T) -> java.util.Iterator<R>): java.util.Iterator<R> = FlatMapIterator<T, R>(this, transform)
private class FlatMapIterator<T, R>(val iterator : java.util.Iterator<T>, val transform: (T) -> java.util.Iterator<R>) : AbstractIterator<R>() { private class FlatMapIterator<T, R>(val iterator : java.util.Iterator<T>, val transform: (T) -> java.util.Iterator<R>) : AbstractIterator<R>() {
var transformed: java.util.Iterator<R> = iterate<R> { null } var transformed: java.util.Iterator<R> = iterate<R> { null }
@@ -98,7 +98,7 @@ private class FlatMapIterator<T, R>(val iterator : java.util.Iterator<T>, val tr
* *
* @includeFunction ../../test/iterators/IteratorsTest.kt takeExtractsTheFirstNElements * @includeFunction ../../test/iterators/IteratorsTest.kt takeExtractsTheFirstNElements
*/ */
inline fun <T> java.util.Iterator<T>.take(n: Int): java.util.Iterator<T> { public inline fun <T> java.util.Iterator<T>.take(n: Int): java.util.Iterator<T> {
fun countTo(n: Int): (T) -> Boolean { fun countTo(n: Int): (T) -> Boolean {
var count = 0 var count = 0
return { ++count; count <= n } return { ++count; count <= n }
@@ -111,7 +111,7 @@ inline fun <T> java.util.Iterator<T>.take(n: Int): java.util.Iterator<T> {
* *
* @includeFunction ../../test/iterators/IteratorsTest.kt filterAndTakeWhileExtractTheElementsWithinRange * @includeFunction ../../test/iterators/IteratorsTest.kt filterAndTakeWhileExtractTheElementsWithinRange
*/ */
inline fun <T> java.util.Iterator<T>.takeWhile(predicate: (T) -> Boolean): java.util.Iterator<T> = TakeWhileIterator<T>(this, predicate) public inline fun <T> java.util.Iterator<T>.takeWhile(predicate: (T) -> Boolean): java.util.Iterator<T> = TakeWhileIterator<T>(this, predicate)
private class TakeWhileIterator<T>(val iterator: java.util.Iterator<T>, val predicate: (T) -> Boolean) : AbstractIterator<T>() { private class TakeWhileIterator<T>(val iterator: java.util.Iterator<T>, val predicate: (T) -> Boolean) : AbstractIterator<T>() {
override protected fun computeNext() : T? { override protected fun computeNext() : T? {
@@ -129,7 +129,7 @@ private class TakeWhileIterator<T>(val iterator: java.util.Iterator<T>, val pred
* *
* @includeFunction ../../test/iterators/IteratorsTest.kt joinConcatenatesTheFirstNElementsAboveAThreshold * @includeFunction ../../test/iterators/IteratorsTest.kt joinConcatenatesTheFirstNElementsAboveAThreshold
*/ */
inline fun <T> java.util.Iterator<T>.join(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int? = null) : String { public inline fun <T> java.util.Iterator<T>.join(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int? = null) : String {
val buffer = StringBuilder(prefix) val buffer = StringBuilder(prefix)
var first = true; var count = 0 var first = true; var count = 0
for (element in this) { for (element in this) {
+20 -20
View File
@@ -7,7 +7,7 @@ import java.util.*
* *
* @includeFunction ../../test/CollectionTest.kt any * @includeFunction ../../test/CollectionTest.kt any
*/ */
inline fun <T> java.lang.Iterable<T>.any(predicate: (T) -> Boolean) : Boolean { public inline fun <T> java.lang.Iterable<T>.any(predicate: (T) -> Boolean) : Boolean {
for (element in this) if (predicate(element)) return true for (element in this) if (predicate(element)) return true
return false return false
} }
@@ -17,7 +17,7 @@ inline fun <T> java.lang.Iterable<T>.any(predicate: (T) -> Boolean) : Boolean {
* *
* @includeFunction ../../test/CollectionTest.kt all * @includeFunction ../../test/CollectionTest.kt all
*/ */
inline fun <T> java.lang.Iterable<T>.all(predicate: (T) -> Boolean) : Boolean { public inline fun <T> java.lang.Iterable<T>.all(predicate: (T) -> Boolean) : Boolean {
for (element in this) if (!predicate(element)) return false for (element in this) if (!predicate(element)) return false
return true return true
} }
@@ -27,7 +27,7 @@ inline fun <T> java.lang.Iterable<T>.all(predicate: (T) -> Boolean) : Boolean {
* *
* @includeFunction ../../test/CollectionTest.kt count * @includeFunction ../../test/CollectionTest.kt count
*/ */
inline fun <T> java.lang.Iterable<T>.count(predicate: (T) -> Boolean) : Int { public inline fun <T> java.lang.Iterable<T>.count(predicate: (T) -> Boolean) : Int {
var count = 0 var count = 0
for (element in this) if (predicate(element)) count++ for (element in this) if (predicate(element)) count++
return count return count
@@ -38,7 +38,7 @@ inline fun <T> java.lang.Iterable<T>.count(predicate: (T) -> Boolean) : Int {
* *
* @includeFunction ../../test/CollectionTest.kt find * @includeFunction ../../test/CollectionTest.kt find
*/ */
inline fun <T> java.lang.Iterable<T>.find(predicate: (T) -> Boolean) : T? { public inline fun <T> java.lang.Iterable<T>.find(predicate: (T) -> Boolean) : T? {
for (element in this) if (predicate(element)) return element for (element in this) if (predicate(element)) return element
return null return null
} }
@@ -48,7 +48,7 @@ inline fun <T> java.lang.Iterable<T>.find(predicate: (T) -> Boolean) : T? {
* *
* @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 { public inline fun <T, C: Collection<in T>> java.lang.Iterable<T>.filterTo(result: C, predicate: (T) -> Boolean) : C {
for (element in this) if (predicate(element)) result.add(element) for (element in this) if (predicate(element)) result.add(element)
return result return result
} }
@@ -58,7 +58,7 @@ inline fun <T, C: Collection<in T>> java.lang.Iterable<T>.filterTo(result: C, pr
* *
* @includeFunction ../../test/CollectionTest.kt filterNotIntoLinkedList * @includeFunction ../../test/CollectionTest.kt filterNotIntoLinkedList
*/ */
inline fun <T, L: List<in T>> java.lang.Iterable<T>.filterNotTo(result: L, predicate: (T) -> Boolean) : L { public 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) for (element in this) if (!predicate(element)) result.add(element)
return result return result
} }
@@ -68,7 +68,7 @@ inline fun <T, L: List<in T>> java.lang.Iterable<T>.filterNotTo(result: L, predi
* *
* @includeFunction ../../test/CollectionTest.kt filterNotNullIntoLinkedList * @includeFunction ../../test/CollectionTest.kt filterNotNullIntoLinkedList
*/ */
inline fun <T, L: List<in T>> java.lang.Iterable<T?>?.filterNotNullTo(result: L) : L { public inline fun <T, L: List<in T>> java.lang.Iterable<T?>?.filterNotNullTo(result: L) : L {
if (this != null) { if (this != null) {
for (element in this) if (element != null) result.add(element) for (element in this) if (element != null) result.add(element)
} }
@@ -80,7 +80,7 @@ inline fun <T, L: List<in T>> java.lang.Iterable<T?>?.filterNotNullTo(result: L)
* *
* @includeFunction ../../test/CollectionTest.kt flatMap * @includeFunction ../../test/CollectionTest.kt flatMap
*/ */
inline fun <T, R> java.lang.Iterable<T>.flatMapTo(result: Collection<R>, transform: (T) -> Collection<R>) : Collection<R> { public inline fun <T, R> java.lang.Iterable<T>.flatMapTo(result: Collection<R>, transform: (T) -> Collection<R>) : Collection<R> {
for (element in this) { for (element in this) {
val list = transform(element) val list = transform(element)
if (list != null) { if (list != null) {
@@ -95,14 +95,14 @@ inline fun <T, R> java.lang.Iterable<T>.flatMapTo(result: Collection<R>, transfo
* *
* @includeFunction ../../test/CollectionTest.kt forEach * @includeFunction ../../test/CollectionTest.kt forEach
*/ */
inline fun <T> java.lang.Iterable<T>.forEach(operation: (T) -> Unit) = for (element in this) operation(element) public inline fun <T> java.lang.Iterable<T>.forEach(operation: (T) -> Unit) : Unit = for (element in this) operation(element)
/** /**
* Folds all elements from from left to right with the *initial* value to perform the operation on sequential pairs of elements * 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: (T, T) -> T): T { public inline fun <T> java.lang.Iterable<T>.fold(initial: T, operation: (T, T) -> T): T {
var answer = initial var answer = initial
for (element in this) answer = operation(answer, element) for (element in this) answer = operation(answer, element)
return answer return answer
@@ -113,14 +113,14 @@ inline fun <T> java.lang.Iterable<T>.fold(initial: T, operation: (T, T) -> T): T
* *
* @includeFunction ../../test/CollectionTest.kt foldRight * @includeFunction ../../test/CollectionTest.kt foldRight
*/ */
inline fun <T> java.lang.Iterable<T>.foldRight(initial: T, operation: (T, T) -> T): T = reverse().fold(initial, operation) public inline fun <T> java.lang.Iterable<T>.foldRight(initial: T, operation: (T, T) -> T): T = reverse().fold(initial, operation)
/** /**
* Transforms each element using the result as the key in a map to group elements by the result * Transforms each element using 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>> { public 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 (element in this) { for (element in this) {
val key = toKey(element) val key = toKey(element)
val list = result.getOrPut(key) { ArrayList<T>() } val list = result.getOrPut(key) { ArrayList<T>() }
@@ -130,7 +130,7 @@ inline fun <T, K> java.lang.Iterable<T>.groupBy(result: Map<K, List<T>> = HashMa
} }
/** Returns a list containing the first elements that satisfy the given *predicate* */ /** 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 { public inline fun <T, L: List<in T>> java.lang.Iterable<T>.takeWhileTo(result: L, predicate: (T) -> Boolean) : L {
for (element in this) if (predicate(element)) result.add(element) else break for (element in this) if (predicate(element)) result.add(element) else break
return result return result
} }
@@ -140,33 +140,33 @@ inline fun <T, L: List<in T>> java.lang.Iterable<T>.takeWhileTo(result: L, predi
* *
* @includeFunction ../../test/CollectionTest.kt reverse * @includeFunction ../../test/CollectionTest.kt reverse
*/ */
inline fun <T> java.lang.Iterable<T>.reverse() : List<T> { public inline fun <T> java.lang.Iterable<T>.reverse() : List<T> {
val answer = LinkedList<T>() val answer = LinkedList<T>()
for (element in this) answer.addFirst(element) for (element in this) answer.addFirst(element)
return answer return answer
} }
/** Copies all elements into the given collection */ /** Copies all elements into the given collection */
inline fun <T, C: Collection<T>> java.lang.Iterable<T>.to(result: C) : C { public inline fun <T, C: Collection<T>> java.lang.Iterable<T>.to(result: C) : C {
for (element in this) result.add(element) for (element in this) result.add(element)
return result return result
} }
/** Copies all elements into a [[LinkedList]] */ /** Copies all elements into a [[LinkedList]] */
inline fun <T> java.lang.Iterable<T>.toLinkedList() : LinkedList<T> = to(LinkedList<T>()) public inline fun <T> java.lang.Iterable<T>.toLinkedList() : LinkedList<T> = to(LinkedList<T>())
/** Copies all elements into a [[List]] */ /** Copies all elements into a [[List]] */
inline fun <T> java.lang.Iterable<T>.toList() : List<T> = to(ArrayList<T>()) public inline fun <T> java.lang.Iterable<T>.toList() : List<T> = to(ArrayList<T>())
/** Copies all elements into a [[Set]] */ /** Copies all elements into a [[Set]] */
inline fun <T> java.lang.Iterable<T>.toSet() : Set<T> = to(HashSet<T>()) public inline fun <T> java.lang.Iterable<T>.toSet() : Set<T> = to(HashSet<T>())
/** Copies all elements into a [[SortedSet]] */ /** Copies all elements into a [[SortedSet]] */
inline fun <T> java.lang.Iterable<T>.toSortedSet() : SortedSet<T> = to(TreeSet<T>()) public inline fun <T> java.lang.Iterable<T>.toSortedSet() : SortedSet<T> = 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> { public 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
@@ -16,35 +16,35 @@ import java.util.List
* *
* @includeFunction ../../test/CollectionTest.kt filter * @includeFunction ../../test/CollectionTest.kt filter
*/ */
inline fun <T> java.lang.Iterable<T>.filter(predicate: (T) -> Boolean) : List<T> = filterTo(ArrayList<T>(), predicate) public inline fun <T> java.lang.Iterable<T>.filter(predicate: (T) -> Boolean) : List<T> = filterTo(ArrayList<T>(), predicate)
/** /**
* Returns a list containing all elements which do not match the given predicate * Returns a list containing all elements 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) : List<T> = filterNotTo(ArrayList<T>(), predicate) public inline fun <T> java.lang.Iterable<T>.filterNot(predicate: (T)-> Boolean) : List<T> = filterNotTo(ArrayList<T>(), predicate)
/** /**
* Returns a list containing all the non-*null* elements * Returns a list containing all the non-*null* elements
* *
* @includeFunction ../../test/CollectionTest.kt filterNotNull * @includeFunction ../../test/CollectionTest.kt filterNotNull
*/ */
inline fun <T> java.lang.Iterable<T?>?.filterNotNull() : List<T> = filterNotNullTo<T, ArrayList<T>>(java.util.ArrayList<T>()) public 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 * 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> = flatMapTo(ArrayList<R>(), transform) public inline fun <T, R> java.lang.Iterable<T>.flatMap(transform: (T)-> Collection<R>) : Collection<R> = flatMapTo(ArrayList<R>(), transform)
/** /**
* Returns a list containing the first *n* elements * Returns a list containing the first *n* elements
* *
* @includeFunction ../../test/CollectionTest.kt take * @includeFunction ../../test/CollectionTest.kt take
*/ */
inline fun <T> java.lang.Iterable<T>.take(n: Int): List<T> { public inline fun <T> java.lang.Iterable<T>.take(n: Int): List<T> {
fun countTo(n: Int): (T) -> Boolean { fun countTo(n: Int): (T) -> Boolean {
var count = 0 var count = 0
return { ++count; count <= n } return { ++count; count <= n }
@@ -57,14 +57,14 @@ inline fun <T> java.lang.Iterable<T>.take(n: Int): List<T> {
* *
* @includeFunction ../../test/CollectionTest.kt takeWhile * @includeFunction ../../test/CollectionTest.kt takeWhile
*/ */
inline fun <T> java.lang.Iterable<T>.takeWhile(predicate: (T) -> Boolean): List<T> = takeWhileTo(ArrayList<T>(), predicate) public 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 * 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 * @includeFunction ../../test/CollectionTest.kt join
*/ */
inline fun <T> java.lang.Iterable<T>.join(separator: String = ", ", prefix: String = "", postfix: String = "") : String { public inline fun <T> java.lang.Iterable<T>.join(separator: String = ", ", prefix: String = "", postfix: String = "") : String {
val buffer = StringBuilder(prefix) val buffer = StringBuilder(prefix)
var first = true var first = true
for (element in this) { for (element in this) {
@@ -2,7 +2,7 @@ package kotlin.support
import java.util.NoSuchElementException import java.util.NoSuchElementException
enum class State { public enum class State {
Ready Ready
NotReady NotReady
Done Done
@@ -14,10 +14,10 @@ enum class State {
* 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> {
var state: State = State.NotReady public var state: State = State.NotReady
var next: T? = null public var next: T? = null
override fun hasNext(): Boolean { public override fun hasNext(): Boolean {
require(state != State.Failed) require(state != State.Failed)
return when (state) { return when (state) {
State.Done -> false State.Done -> false
@@ -26,13 +26,13 @@ public abstract class AbstractIterator<T>: java.util.Iterator<T> {
} }
} }
override fun next(): T { public override fun next(): T {
if (!hasNext()) throw NoSuchElementException() if (!hasNext()) throw NoSuchElementException()
state = State.NotReady state = State.NotReady
return next.sure() return next.sure()
} }
override fun remove() { throw UnsupportedOperationException() } public override fun remove() { 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 {