From 5c41c9dca6f691882bad43255bbbaa3a81177f86 Mon Sep 17 00:00:00 2001 From: Daniel Dietrich Date: Fri, 27 Jul 2018 14:50:29 +0200 Subject: [PATCH] Collection functions (#2267) * Removed PartialFunction and Function1 from collection interfaces * Added asPartialFunction() to Seq, Map and Multimap --- vavr/generator/Generator.scala | 2 +- vavr/src-gen/test/java/io/vavr/APITest.java | 330 +++++++++--------- .../java/io/vavr/collection/IndexedSeq.java | 20 +- .../java/io/vavr/collection/LinearSeq.java | 22 +- .../src/main/java/io/vavr/collection/Map.java | 55 ++- .../java/io/vavr/collection/Multimap.java | 34 +- .../src/main/java/io/vavr/collection/Seq.java | 63 +--- .../src/main/java/io/vavr/collection/Set.java | 16 +- vavr/src/test/java/io/vavr/MatchTest.java | 12 + .../io/vavr/collection/AbstractMapTest.java | 40 +-- .../vavr/collection/AbstractMultimapTest.java | 24 +- .../io/vavr/collection/AbstractSeqTest.java | 91 ++--- .../collection/AbstractTraversableTest.java | 6 +- .../java/io/vavr/collection/CharSeqTest.java | 4 +- .../io/vavr/collection/QuickSortTest.java | 6 +- .../java/io/vavr/collection/StreamTest.java | 2 +- 16 files changed, 336 insertions(+), 391 deletions(-) diff --git a/vavr/generator/Generator.scala b/vavr/generator/Generator.scala index 525c5fffe9..d63e00bd87 100644 --- a/vavr/generator/Generator.scala +++ b/vavr/generator/Generator.scala @@ -2584,7 +2584,7 @@ def generateTestClasses(): Unit = { @$test public void shouldCreate${func}From${i}Pairs() { $MapType map = $func(${(1 to i).gen(j => s"$j, ${j*2}")(", ")}); - ${(1 to i).gen(j => s"assertThat(map.apply($j)).isEqualTo(${j*2});")("\n")} + ${(1 to i).gen(j => s"assertThat(map.get($j).get()).isEqualTo(${j*2});")("\n")} } """ })("\n\n")} diff --git a/vavr/src-gen/test/java/io/vavr/APITest.java b/vavr/src-gen/test/java/io/vavr/APITest.java index 713f7a9b59..5f8221e9e8 100644 --- a/vavr/src-gen/test/java/io/vavr/APITest.java +++ b/vavr/src-gen/test/java/io/vavr/APITest.java @@ -598,106 +598,106 @@ public void shouldLinkedMapFromPairsReturnNotNull() { @Test public void shouldCreateLinkedMapFrom1Pairs() { Map map = LinkedMap(1, 2); - assertThat(map.apply(1)).isEqualTo(2); + assertThat(map.get(1).get()).isEqualTo(2); } @Test public void shouldCreateLinkedMapFrom2Pairs() { Map map = LinkedMap(1, 2, 2, 4); - assertThat(map.apply(1)).isEqualTo(2); - assertThat(map.apply(2)).isEqualTo(4); + assertThat(map.get(1).get()).isEqualTo(2); + assertThat(map.get(2).get()).isEqualTo(4); } @Test public void shouldCreateLinkedMapFrom3Pairs() { Map map = LinkedMap(1, 2, 2, 4, 3, 6); - assertThat(map.apply(1)).isEqualTo(2); - assertThat(map.apply(2)).isEqualTo(4); - assertThat(map.apply(3)).isEqualTo(6); + assertThat(map.get(1).get()).isEqualTo(2); + assertThat(map.get(2).get()).isEqualTo(4); + assertThat(map.get(3).get()).isEqualTo(6); } @Test public void shouldCreateLinkedMapFrom4Pairs() { Map map = LinkedMap(1, 2, 2, 4, 3, 6, 4, 8); - assertThat(map.apply(1)).isEqualTo(2); - assertThat(map.apply(2)).isEqualTo(4); - assertThat(map.apply(3)).isEqualTo(6); - assertThat(map.apply(4)).isEqualTo(8); + assertThat(map.get(1).get()).isEqualTo(2); + assertThat(map.get(2).get()).isEqualTo(4); + assertThat(map.get(3).get()).isEqualTo(6); + assertThat(map.get(4).get()).isEqualTo(8); } @Test public void shouldCreateLinkedMapFrom5Pairs() { Map map = LinkedMap(1, 2, 2, 4, 3, 6, 4, 8, 5, 10); - assertThat(map.apply(1)).isEqualTo(2); - assertThat(map.apply(2)).isEqualTo(4); - assertThat(map.apply(3)).isEqualTo(6); - assertThat(map.apply(4)).isEqualTo(8); - assertThat(map.apply(5)).isEqualTo(10); + assertThat(map.get(1).get()).isEqualTo(2); + assertThat(map.get(2).get()).isEqualTo(4); + assertThat(map.get(3).get()).isEqualTo(6); + assertThat(map.get(4).get()).isEqualTo(8); + assertThat(map.get(5).get()).isEqualTo(10); } @Test public void shouldCreateLinkedMapFrom6Pairs() { Map map = LinkedMap(1, 2, 2, 4, 3, 6, 4, 8, 5, 10, 6, 12); - assertThat(map.apply(1)).isEqualTo(2); - assertThat(map.apply(2)).isEqualTo(4); - assertThat(map.apply(3)).isEqualTo(6); - assertThat(map.apply(4)).isEqualTo(8); - assertThat(map.apply(5)).isEqualTo(10); - assertThat(map.apply(6)).isEqualTo(12); + assertThat(map.get(1).get()).isEqualTo(2); + assertThat(map.get(2).get()).isEqualTo(4); + assertThat(map.get(3).get()).isEqualTo(6); + assertThat(map.get(4).get()).isEqualTo(8); + assertThat(map.get(5).get()).isEqualTo(10); + assertThat(map.get(6).get()).isEqualTo(12); } @Test public void shouldCreateLinkedMapFrom7Pairs() { Map map = LinkedMap(1, 2, 2, 4, 3, 6, 4, 8, 5, 10, 6, 12, 7, 14); - assertThat(map.apply(1)).isEqualTo(2); - assertThat(map.apply(2)).isEqualTo(4); - assertThat(map.apply(3)).isEqualTo(6); - assertThat(map.apply(4)).isEqualTo(8); - assertThat(map.apply(5)).isEqualTo(10); - assertThat(map.apply(6)).isEqualTo(12); - assertThat(map.apply(7)).isEqualTo(14); + assertThat(map.get(1).get()).isEqualTo(2); + assertThat(map.get(2).get()).isEqualTo(4); + assertThat(map.get(3).get()).isEqualTo(6); + assertThat(map.get(4).get()).isEqualTo(8); + assertThat(map.get(5).get()).isEqualTo(10); + assertThat(map.get(6).get()).isEqualTo(12); + assertThat(map.get(7).get()).isEqualTo(14); } @Test public void shouldCreateLinkedMapFrom8Pairs() { Map map = LinkedMap(1, 2, 2, 4, 3, 6, 4, 8, 5, 10, 6, 12, 7, 14, 8, 16); - assertThat(map.apply(1)).isEqualTo(2); - assertThat(map.apply(2)).isEqualTo(4); - assertThat(map.apply(3)).isEqualTo(6); - assertThat(map.apply(4)).isEqualTo(8); - assertThat(map.apply(5)).isEqualTo(10); - assertThat(map.apply(6)).isEqualTo(12); - assertThat(map.apply(7)).isEqualTo(14); - assertThat(map.apply(8)).isEqualTo(16); + assertThat(map.get(1).get()).isEqualTo(2); + assertThat(map.get(2).get()).isEqualTo(4); + assertThat(map.get(3).get()).isEqualTo(6); + assertThat(map.get(4).get()).isEqualTo(8); + assertThat(map.get(5).get()).isEqualTo(10); + assertThat(map.get(6).get()).isEqualTo(12); + assertThat(map.get(7).get()).isEqualTo(14); + assertThat(map.get(8).get()).isEqualTo(16); } @Test public void shouldCreateLinkedMapFrom9Pairs() { Map map = LinkedMap(1, 2, 2, 4, 3, 6, 4, 8, 5, 10, 6, 12, 7, 14, 8, 16, 9, 18); - assertThat(map.apply(1)).isEqualTo(2); - assertThat(map.apply(2)).isEqualTo(4); - assertThat(map.apply(3)).isEqualTo(6); - assertThat(map.apply(4)).isEqualTo(8); - assertThat(map.apply(5)).isEqualTo(10); - assertThat(map.apply(6)).isEqualTo(12); - assertThat(map.apply(7)).isEqualTo(14); - assertThat(map.apply(8)).isEqualTo(16); - assertThat(map.apply(9)).isEqualTo(18); + assertThat(map.get(1).get()).isEqualTo(2); + assertThat(map.get(2).get()).isEqualTo(4); + assertThat(map.get(3).get()).isEqualTo(6); + assertThat(map.get(4).get()).isEqualTo(8); + assertThat(map.get(5).get()).isEqualTo(10); + assertThat(map.get(6).get()).isEqualTo(12); + assertThat(map.get(7).get()).isEqualTo(14); + assertThat(map.get(8).get()).isEqualTo(16); + assertThat(map.get(9).get()).isEqualTo(18); } @Test public void shouldCreateLinkedMapFrom10Pairs() { Map map = LinkedMap(1, 2, 2, 4, 3, 6, 4, 8, 5, 10, 6, 12, 7, 14, 8, 16, 9, 18, 10, 20); - assertThat(map.apply(1)).isEqualTo(2); - assertThat(map.apply(2)).isEqualTo(4); - assertThat(map.apply(3)).isEqualTo(6); - assertThat(map.apply(4)).isEqualTo(8); - assertThat(map.apply(5)).isEqualTo(10); - assertThat(map.apply(6)).isEqualTo(12); - assertThat(map.apply(7)).isEqualTo(14); - assertThat(map.apply(8)).isEqualTo(16); - assertThat(map.apply(9)).isEqualTo(18); - assertThat(map.apply(10)).isEqualTo(20); + assertThat(map.get(1).get()).isEqualTo(2); + assertThat(map.get(2).get()).isEqualTo(4); + assertThat(map.get(3).get()).isEqualTo(6); + assertThat(map.get(4).get()).isEqualTo(8); + assertThat(map.get(5).get()).isEqualTo(10); + assertThat(map.get(6).get()).isEqualTo(12); + assertThat(map.get(7).get()).isEqualTo(14); + assertThat(map.get(8).get()).isEqualTo(16); + assertThat(map.get(9).get()).isEqualTo(18); + assertThat(map.get(10).get()).isEqualTo(20); } @Test @@ -723,106 +723,106 @@ public void shouldMapFromPairsReturnNotNull() { @Test public void shouldCreateMapFrom1Pairs() { Map map = Map(1, 2); - assertThat(map.apply(1)).isEqualTo(2); + assertThat(map.get(1).get()).isEqualTo(2); } @Test public void shouldCreateMapFrom2Pairs() { Map map = Map(1, 2, 2, 4); - assertThat(map.apply(1)).isEqualTo(2); - assertThat(map.apply(2)).isEqualTo(4); + assertThat(map.get(1).get()).isEqualTo(2); + assertThat(map.get(2).get()).isEqualTo(4); } @Test public void shouldCreateMapFrom3Pairs() { Map map = Map(1, 2, 2, 4, 3, 6); - assertThat(map.apply(1)).isEqualTo(2); - assertThat(map.apply(2)).isEqualTo(4); - assertThat(map.apply(3)).isEqualTo(6); + assertThat(map.get(1).get()).isEqualTo(2); + assertThat(map.get(2).get()).isEqualTo(4); + assertThat(map.get(3).get()).isEqualTo(6); } @Test public void shouldCreateMapFrom4Pairs() { Map map = Map(1, 2, 2, 4, 3, 6, 4, 8); - assertThat(map.apply(1)).isEqualTo(2); - assertThat(map.apply(2)).isEqualTo(4); - assertThat(map.apply(3)).isEqualTo(6); - assertThat(map.apply(4)).isEqualTo(8); + assertThat(map.get(1).get()).isEqualTo(2); + assertThat(map.get(2).get()).isEqualTo(4); + assertThat(map.get(3).get()).isEqualTo(6); + assertThat(map.get(4).get()).isEqualTo(8); } @Test public void shouldCreateMapFrom5Pairs() { Map map = Map(1, 2, 2, 4, 3, 6, 4, 8, 5, 10); - assertThat(map.apply(1)).isEqualTo(2); - assertThat(map.apply(2)).isEqualTo(4); - assertThat(map.apply(3)).isEqualTo(6); - assertThat(map.apply(4)).isEqualTo(8); - assertThat(map.apply(5)).isEqualTo(10); + assertThat(map.get(1).get()).isEqualTo(2); + assertThat(map.get(2).get()).isEqualTo(4); + assertThat(map.get(3).get()).isEqualTo(6); + assertThat(map.get(4).get()).isEqualTo(8); + assertThat(map.get(5).get()).isEqualTo(10); } @Test public void shouldCreateMapFrom6Pairs() { Map map = Map(1, 2, 2, 4, 3, 6, 4, 8, 5, 10, 6, 12); - assertThat(map.apply(1)).isEqualTo(2); - assertThat(map.apply(2)).isEqualTo(4); - assertThat(map.apply(3)).isEqualTo(6); - assertThat(map.apply(4)).isEqualTo(8); - assertThat(map.apply(5)).isEqualTo(10); - assertThat(map.apply(6)).isEqualTo(12); + assertThat(map.get(1).get()).isEqualTo(2); + assertThat(map.get(2).get()).isEqualTo(4); + assertThat(map.get(3).get()).isEqualTo(6); + assertThat(map.get(4).get()).isEqualTo(8); + assertThat(map.get(5).get()).isEqualTo(10); + assertThat(map.get(6).get()).isEqualTo(12); } @Test public void shouldCreateMapFrom7Pairs() { Map map = Map(1, 2, 2, 4, 3, 6, 4, 8, 5, 10, 6, 12, 7, 14); - assertThat(map.apply(1)).isEqualTo(2); - assertThat(map.apply(2)).isEqualTo(4); - assertThat(map.apply(3)).isEqualTo(6); - assertThat(map.apply(4)).isEqualTo(8); - assertThat(map.apply(5)).isEqualTo(10); - assertThat(map.apply(6)).isEqualTo(12); - assertThat(map.apply(7)).isEqualTo(14); + assertThat(map.get(1).get()).isEqualTo(2); + assertThat(map.get(2).get()).isEqualTo(4); + assertThat(map.get(3).get()).isEqualTo(6); + assertThat(map.get(4).get()).isEqualTo(8); + assertThat(map.get(5).get()).isEqualTo(10); + assertThat(map.get(6).get()).isEqualTo(12); + assertThat(map.get(7).get()).isEqualTo(14); } @Test public void shouldCreateMapFrom8Pairs() { Map map = Map(1, 2, 2, 4, 3, 6, 4, 8, 5, 10, 6, 12, 7, 14, 8, 16); - assertThat(map.apply(1)).isEqualTo(2); - assertThat(map.apply(2)).isEqualTo(4); - assertThat(map.apply(3)).isEqualTo(6); - assertThat(map.apply(4)).isEqualTo(8); - assertThat(map.apply(5)).isEqualTo(10); - assertThat(map.apply(6)).isEqualTo(12); - assertThat(map.apply(7)).isEqualTo(14); - assertThat(map.apply(8)).isEqualTo(16); + assertThat(map.get(1).get()).isEqualTo(2); + assertThat(map.get(2).get()).isEqualTo(4); + assertThat(map.get(3).get()).isEqualTo(6); + assertThat(map.get(4).get()).isEqualTo(8); + assertThat(map.get(5).get()).isEqualTo(10); + assertThat(map.get(6).get()).isEqualTo(12); + assertThat(map.get(7).get()).isEqualTo(14); + assertThat(map.get(8).get()).isEqualTo(16); } @Test public void shouldCreateMapFrom9Pairs() { Map map = Map(1, 2, 2, 4, 3, 6, 4, 8, 5, 10, 6, 12, 7, 14, 8, 16, 9, 18); - assertThat(map.apply(1)).isEqualTo(2); - assertThat(map.apply(2)).isEqualTo(4); - assertThat(map.apply(3)).isEqualTo(6); - assertThat(map.apply(4)).isEqualTo(8); - assertThat(map.apply(5)).isEqualTo(10); - assertThat(map.apply(6)).isEqualTo(12); - assertThat(map.apply(7)).isEqualTo(14); - assertThat(map.apply(8)).isEqualTo(16); - assertThat(map.apply(9)).isEqualTo(18); + assertThat(map.get(1).get()).isEqualTo(2); + assertThat(map.get(2).get()).isEqualTo(4); + assertThat(map.get(3).get()).isEqualTo(6); + assertThat(map.get(4).get()).isEqualTo(8); + assertThat(map.get(5).get()).isEqualTo(10); + assertThat(map.get(6).get()).isEqualTo(12); + assertThat(map.get(7).get()).isEqualTo(14); + assertThat(map.get(8).get()).isEqualTo(16); + assertThat(map.get(9).get()).isEqualTo(18); } @Test public void shouldCreateMapFrom10Pairs() { Map map = Map(1, 2, 2, 4, 3, 6, 4, 8, 5, 10, 6, 12, 7, 14, 8, 16, 9, 18, 10, 20); - assertThat(map.apply(1)).isEqualTo(2); - assertThat(map.apply(2)).isEqualTo(4); - assertThat(map.apply(3)).isEqualTo(6); - assertThat(map.apply(4)).isEqualTo(8); - assertThat(map.apply(5)).isEqualTo(10); - assertThat(map.apply(6)).isEqualTo(12); - assertThat(map.apply(7)).isEqualTo(14); - assertThat(map.apply(8)).isEqualTo(16); - assertThat(map.apply(9)).isEqualTo(18); - assertThat(map.apply(10)).isEqualTo(20); + assertThat(map.get(1).get()).isEqualTo(2); + assertThat(map.get(2).get()).isEqualTo(4); + assertThat(map.get(3).get()).isEqualTo(6); + assertThat(map.get(4).get()).isEqualTo(8); + assertThat(map.get(5).get()).isEqualTo(10); + assertThat(map.get(6).get()).isEqualTo(12); + assertThat(map.get(7).get()).isEqualTo(14); + assertThat(map.get(8).get()).isEqualTo(16); + assertThat(map.get(9).get()).isEqualTo(18); + assertThat(map.get(10).get()).isEqualTo(20); } @Test @@ -848,106 +848,106 @@ public void shouldSortedMapFromPairsReturnNotNull() { @Test public void shouldCreateSortedMapFrom1Pairs() { Map map = SortedMap(1, 2); - assertThat(map.apply(1)).isEqualTo(2); + assertThat(map.get(1).get()).isEqualTo(2); } @Test public void shouldCreateSortedMapFrom2Pairs() { Map map = SortedMap(1, 2, 2, 4); - assertThat(map.apply(1)).isEqualTo(2); - assertThat(map.apply(2)).isEqualTo(4); + assertThat(map.get(1).get()).isEqualTo(2); + assertThat(map.get(2).get()).isEqualTo(4); } @Test public void shouldCreateSortedMapFrom3Pairs() { Map map = SortedMap(1, 2, 2, 4, 3, 6); - assertThat(map.apply(1)).isEqualTo(2); - assertThat(map.apply(2)).isEqualTo(4); - assertThat(map.apply(3)).isEqualTo(6); + assertThat(map.get(1).get()).isEqualTo(2); + assertThat(map.get(2).get()).isEqualTo(4); + assertThat(map.get(3).get()).isEqualTo(6); } @Test public void shouldCreateSortedMapFrom4Pairs() { Map map = SortedMap(1, 2, 2, 4, 3, 6, 4, 8); - assertThat(map.apply(1)).isEqualTo(2); - assertThat(map.apply(2)).isEqualTo(4); - assertThat(map.apply(3)).isEqualTo(6); - assertThat(map.apply(4)).isEqualTo(8); + assertThat(map.get(1).get()).isEqualTo(2); + assertThat(map.get(2).get()).isEqualTo(4); + assertThat(map.get(3).get()).isEqualTo(6); + assertThat(map.get(4).get()).isEqualTo(8); } @Test public void shouldCreateSortedMapFrom5Pairs() { Map map = SortedMap(1, 2, 2, 4, 3, 6, 4, 8, 5, 10); - assertThat(map.apply(1)).isEqualTo(2); - assertThat(map.apply(2)).isEqualTo(4); - assertThat(map.apply(3)).isEqualTo(6); - assertThat(map.apply(4)).isEqualTo(8); - assertThat(map.apply(5)).isEqualTo(10); + assertThat(map.get(1).get()).isEqualTo(2); + assertThat(map.get(2).get()).isEqualTo(4); + assertThat(map.get(3).get()).isEqualTo(6); + assertThat(map.get(4).get()).isEqualTo(8); + assertThat(map.get(5).get()).isEqualTo(10); } @Test public void shouldCreateSortedMapFrom6Pairs() { Map map = SortedMap(1, 2, 2, 4, 3, 6, 4, 8, 5, 10, 6, 12); - assertThat(map.apply(1)).isEqualTo(2); - assertThat(map.apply(2)).isEqualTo(4); - assertThat(map.apply(3)).isEqualTo(6); - assertThat(map.apply(4)).isEqualTo(8); - assertThat(map.apply(5)).isEqualTo(10); - assertThat(map.apply(6)).isEqualTo(12); + assertThat(map.get(1).get()).isEqualTo(2); + assertThat(map.get(2).get()).isEqualTo(4); + assertThat(map.get(3).get()).isEqualTo(6); + assertThat(map.get(4).get()).isEqualTo(8); + assertThat(map.get(5).get()).isEqualTo(10); + assertThat(map.get(6).get()).isEqualTo(12); } @Test public void shouldCreateSortedMapFrom7Pairs() { Map map = SortedMap(1, 2, 2, 4, 3, 6, 4, 8, 5, 10, 6, 12, 7, 14); - assertThat(map.apply(1)).isEqualTo(2); - assertThat(map.apply(2)).isEqualTo(4); - assertThat(map.apply(3)).isEqualTo(6); - assertThat(map.apply(4)).isEqualTo(8); - assertThat(map.apply(5)).isEqualTo(10); - assertThat(map.apply(6)).isEqualTo(12); - assertThat(map.apply(7)).isEqualTo(14); + assertThat(map.get(1).get()).isEqualTo(2); + assertThat(map.get(2).get()).isEqualTo(4); + assertThat(map.get(3).get()).isEqualTo(6); + assertThat(map.get(4).get()).isEqualTo(8); + assertThat(map.get(5).get()).isEqualTo(10); + assertThat(map.get(6).get()).isEqualTo(12); + assertThat(map.get(7).get()).isEqualTo(14); } @Test public void shouldCreateSortedMapFrom8Pairs() { Map map = SortedMap(1, 2, 2, 4, 3, 6, 4, 8, 5, 10, 6, 12, 7, 14, 8, 16); - assertThat(map.apply(1)).isEqualTo(2); - assertThat(map.apply(2)).isEqualTo(4); - assertThat(map.apply(3)).isEqualTo(6); - assertThat(map.apply(4)).isEqualTo(8); - assertThat(map.apply(5)).isEqualTo(10); - assertThat(map.apply(6)).isEqualTo(12); - assertThat(map.apply(7)).isEqualTo(14); - assertThat(map.apply(8)).isEqualTo(16); + assertThat(map.get(1).get()).isEqualTo(2); + assertThat(map.get(2).get()).isEqualTo(4); + assertThat(map.get(3).get()).isEqualTo(6); + assertThat(map.get(4).get()).isEqualTo(8); + assertThat(map.get(5).get()).isEqualTo(10); + assertThat(map.get(6).get()).isEqualTo(12); + assertThat(map.get(7).get()).isEqualTo(14); + assertThat(map.get(8).get()).isEqualTo(16); } @Test public void shouldCreateSortedMapFrom9Pairs() { Map map = SortedMap(1, 2, 2, 4, 3, 6, 4, 8, 5, 10, 6, 12, 7, 14, 8, 16, 9, 18); - assertThat(map.apply(1)).isEqualTo(2); - assertThat(map.apply(2)).isEqualTo(4); - assertThat(map.apply(3)).isEqualTo(6); - assertThat(map.apply(4)).isEqualTo(8); - assertThat(map.apply(5)).isEqualTo(10); - assertThat(map.apply(6)).isEqualTo(12); - assertThat(map.apply(7)).isEqualTo(14); - assertThat(map.apply(8)).isEqualTo(16); - assertThat(map.apply(9)).isEqualTo(18); + assertThat(map.get(1).get()).isEqualTo(2); + assertThat(map.get(2).get()).isEqualTo(4); + assertThat(map.get(3).get()).isEqualTo(6); + assertThat(map.get(4).get()).isEqualTo(8); + assertThat(map.get(5).get()).isEqualTo(10); + assertThat(map.get(6).get()).isEqualTo(12); + assertThat(map.get(7).get()).isEqualTo(14); + assertThat(map.get(8).get()).isEqualTo(16); + assertThat(map.get(9).get()).isEqualTo(18); } @Test public void shouldCreateSortedMapFrom10Pairs() { Map map = SortedMap(1, 2, 2, 4, 3, 6, 4, 8, 5, 10, 6, 12, 7, 14, 8, 16, 9, 18, 10, 20); - assertThat(map.apply(1)).isEqualTo(2); - assertThat(map.apply(2)).isEqualTo(4); - assertThat(map.apply(3)).isEqualTo(6); - assertThat(map.apply(4)).isEqualTo(8); - assertThat(map.apply(5)).isEqualTo(10); - assertThat(map.apply(6)).isEqualTo(12); - assertThat(map.apply(7)).isEqualTo(14); - assertThat(map.apply(8)).isEqualTo(16); - assertThat(map.apply(9)).isEqualTo(18); - assertThat(map.apply(10)).isEqualTo(20); + assertThat(map.get(1).get()).isEqualTo(2); + assertThat(map.get(2).get()).isEqualTo(4); + assertThat(map.get(3).get()).isEqualTo(6); + assertThat(map.get(4).get()).isEqualTo(8); + assertThat(map.get(5).get()).isEqualTo(10); + assertThat(map.get(6).get()).isEqualTo(12); + assertThat(map.get(7).get()).isEqualTo(14); + assertThat(map.get(8).get()).isEqualTo(16); + assertThat(map.get(9).get()).isEqualTo(18); + assertThat(map.get(10).get()).isEqualTo(20); } @Test diff --git a/vavr/src/main/java/io/vavr/collection/IndexedSeq.java b/vavr/src/main/java/io/vavr/collection/IndexedSeq.java index 4225abb27d..626f11b95a 100644 --- a/vavr/src/main/java/io/vavr/collection/IndexedSeq.java +++ b/vavr/src/main/java/io/vavr/collection/IndexedSeq.java @@ -71,6 +71,21 @@ static IndexedSeq narrow(IndexedSeq indexedSeq) { @Override IndexedSeq asJavaMutable(Consumer> action); + @Override + default PartialFunction asPartialFunction() throws IndexOutOfBoundsException { + return new PartialFunction() { + private static final long serialVersionUID = 1L; + @Override + public T apply(Integer index) { + return get(index); + } + @Override + public boolean isDefinedAt(Integer index) { + return 0 <= index && index < length(); + } + }; + } + @Override IndexedSeq collect(PartialFunction partialFunction); @@ -177,11 +192,6 @@ default int indexOfSlice(Iterable that, int from) { @Override IndexedSeq intersperse(T element); - @Override - default boolean isDefinedAt(Integer index) { - return 0 <= index && index < length(); - } - @Override default T last() { if (isEmpty()) { diff --git a/vavr/src/main/java/io/vavr/collection/LinearSeq.java b/vavr/src/main/java/io/vavr/collection/LinearSeq.java index 1b6483e3b0..0ca5427981 100644 --- a/vavr/src/main/java/io/vavr/collection/LinearSeq.java +++ b/vavr/src/main/java/io/vavr/collection/LinearSeq.java @@ -70,6 +70,22 @@ static LinearSeq narrow(LinearSeq linearSeq) { @Override LinearSeq asJavaMutable(Consumer> action); + @Override + default PartialFunction asPartialFunction() throws IndexOutOfBoundsException { + return new PartialFunction() { + private static final long serialVersionUID = 1L; + @Override + public T apply(Integer index) { + return get(index); + } + @Override + public boolean isDefinedAt(Integer index) { + // we can't use length() because of infinite long sequences + return 0 <= index && drop(index).nonEmpty(); + } + }; + } + @Override LinearSeq collect(PartialFunction partialFunction); @@ -160,12 +176,6 @@ default int indexWhere(Predicate predicate, int from) { @Override LinearSeq intersperse(T element); - @Override - default boolean isDefinedAt(Integer index) { - // we can't use length() because of infinite long sequences - return 0 <= index && drop(index).nonEmpty(); - } - @Override default int lastIndexOfSlice(Iterable that, int end) { Objects.requireNonNull(that, "that is null"); diff --git a/vavr/src/main/java/io/vavr/collection/Map.java b/vavr/src/main/java/io/vavr/collection/Map.java index cc4d7fc4c9..c221a9033d 100644 --- a/vavr/src/main/java/io/vavr/collection/Map.java +++ b/vavr/src/main/java/io/vavr/collection/Map.java @@ -87,15 +87,13 @@ *
  • {@link #transform(Function)}
  • *
  • {@link #unzip(BiFunction)}
  • *
  • {@link #unzip3(BiFunction)}
  • - *
  • {@link #withDefault(Function)}
  • - *
  • {@link #withDefaultValue(Object)}
  • * * * @param Key type * @param Value type * @author Daniel Dietrich, Ruslan Sennov */ -public interface Map extends Traversable>, PartialFunction, Serializable { +public interface Map extends Traversable>, Serializable { long serialVersionUID = 1L; @@ -141,9 +139,26 @@ static Tuple2 entry(K key, V value) { return Tuple.of(key, value); } - @Override - default V apply(K key) { - return get(key).getOrElseThrow(() -> new NoSuchElementException(String.valueOf(key))); + /** + * Turns this {@code Map} into a {@link PartialFunction} which is defined at a specific index, if this {@code Map} + * contains the given key. When applied to a defined key, the partial function will return + * the value of this {@code Map} that is associated with the key. + * + * @return a new {@link PartialFunction} + * @throws NoSuchElementException when a non-existing key is applied to the partial function + */ + default PartialFunction asPartialFunction() throws IndexOutOfBoundsException { + return new PartialFunction() { + private static final long serialVersionUID = 1L; + @Override + public V apply(K key) { + return get(key).getOrElseThrow(() -> new NoSuchElementException(String.valueOf(key))); + } + @Override + public boolean isDefinedAt(K key) { + return containsKey(key); + } + }; } @Override @@ -659,29 +674,6 @@ default Iterator valuesIterator() { return iterator().map(Tuple2::_2); } - /** - * Turns this map from a partial function into a total function that - * returns a value computed by defaultFunction for all keys - * absent from the map. - * - * @param defaultFunction function to evaluate for all keys not present in the map - * @return a total function from K to T - */ - default Function1 withDefault(Function defaultFunction) { - return k -> get(k).getOrElse(() -> defaultFunction.apply(k)); - } - - /** - * Turns this map from a partial function into a total function that - * returns defaultValue for all keys absent from the map. - * - * @param defaultValue default value to return for all keys not present in the map - * @return a total function from K to T - */ - default Function1 withDefaultValue(V defaultValue) { - return k -> get(k).getOrElse(defaultValue); - } - @Override default Seq, U>> zip(Iterable that) { return zipWith(that, Tuple::of); @@ -746,11 +738,6 @@ default Seq zipWithIndex(BiFunction, ? super Integer @Override io.vavr.collection.Iterator> grouped(int size); - @Override - default boolean isDefinedAt(K key) { - return containsKey(key); - } - @Override default boolean isDistinct() { return true; diff --git a/vavr/src/main/java/io/vavr/collection/Multimap.java b/vavr/src/main/java/io/vavr/collection/Multimap.java index 2cd2515e04..511f6c1d87 100644 --- a/vavr/src/main/java/io/vavr/collection/Multimap.java +++ b/vavr/src/main/java/io/vavr/collection/Multimap.java @@ -87,7 +87,7 @@ * @param Value type * @author Ruslan Sennov */ -public interface Multimap extends Traversable>, PartialFunction>, Serializable { +public interface Multimap extends Traversable>, Serializable { long serialVersionUID = 1L; @@ -154,11 +154,6 @@ static Multimap narrow(Multimap map) { // -- non-static API - @Override - default Traversable apply(K key) { - return get(key).getOrElseThrow(NoSuchElementException::new); - } - /** * Converts this {@code Multimap} to a {@code Map} * @@ -166,6 +161,28 @@ default Traversable apply(K key) { */ Map> asMap(); + /** + * Turns this {@code Multimap} into a {@link PartialFunction} which is defined at a specific index, if this {@code Multimap} + * contains the given key. When applied to a defined key, the partial function will return + * the {@link Traversable} of this {@code Multimap} that is associated with the key. + * + * @return a new {@link PartialFunction} + * @throws NoSuchElementException when a non-existing key is applied to the partial function + */ + default PartialFunction> asPartialFunction() throws IndexOutOfBoundsException { + return new PartialFunction>() { + private static final long serialVersionUID = 1L; + @Override + public Traversable apply(K key) { + return get(key).getOrElseThrow(NoSuchElementException::new); + } + @Override + public boolean isDefinedAt(K key) { + return containsKey(key); + } + }; + } + /** * Maps this {@code Multimap} to a new {@code Multimap} with different component type by applying a function to its elements. * @@ -318,11 +335,6 @@ default boolean hasDefiniteSize() { return true; } - @Override - default boolean isDefinedAt(K key) { - return containsKey(key); - } - @Override default boolean isDistinct() { return true; diff --git a/vavr/src/main/java/io/vavr/collection/Seq.java b/vavr/src/main/java/io/vavr/collection/Seq.java index f43269a911..d62e9aada7 100644 --- a/vavr/src/main/java/io/vavr/collection/Seq.java +++ b/vavr/src/main/java/io/vavr/collection/Seq.java @@ -42,6 +42,12 @@ *
  • {@link #update(int, Object)}
  • * * + * Conversion: + * + *
      + *
    • {@link #asPartialFunction}
    • + *
    + * * Filtering: * *
      @@ -106,7 +112,7 @@ * @param Component type * @author Daniel Dietrich */ -public interface Seq extends Traversable, PartialFunction, Serializable { +public interface Seq extends Traversable, Serializable { long serialVersionUID = 1L; @@ -140,19 +146,6 @@ static Seq narrow(Seq seq) { * @throws NullPointerException if {@code elements} is null */ Seq appendAll(Iterable elements); - - /** - * A {@code Seq} is a partial function which returns the element at the specified index by calling - * {@linkplain #get(int)}. - * - * @param index an index - * @return the element at the given index - * @throws IndexOutOfBoundsException if this is empty, index < 0 or index >= length() - */ - @Override - default T apply(Integer index) { - return get(index); - } /** * Creates an immutable {@link java.util.List} view on top of this {@code Seq}, @@ -205,6 +198,16 @@ default T apply(Integer index) { @GwtIncompatible Seq asJavaMutable(Consumer> action); + /** + * Turns this {@code Seq} into a {@link PartialFunction} which is defined at a specific index, if this {@code Seq} + * contains at least index + 1 elements. When applied to a defined index, the partial function will return + * the value of this {@code Seq} at the specified index. + * + * @return a new {@link PartialFunction} + * @throws IndexOutOfBoundsException if this is empty, index < 0 or index >= length() + */ + PartialFunction asPartialFunction() throws IndexOutOfBoundsException; + @Override Seq collect(PartialFunction partialFunction); @@ -585,16 +588,6 @@ default Option lastIndexWhereOption(Predicate predicate, int return Collections.indexOption(lastIndexWhere(predicate, end)); } - /** - * Turns this sequence into a plain function returning an Option result. - * - * @return a function that takes an index i and returns the value of - * this sequence in a Some if the index is within bounds, otherwise a None. - */ - default Function1> lift() { - return i -> (i >= 0 && i < length()) ? Option.some(apply(i)) : Option.none(); - } - /** * Returns the index of the last occurrence of the given element before or at a given end index * or -1 if this does not contain the given element. @@ -1275,28 +1268,6 @@ default U foldRight(U zero, BiFunction f) @Override Seq zipWithIndex(BiFunction mapper); - /** - * Turns this sequence from a partial function into a total function that - * returns defaultValue for all indexes that are out of bounds. - * - * @param defaultValue default value to return for out of bound indexes - * @return a total function from index to T - */ - default Function1 withDefaultValue(T defaultValue) { - return i -> (i >= 0 && i < length()) ? apply(i) : defaultValue; - } - - /** - * Turns this sequence from a partial function into a total function that - * returns a value computed by defaultFunction for all indexes that are out of bounds. - * - * @param defaultFunction function to evaluate for all out of bounds indexes. - * @return a total function from index to T - */ - default Function1 withDefault(Function defaultFunction) { - return i -> (i >= 0 && i < length()) ? apply(i) : defaultFunction.apply(i); - } - @Override default boolean isSequential() { return true; diff --git a/vavr/src/main/java/io/vavr/collection/Set.java b/vavr/src/main/java/io/vavr/collection/Set.java index 2cbfd991c4..2ae1a0f209 100644 --- a/vavr/src/main/java/io/vavr/collection/Set.java +++ b/vavr/src/main/java/io/vavr/collection/Set.java @@ -19,7 +19,6 @@ */ package io.vavr.collection; -import io.vavr.Function1; import io.vavr.PartialFunction; import io.vavr.Tuple3; import io.vavr.Tuple2; @@ -82,7 +81,7 @@ * @param Component type * @author Daniel Dietrich, Ruslan Sennov */ -public interface Set extends Traversable, Function1, Serializable { +public interface Set extends Traversable, Serializable { long serialVersionUID = 1L; @@ -116,19 +115,6 @@ static Set narrow(Set set) { */ Set addAll(Iterable elements); - /** - * Tests if a given {@code element} is contained in this {@code Set}. - *

      - * This method is equivalent to {@link #contains(Object)}. - * - * @param element the element to test for membership. - * @return {@code true} if the given {@code element} is contained, {@code false} otherwise. - */ - @Override - default Boolean apply(T element) { - return contains(element); - } - /** * Calculates the difference between this set and another set. *

      diff --git a/vavr/src/test/java/io/vavr/MatchTest.java b/vavr/src/test/java/io/vavr/MatchTest.java index 3b5689441f..fbad8a312f 100644 --- a/vavr/src/test/java/io/vavr/MatchTest.java +++ b/vavr/src/test/java/io/vavr/MatchTest.java @@ -20,6 +20,7 @@ package io.vavr; import io.vavr.collection.List; +import io.vavr.collection.Set; import io.vavr.control.Either; import io.vavr.control.Option; import io.vavr.control.Option.Some; @@ -339,6 +340,17 @@ public void shouldDecomposeListWithNonEmptyTail() { } */ + // -- Set + + @Test + public void shouldDecomposeSet() { + final Set abc = Set("abc"); + final Set result = Match(abc).of( // Does not compile: the Java inference engine sees abc as a Function1 before a Set thus expects result to be of type Boolean + Case($(), () -> abc) + ); + assertThat(result).isEqualTo(abc); + } + // -- Validation @Test diff --git a/vavr/src/test/java/io/vavr/collection/AbstractMapTest.java b/vavr/src/test/java/io/vavr/collection/AbstractMapTest.java index 3e68ce3026..f63c7df679 100644 --- a/vavr/src/test/java/io/vavr/collection/AbstractMapTest.java +++ b/vavr/src/test/java/io/vavr/collection/AbstractMapTest.java @@ -461,16 +461,26 @@ public void shouldConstructWithFillWithDuplicatedKeys() { .isEqualTo(mapOf(1, "2", 2, "4")); } - // -- PartialFunction + // -- asPartialFunction @Test public void shouldImplementPartialFunction() { - PartialFunction f = mapOf(1, "1"); + PartialFunction f = mapOf(1, "1").asPartialFunction(); assertThat(f.isDefinedAt(1)).isTrue(); assertThat(f.apply(1)).isEqualTo("1"); assertThat(f.isDefinedAt(2)).isFalse(); } + @Test + public void shouldApplyExistingKey() { + assertThat(emptyInt().put(1, 2).asPartialFunction().apply(1)).isEqualTo(2); + } + + @Test(expected = NoSuchElementException.class) + public void shouldApplyNonExistingKey() { + emptyInt().put(1, 2).asPartialFunction().apply(3); + } + // -- equality @Test @@ -532,18 +542,6 @@ public void shouldConvertToJavaMap() { assertThat(actual.toJavaMap()).isEqualTo(expected); } - // -- apply - - @Test - public void shouldApplyExistingKey() { - assertThat(emptyInt().put(1, 2).apply(1)).isEqualTo(2); - } - - @Test(expected = NoSuchElementException.class) - public void shouldApplyNonExistingKey() { - emptyInt().put(1, 2).apply(3); - } - // -- contains @Test @@ -1249,20 +1247,6 @@ public void lift() { assertThat(lifted.apply("a").isEmpty()).isTrue(); } - @Test - public void withDefaultValue() { - final Function1 withDef = mapOf("A", 1).withDefaultValue(2); - assertThat(withDef.apply("A")).isEqualTo(1); - assertThat(withDef.apply("a")).isEqualTo(2); - } - - @Test - public void withDefault() { - final Function1 withDef = mapOf("A", 1).withDefault(String::length); - assertThat(withDef.apply("A")).isEqualTo(1); - assertThat(withDef.apply("aaa")).isEqualTo(3); - } - // -- filter @Test diff --git a/vavr/src/test/java/io/vavr/collection/AbstractMultimapTest.java b/vavr/src/test/java/io/vavr/collection/AbstractMultimapTest.java index 0e60e7edfb..105902500d 100644 --- a/vavr/src/test/java/io/vavr/collection/AbstractMultimapTest.java +++ b/vavr/src/test/java/io/vavr/collection/AbstractMultimapTest.java @@ -339,16 +339,24 @@ public void shouldConstructFromJavaMap() { assertThat(map).isEqualTo(this. emptyMap().put("1", 2).put("3", 4)); } - // -- apply + // -- asPartialFunction @Test public void shouldApplyExistingKey() { - assertThat(emptyIntInt().put(1, 2).apply(1)).isEqualTo(io.vavr.collection.HashSet.of(2)); + assertThat(emptyIntInt().put(1, 2).asPartialFunction().apply(1)).isEqualTo(io.vavr.collection.HashSet.of(2)); } @Test(expected = NoSuchElementException.class) public void shouldApplyNonExistingKey() { - emptyIntInt().put(1, 2).apply(3); + emptyIntInt().put(1, 2).asPartialFunction().apply(3); + } + + @Test + public void shouldImplementPartialFunction() { + PartialFunction> f = mapOf(1, "1").asPartialFunction(); + assertThat(f.isDefinedAt(1)).isTrue(); + assertThat(f.apply(1).contains("1")).isTrue(); + assertThat(f.isDefinedAt(2)).isFalse(); } // -- asMap @@ -1207,16 +1215,6 @@ public void shouldThrowIfZipAllWithThatIsNull() { emptyMap().zipAll(null, null, null); } - // -- PartialFunction - - @Test - public void shouldImplementPartialFunction() { - PartialFunction> f = mapOf(1, "1"); - assertThat(f.isDefinedAt(1)).isTrue(); - assertThat(f.apply(1).contains("1")).isTrue(); - assertThat(f.isDefinedAt(2)).isFalse(); - } - // -- disabled super tests @Override diff --git a/vavr/src/test/java/io/vavr/collection/AbstractSeqTest.java b/vavr/src/test/java/io/vavr/collection/AbstractSeqTest.java index a276125f78..91d7bf5265 100644 --- a/vavr/src/test/java/io/vavr/collection/AbstractSeqTest.java +++ b/vavr/src/test/java/io/vavr/collection/AbstractSeqTest.java @@ -19,7 +19,6 @@ */ package io.vavr.collection; -import io.vavr.Function1; import io.vavr.Tuple; import io.vavr.control.Option; import io.vavr.Tuple2; @@ -251,11 +250,41 @@ public void shouldReturnSameSeqWhenNonEmptyAppendAllEmpty() { } } - // -- apply + // -- asPartialFunction @Test public void shouldUseSeqAsPartialFunction() { - assertThat(of(1, 2, 3).apply(1)).isEqualTo(2); + assertThat(of(1, 2, 3).asPartialFunction().apply(1)).isEqualTo(2); + } + + @Test + public void shouldNotBeDefinedAtNegativeIndexWhenEmpty() { + assertThat(empty().asPartialFunction().isDefinedAt(-1)).isFalse(); + } + + @Test + public void shouldNotBeDefinedAtNegativeIndexWhenNonEmpty() { + assertThat(of(1).asPartialFunction().isDefinedAt(-1)).isFalse(); + } + + @Test + public void shouldNotBeDefinedAtIndex0WhenEmpty() { + assertThat(empty().asPartialFunction().isDefinedAt(0)).isFalse(); + } + + @Test + public void shouldBeDefinedAtIndex0WhenNonEmpty() { + assertThat(of(1).asPartialFunction().isDefinedAt(0)).isTrue(); + } + + @Test + public void shouldBeDefinedAtLastIndexWhenNonEmpty() { + assertThat(of(1, 2, 3).asPartialFunction().isDefinedAt(2)).isTrue(); + } + + @Test + public void shouldNotBeDefinedAtIndexOutOfBoundsWhenNonEmpty() { + assertThat(of(1, 2, 3).asPartialFunction().isDefinedAt(3)).isFalse(); } // -- combinations @@ -903,38 +932,6 @@ public void shouldIntersperseMultipleElements() { assertThat(of('a', 'b').intersperse(',')).isEqualTo(of('a', ',', 'b')); } - // -- isDefinedAt() - - @Test - public void shouldNotBeDefinedAtNegativeIndexWhenEmpty() { - assertThat(empty().isDefinedAt(-1)).isFalse(); - } - - @Test - public void shouldNotBeDefinedAtNegativeIndexWhenNonEmpty() { - assertThat(of(1).isDefinedAt(-1)).isFalse(); - } - - @Test - public void shouldNotBeDefinedAtIndex0WhenEmpty() { - assertThat(empty().isDefinedAt(0)).isFalse(); - } - - @Test - public void shouldBeDefinedAtIndex0WhenNonEmpty() { - assertThat(of(1).isDefinedAt(0)).isTrue(); - } - - @Test - public void shouldBeDefinedAtLastIndexWhenNonEmpty() { - assertThat(of(1, 2, 3).isDefinedAt(2)).isTrue(); - } - - @Test - public void shouldNotBeDefinedAtIndexOutOfBoundsWhenNonEmpty() { - assertThat(of(1, 2, 3).isDefinedAt(3)).isFalse(); - } - // -- isSequential() @Test @@ -2279,28 +2276,4 @@ public void shouldTestIndexedSeqEndsWithNonIndexedSeq() { assertThat(of(1, 2, 3, 4).endsWith(Stream.of(2, 3, 5))).isFalse(); } - @Test - public void lift() { - final Function1> lifted = of("a", "b", "c").lift(); - assertThat(lifted.apply(1).get()).isEqualTo("b"); - assertThat(lifted.apply(-1).isEmpty()).isTrue(); - assertThat(lifted.apply(3).isEmpty()).isTrue(); - } - - @Test - public void withDefaultValue() { - final Function1 withDef = of("a", "b", "c").withDefaultValue("z"); - assertThat(withDef.apply(2)).isEqualTo("c"); - assertThat(withDef.apply(-1)).isEqualTo("z"); - assertThat(withDef.apply(3)).isEqualTo("z"); - } - - @Test - public void withDefault() { - final Function1 withDef = of("a", "b", "c").withDefault(Object::toString); - assertThat(withDef.apply(2)).isEqualTo("c"); - assertThat(withDef.apply(-1)).isEqualTo("-1"); - assertThat(withDef.apply(3)).isEqualTo("3"); - } - } diff --git a/vavr/src/test/java/io/vavr/collection/AbstractTraversableTest.java b/vavr/src/test/java/io/vavr/collection/AbstractTraversableTest.java index 4a8b5866a9..37b5dd24df 100644 --- a/vavr/src/test/java/io/vavr/collection/AbstractTraversableTest.java +++ b/vavr/src/test/java/io/vavr/collection/AbstractTraversableTest.java @@ -326,7 +326,7 @@ public void shouldCollectUsingCase() { @Test public void shouldCollectUsingMap() { final Map map = Map(1, "one", 3, "three"); - final Traversable actual = of(1, 2, 3, 4).collect(map); + final Traversable actual = of(1, 2, 3, 4).collect(map.asPartialFunction()); assertThat(actual).isEqualTo(of("one", "three")); } @@ -335,7 +335,7 @@ public void shouldCollectUsingMap() { public void shouldCollectUsingMultimap() { if (!isOrdered()) { final Multimap map = HashMultimap.withSeq().of(1, "one", 1, "un", 3, "three", 3, "trois"); - final Traversable> actual = of(1, 2, 3, 4).collect(map); + final Traversable> actual = of(1, 2, 3, 4).collect(map.asPartialFunction()); assertThat(actual).isEqualTo(of(List("one", "un"), List("three", "trois"))); } } @@ -343,7 +343,7 @@ public void shouldCollectUsingMultimap() { @Test public void shouldCollectUsingSeq() { final Seq map = List("one", "two", "three", "four"); - final Traversable actual = of(0, 2).collect(map); + final Traversable actual = of(0, 2).collect(map.asPartialFunction()); assertThat(actual).isEqualTo(of("one", "three")); } diff --git a/vavr/src/test/java/io/vavr/collection/CharSeqTest.java b/vavr/src/test/java/io/vavr/collection/CharSeqTest.java index fad43efb7e..d238dd1695 100644 --- a/vavr/src/test/java/io/vavr/collection/CharSeqTest.java +++ b/vavr/src/test/java/io/vavr/collection/CharSeqTest.java @@ -2184,11 +2184,11 @@ public void shouldThrowWhenAppendingAllIterableThatContainsNull() { assertThatThrownBy(() -> CharSeq.empty().appendAll(Arrays.asList('1', null))).isInstanceOf(NullPointerException.class); } - // -- apply + // -- asPartialFunction @Test public void shouldUseSeqAsPartialFunction() { - assertThat(CharSeq.of('1', '2', '3').apply(1)).isEqualTo('2'); + assertThat(CharSeq.of('1', '2', '3').asPartialFunction().apply(1)).isEqualTo('2'); } // -- combinations diff --git a/vavr/src/test/java/io/vavr/collection/QuickSortTest.java b/vavr/src/test/java/io/vavr/collection/QuickSortTest.java index ff9bab54b7..c23d76b9bf 100644 --- a/vavr/src/test/java/io/vavr/collection/QuickSortTest.java +++ b/vavr/src/test/java/io/vavr/collection/QuickSortTest.java @@ -19,10 +19,11 @@ */ package io.vavr.collection; -import io.vavr.Function1; import org.assertj.core.api.Assertions; import org.junit.Test; +import java.util.function.IntFunction; + import static java.lang.Integer.signum; import static io.vavr.API.List; import static org.assertj.core.api.Assertions.assertThat; @@ -43,7 +44,8 @@ private static Seq sort(Seq values) { } private static > List sort2(List values) { if (values.size() <= 1) return values; - final Function1> parts = values.groupBy(v -> signum(v.compareTo(values.head()))).withDefaultValue(List()); + final Map> map = values.groupBy(v -> signum(v.compareTo(values.head()))); + final IntFunction> parts = signum -> map.get(signum).getOrElse(List()); return sort2(parts.apply(-1)).appendAll(parts.apply(0)).appendAll(sort2(parts.apply(1))); } } diff --git a/vavr/src/test/java/io/vavr/collection/StreamTest.java b/vavr/src/test/java/io/vavr/collection/StreamTest.java index 16f2edaeeb..4fea5f2f95 100644 --- a/vavr/src/test/java/io/vavr/collection/StreamTest.java +++ b/vavr/src/test/java/io/vavr/collection/StreamTest.java @@ -609,7 +609,7 @@ public void shouldReturnTheOriginalStreamWhenTryingToExtendInfiniteStreamWithFun @Test public void shouldBeDefinedAtNonNegativeIndexWhenInfinitelyLong() { - assertThat(Stream.continually(1).isDefinedAt(1)).isTrue(); + assertThat(Stream.continually(1).asPartialFunction().isDefinedAt(1)).isTrue(); } // -- isLazy