added more links to test code inside the kdoc and tidied up the tests a little so they look a bit nicer and more self contained in the documentation

This commit is contained in:
James Strachan
2012-03-27 11:33:05 +01:00
parent 92a1cde223
commit 31fd665913
2 changed files with 144 additions and 48 deletions
+64 -17
View File
@@ -16,7 +16,11 @@ inline fun <T> java.lang.Iterable<T>.any(predicate: (T)-> Boolean) : Boolean {
return false
}
/** Returns true if all elements in the collection match the given predicate */
/**
* Returns true if all elements in the collection match the given predicate
*
* @includeFunction ../../test/CollectionTest.kt all
*/
inline fun <T> java.lang.Iterable<T>.all(predicate: (T)-> Boolean) : Boolean {
for (elem in this) {
if (!predicate(elem)) {
@@ -26,7 +30,11 @@ inline fun <T> java.lang.Iterable<T>.all(predicate: (T)-> Boolean) : Boolean {
return true
}
/** Returns the number of items which match the given predicate */
/**
* Returns the number of items which match the given predicate
*
* @includeFunction ../../test/CollectionTest.kt count
*/
inline fun <T> java.lang.Iterable<T>.count(predicate: (T)-> Boolean) : Int {
var answer = 0
for (elem in this) {
@@ -36,7 +44,11 @@ inline fun <T> java.lang.Iterable<T>.count(predicate: (T)-> Boolean) : Int {
return answer
}
/** Returns the first item in the collection which matches the given predicate or null if none matched */
/**
* Returns the first item in the collection which matches the given predicate or null if none matched
*
* @includeFunction ../../test/CollectionTest.kt find
*/
inline fun <T> java.lang.Iterable<T>.find(predicate: (T)-> Boolean) : T? {
for (elem in this) {
if (predicate(elem))
@@ -45,7 +57,11 @@ inline fun <T> java.lang.Iterable<T>.find(predicate: (T)-> Boolean) : T? {
return null
}
/** Filters all elements in this collection which match the given predicate into the given result collection */
/**
* Filters all elements in this collection which match the given predicate into the given result collection
*
* @includeFunction ../../test/CollectionTest.kt filterIntoLinkedList
*/
inline fun <T, C: Collection<in T>> java.lang.Iterable<T>.filterTo(result: C, predicate: (T)-> Boolean) : C {
for (elem in this) {
if (predicate(elem))
@@ -54,18 +70,27 @@ inline fun <T, C: Collection<in T>> java.lang.Iterable<T>.filterTo(result: C, pr
return result
}
/** Filters all the null elements in this collection into the given result collection */
/**
* Filters all the null elements in this collection into the given result collection
*
* @includeFunction ../../test/CollectionTest.kt filterNotNullIntoLinkedList
*/
inline fun <T, C: Collection<in T>> java.lang.Iterable<T?>?.filterNotNullTo(result: C) : C {
if (this != null) {
for (elem in this) {
if (elem != null)
if (elem != null) {
result.add(elem)
}
}
}
return result
}
/** Returns a new collection containing all elements in this collection which do not match the given predicate */
/**
* Returns a new collection containing all elements in this collection which do not match the given predicate
*
* @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))
@@ -75,9 +100,10 @@ inline fun <T, C: Collection<in T>> java.lang.Iterable<T>.filterNotTo(result: C,
}
/**
* Returns the result of transforming each item in the collection to a one or more values which
* are concatenated together into a single collection
*/
* 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)
@@ -90,7 +116,11 @@ inline fun <T, R> java.lang.Iterable<T>.flatMapTo(result: Collection<R>, transfo
return result
}
/** Performs the given operation on each element inside the collection */
/**
* Performs the given operation on each element inside the collection
*
* @includeFunction ../../test/CollectionTest.kt forEach
*/
inline fun <T> java.lang.Iterable<T>.forEach(operation: (element: T) -> Unit) {
for (elem in this)
operation(elem)
@@ -99,8 +129,7 @@ inline fun <T> java.lang.Iterable<T>.forEach(operation: (element: T) -> Unit) {
/**
* Folds all the values from from left to right with the initial value to perform the operation on sequential pairs of values
*
* For example to sum together all numeric values in a collection of numbers it would be
* {code}val total = numbers.fold(0){(a, b) -> a + b}{code}
* @includeFunction ../../test/CollectionTest.kt fold
*/
inline fun <T> java.lang.Iterable<T>.fold(initial: T, operation: (it: T, it2: T) -> T): T {
var answer = initial
@@ -112,6 +141,8 @@ inline fun <T> java.lang.Iterable<T>.fold(initial: T, operation: (it: T, it2: T)
/**
* Folds all the values from right to left with the initial value to perform the operation on sequential pairs of values
*
* @includeFunction ../../test/CollectionTest.kt foldRight
*/
inline fun <T> java.lang.Iterable<T>.foldRight(initial: T, operation: (it: T, it2: T) -> T): T {
val reversed = this.reverse()
@@ -121,6 +152,8 @@ inline fun <T> java.lang.Iterable<T>.foldRight(initial: T, operation: (it: T, it
/**
* Iterates through the collection performing the transformation on each element and using the result
* as the key in a map to group elements by the result
*
* @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>> {
for (elem in this) {
@@ -132,7 +165,11 @@ inline fun <T,K> java.lang.Iterable<T>.groupBy(result: Map<K,List<T>> = HashMap<
}
/** Creates a String from all the elements in the collection, using the seperator between them and using the given prefix and postfix if supplied */
/**
* Creates a String from all the elements in the collection, using the seperator between them 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
@@ -147,7 +184,11 @@ inline fun <T> java.lang.Iterable<T>.join(separator: String, prefix: String = ""
return buffer.toString().sure()
}
/** Returns a reversed List of this collection */
/**
* Returns a reversed List of this collection
*
* @includeFunction ../../test/CollectionTest.kt reverse
*/
inline fun <T> java.lang.Iterable<T>.reverse() : List<T> {
val answer = LinkedList<T>()
for (elem in this) {
@@ -156,14 +197,20 @@ inline fun <T> java.lang.Iterable<T>.reverse() : List<T> {
return answer
}
/** Copies the collection 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 {
for (elem in this)
result.add(elem)
return result
}
/** Converts the collection into a LinkedList */
/**
* Converts the collection into a LinkedList
*/
inline fun <T> java.lang.Iterable<T>.toLinkedList() : LinkedList<T> = this.to(LinkedList<T>())
/** Converts the collection into a List */