From 852e21fe7ef96f2cc581200b0d8d94340151fb71 Mon Sep 17 00:00:00 2001 From: James Strachan Date: Sat, 7 Jan 2012 07:49:54 +0000 Subject: [PATCH] added Iterable.reverse() along with foldRight(init, op) --- stdlib/ktSrc/JavaIterables.kt | 19 ++++++++++++++++++- testlib/test/CollectionTest.kt | 24 ++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/stdlib/ktSrc/JavaIterables.kt b/stdlib/ktSrc/JavaIterables.kt index d299d6485a6..8815f7e929d 100644 --- a/stdlib/ktSrc/JavaIterables.kt +++ b/stdlib/ktSrc/JavaIterables.kt @@ -82,7 +82,7 @@ inline fun java.lang.Iterable.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 * {code}val total = numbers.fold(0){(a, b) -> a + b}{code} @@ -95,6 +95,14 @@ inline fun java.lang.Iterable.fold(initial: T, operation: (it: T, it2: T) 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 java.lang.Iterable.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 * as the key in a map to group elements by the result @@ -124,6 +132,15 @@ inline fun java.lang.Iterable.join(separator: String, prefix: String = "" return buffer.toString().sure() } +/** Returns a reversed List of this collection */ +inline fun java.lang.Iterable.reverse() : List { + val answer = LinkedList() + for (elem in this) { + answer.addFirst(elem) + } + return answer +} + inline fun > java.lang.Iterable.to(result: C) : C { for (elem in this) result.add(elem) diff --git a/testlib/test/CollectionTest.kt b/testlib/test/CollectionTest.kt index bf570281b2c..c479553458e 100644 --- a/testlib/test/CollectionTest.kt +++ b/testlib/test/CollectionTest.kt @@ -140,8 +140,27 @@ class CollectionTest() : TestSupport() { val numbers = arrayList() 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{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{it.toString()}.foldRight(""){(it, it2) -> it + it2} + } + } + + fun testGroupBy() { val words = arrayList("a", "ab", "abc", "def", "abcd") /* @@ -179,6 +198,11 @@ class CollectionTest() : TestSupport() { assertEquals(arrayList(3, 3), lengths) } + fun testReverse() { + val rev = data.reverse() + assertEquals(arrayList("bar", "foo"), rev) + } + fun testSort() { val coll: List = arrayList("foo", "bar", "abc")