added Iterable<T>.reverse() along with foldRight(init, op)
This commit is contained in:
@@ -82,7 +82,7 @@ inline fun <T> java.lang.Iterable<T>.foreach(operation: (element: T) -> Unit) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Folds all the values from from left to right using the initial value to perform the operation on sequential pairs of values
|
* 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
|
* 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}
|
* {code}val total = numbers.fold(0){(a, b) -> a + b}{code}
|
||||||
@@ -95,6 +95,14 @@ inline fun <T> java.lang.Iterable<T>.fold(initial: T, operation: (it: T, it2: T)
|
|||||||
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
|
||||||
|
*/
|
||||||
|
inline fun <T> java.lang.Iterable<T>.foldRight(initial: T, operation: (it: T, it2: T) -> T): T {
|
||||||
|
val reversed = this.reverse()
|
||||||
|
return reversed.fold(initial, operation)
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Iterates through the collection performing the transformation on each element and using the result
|
* 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
|
* as the key in a map to group elements by the result
|
||||||
@@ -124,6 +132,15 @@ inline fun <T> java.lang.Iterable<T>.join(separator: String, prefix: String = ""
|
|||||||
return buffer.toString().sure()
|
return buffer.toString().sure()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Returns a reversed List of this collection */
|
||||||
|
inline fun <T> java.lang.Iterable<T>.reverse() : List<T> {
|
||||||
|
val answer = LinkedList<T>()
|
||||||
|
for (elem in this) {
|
||||||
|
answer.addFirst(elem)
|
||||||
|
}
|
||||||
|
return answer
|
||||||
|
}
|
||||||
|
|
||||||
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 (elem in this)
|
||||||
result.add(elem)
|
result.add(elem)
|
||||||
|
|||||||
@@ -140,8 +140,27 @@ class CollectionTest() : TestSupport() {
|
|||||||
val numbers = arrayList<Int>()
|
val numbers = arrayList<Int>()
|
||||||
numbers.fold(0){(it, it2) -> it + it2}
|
numbers.fold(0){(it, it2) -> it + it2}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
expect("1234") {
|
||||||
|
val numbers = arrayList(1, 2, 3, 4)
|
||||||
|
|
||||||
|
// TODO would be nice to be able to write this as this
|
||||||
|
// numbers.map{it.toString()}.fold(""){it + it2}
|
||||||
|
numbers.map<Int,String>{it.toString()}.fold(""){(it, it2) -> it + it2}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun testFoldRight() {
|
||||||
|
expect("4321") {
|
||||||
|
val numbers = arrayList(1, 2, 3, 4)
|
||||||
|
|
||||||
|
// TODO would be nice to be able to write this as this
|
||||||
|
// numbers.map{it.toString()}.foldRight(""){it + it2}
|
||||||
|
numbers.map<Int,String>{it.toString()}.foldRight(""){(it, it2) -> it + it2}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
fun testGroupBy() {
|
fun testGroupBy() {
|
||||||
val words = arrayList("a", "ab", "abc", "def", "abcd")
|
val words = arrayList("a", "ab", "abc", "def", "abcd")
|
||||||
/*
|
/*
|
||||||
@@ -179,6 +198,11 @@ class CollectionTest() : TestSupport() {
|
|||||||
assertEquals(arrayList(3, 3), lengths)
|
assertEquals(arrayList(3, 3), lengths)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun testReverse() {
|
||||||
|
val rev = data.reverse()
|
||||||
|
assertEquals(arrayList("bar", "foo"), rev)
|
||||||
|
}
|
||||||
|
|
||||||
fun testSort() {
|
fun testSort() {
|
||||||
val coll: List<String> = arrayList("foo", "bar", "abc")
|
val coll: List<String> = arrayList("foo", "bar", "abc")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user