Add flatten, flatMap, and filterNotNull functions with corresponding tests
This commit is contained in:
1 parent
357585d3ba
commit
3b6504d3b1
2 files changed
+43
No files matched your search
@@ -27,6 +27,14 @@ fun Iterable.filter(predicate) {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
filter out all null elements from this collection (Iterable); collection of
|
||||
non-null elements is returned
|
||||
*/
|
||||
fun Iterable.filterNotNull() {
|
||||
filter { it != null }
|
||||
}
|
||||
|
||||
/* Skip the first N elements of this iterable. */
|
||||
fun Iterable.drop(n) {
|
||||
var cnt = 0
|
||||
@@ -179,6 +187,26 @@ fun Iterable.shuffled() {
|
||||
toList().apply { shuffle() }
|
||||
}
|
||||
|
||||
/*
|
||||
Returns a single list of all elements from all collections in the given collection.
|
||||
@return List
|
||||
*/
|
||||
fun Iterable.flatten() {
|
||||
val result = []
|
||||
forEach { i ->
|
||||
i.forEach { result.add(it) }
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
/*
|
||||
Returns a single list of all elements yielded from results of transform function being
|
||||
invoked on each element of original collection.
|
||||
*/
|
||||
fun Iterable.flatMap(transform): List {
|
||||
map(transform).flatten()
|
||||
}
|
||||
|
||||
/* Return string representation like [a,b,c]. */
|
||||
fun List.toString() {
|
||||
"[" + joinToString(",") + "]"
|
||||
|
||||
Reference in new issue
Block a user