split the filter method into filter(predicate) and filterTo(result, predicate) for easier completion & avoid confusion - seems 2 methods is better than the default arguments approach
This commit is contained in:
@@ -8,17 +8,6 @@ Filters given iterator
|
|||||||
*/
|
*/
|
||||||
inline fun <T> java.util.Iterator<T>.filter(f: (T)-> Boolean) : java.util.Iterator<T> = FilterIterator<T>(this, f)
|
inline fun <T> java.util.Iterator<T>.filter(f: (T)-> Boolean) : java.util.Iterator<T> = FilterIterator<T>(this, f)
|
||||||
|
|
||||||
/*
|
|
||||||
Adds filtered elements in to given container
|
|
||||||
*/
|
|
||||||
inline fun <T,U : Collection<in T>> java.lang.Iterable<T>.filterTo(var container: U, filter: (T)->Boolean) : U {
|
|
||||||
for(element in this) {
|
|
||||||
if(filter(element))
|
|
||||||
container.add(element)
|
|
||||||
}
|
|
||||||
return container
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Create iterator filtering given java.lang.Iterable
|
Create iterator filtering given java.lang.Iterable
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -41,8 +41,11 @@ inline fun <T> java.lang.Iterable<T>.find(predicate: (T)-> Boolean) : T? {
|
|||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns a new collection containing all elements in this collection which match the given predicate */
|
/** Returns a new List containing all elements in this collection which match the given predicate */
|
||||||
inline fun <T> java.lang.Iterable<T>.filter(result: Collection<T> = ArrayList<T>(), predicate: (T)-> Boolean) : Collection<T> {
|
inline fun <T> java.lang.Iterable<T>.filter(predicate: (T)-> Boolean) : Collection<T> = filterTo(java.util.ArrayList<T>(), predicate)
|
||||||
|
|
||||||
|
/** Filters all elements in this collection which match the given predicate into the given result collection */
|
||||||
|
inline fun <T, C: Collection<in T>> java.lang.Iterable<T>.filterTo(result: C, predicate: (T)-> Boolean) : C {
|
||||||
for (elem in this) {
|
for (elem in this) {
|
||||||
if (predicate(elem))
|
if (predicate(elem))
|
||||||
result.add(elem)
|
result.add(elem)
|
||||||
@@ -51,7 +54,10 @@ inline fun <T> java.lang.Iterable<T>.filter(result: Collection<T> = ArrayList<T>
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** 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 */
|
||||||
inline fun <T> java.lang.Iterable<T>.filterNot(result: Collection<T> = ArrayList<T>(), predicate: (T)-> Boolean) : Collection<T> {
|
inline fun <T> java.lang.Iterable<T>.filterNot(predicate: (T)-> Boolean) : Collection<T> = filterNotTo(ArrayList<T>(), predicate)
|
||||||
|
|
||||||
|
/** Returns a new collection containing all elements in this collection which do not match the given predicate */
|
||||||
|
inline fun <T, C: Collection<in T>> java.lang.Iterable<T>.filterNotTo(result: C, predicate: (T)-> Boolean) : C {
|
||||||
for (elem in this) {
|
for (elem in this) {
|
||||||
if (!predicate(elem))
|
if (!predicate(elem))
|
||||||
result.add(elem)
|
result.add(elem)
|
||||||
|
|||||||
Reference in New Issue
Block a user