From 3fa49cadc08ef35171b873d25d7a49aa6fd0fb04 Mon Sep 17 00:00:00 2001 From: James Strachan Date: Fri, 23 Dec 2011 16:27:40 +0000 Subject: [PATCH] added filterNot method --- stdlib/ktSrc/JavaIterables.kt | 9 +++++++++ stdlib/ktSrc/generated/ArraysFromJavaIterables.kt | 9 +++++++++ stdlib/ktSrc/generated/StandardFromJavaIterables.kt | 9 +++++++++ testlib/test/CollectionTest.kt | 10 ++++++++++ 4 files changed, 37 insertions(+) diff --git a/stdlib/ktSrc/JavaIterables.kt b/stdlib/ktSrc/JavaIterables.kt index 27bea581e02..c10479d2f10 100644 --- a/stdlib/ktSrc/JavaIterables.kt +++ b/stdlib/ktSrc/JavaIterables.kt @@ -40,6 +40,15 @@ inline fun java.lang.Iterable.filter(result: Collection = ArrayList return result } +/** Returns a new collection containing all elements in this collection which do not match the given predicate */ +inline fun java.lang.Iterable.filterNot(result: Collection = ArrayList(), predicate: (T)-> Boolean) : Collection { + for (elem in this) { + if (!predicate(elem)) + result.add(elem) + } + return result +} + /** * Returns the result of transforming each item in the collection to a one or more values which * are concatenated together into a single collection diff --git a/stdlib/ktSrc/generated/ArraysFromJavaIterables.kt b/stdlib/ktSrc/generated/ArraysFromJavaIterables.kt index bad6f8548ed..9794644f7d7 100644 --- a/stdlib/ktSrc/generated/ArraysFromJavaIterables.kt +++ b/stdlib/ktSrc/generated/ArraysFromJavaIterables.kt @@ -41,6 +41,15 @@ inline fun Array.filter(result: Collection = ArrayList(), predicate return result } +/** Returns a new collection containing all elements in this collection which do not match the given predicate */ +inline fun Array.filterNot(result: Collection = ArrayList(), predicate: (T)-> Boolean) : Collection { + for (elem in this) { + if (!predicate(elem)) + result.add(elem) + } + return result +} + /** * Returns the result of transforming each item in the collection to a one or more values which * are concatenated together into a single collection diff --git a/stdlib/ktSrc/generated/StandardFromJavaIterables.kt b/stdlib/ktSrc/generated/StandardFromJavaIterables.kt index f56796318f4..00a566350ec 100644 --- a/stdlib/ktSrc/generated/StandardFromJavaIterables.kt +++ b/stdlib/ktSrc/generated/StandardFromJavaIterables.kt @@ -41,6 +41,15 @@ inline fun Iterable.filter(result: Collection = ArrayList(), predic return result } +/** Returns a new collection containing all elements in this collection which do not match the given predicate */ +inline fun Iterable.filterNot(result: Collection = ArrayList(), predicate: (T)-> Boolean) : Collection { + for (elem in this) { + if (!predicate(elem)) + result.add(elem) + } + return result +} + /** * Returns the result of transforming each item in the collection to a one or more values which * are concatenated together into a single collection diff --git a/testlib/test/CollectionTest.kt b/testlib/test/CollectionTest.kt index 1a85c73a7e5..1c4ef83d11d 100644 --- a/testlib/test/CollectionTest.kt +++ b/testlib/test/CollectionTest.kt @@ -39,6 +39,16 @@ class CollectionTest() : TestSupport() { assertEquals(arrayList("foo"), foo) } + fun testFilterNot() { + val foo = data.filterNot{it.startsWith("b")} + + assert { + foo.all{it.startsWith("f")} + } + assertEquals(1, foo.size) + assertEquals(arrayList("foo"), foo) + } + fun testFilterIntoLinkedList() { // TODO would be nice to avoid the val foo = data.filter(linkedList()){it.startsWith("f")}