From df3ed45e046763241c04dcb45288d1fbffb8d348 Mon Sep 17 00:00:00 2001 From: Yadu Krishnan Date: Sat, 11 May 2024 13:42:59 +0200 Subject: [PATCH 1/3] Code sample for filter and takeWhile method differences --- build.sbt | 7 ++++ .../scala-core-collections-4/README.md | 3 ++ .../FilterAndTakeWhileUnitTest.scala | 42 +++++++++++++++++++ 3 files changed, 52 insertions(+) create mode 100644 scala-core-collections-modules/scala-core-collections-4/README.md create mode 100644 scala-core-collections-modules/scala-core-collections-4/src/test/scala/com/baeldung/scala/filtertakewhile/FilterAndTakeWhileUnitTest.scala diff --git a/build.sbt b/build.sbt index 1cedbd74f..c3d7fb091 100644 --- a/build.sbt +++ b/build.sbt @@ -208,6 +208,13 @@ lazy val scala_core_collections_3 = libraryDependencies ++= scalaTestDeps ) +lazy val scala_core_collections_4 = + (project in file("scala-core-collections-modules/scala-core-collections-4")) + .settings( + scalaVersion := scala3Version, + libraryDependencies ++= scalaTestDeps + ) + lazy val scala_core_map = (project in file("scala-core-collections-modules/scala-core-map")) .settings( diff --git a/scala-core-collections-modules/scala-core-collections-4/README.md b/scala-core-collections-modules/scala-core-collections-4/README.md new file mode 100644 index 000000000..e5554115a --- /dev/null +++ b/scala-core-collections-modules/scala-core-collections-4/README.md @@ -0,0 +1,3 @@ +## Relevant Articles + +- diff --git a/scala-core-collections-modules/scala-core-collections-4/src/test/scala/com/baeldung/scala/filtertakewhile/FilterAndTakeWhileUnitTest.scala b/scala-core-collections-modules/scala-core-collections-4/src/test/scala/com/baeldung/scala/filtertakewhile/FilterAndTakeWhileUnitTest.scala new file mode 100644 index 000000000..80964369c --- /dev/null +++ b/scala-core-collections-modules/scala-core-collections-4/src/test/scala/com/baeldung/scala/filtertakewhile/FilterAndTakeWhileUnitTest.scala @@ -0,0 +1,42 @@ +package com.baeldung.scala.filtertakewhile + +import org.scalatest.matchers.should.Matchers +import org.scalatest.wordspec.AnyWordSpec + +import java.time.{LocalDate, LocalDateTime} +import scala.concurrent.{Await, Future, TimeoutException} +import scala.concurrent.ExecutionContext.Implicits.global +import scala.concurrent.duration.* + +class FilterAndTakeWhileUnitTest extends AnyWordSpec with Matchers { + + "filter" should { + "filter all elements matching the condition" in { + val numbers = List(1, 2, 3, 4, 5, 6) + val oddNumbers = numbers.filter(_ % 2 != 0) + oddNumbers shouldBe List(1, 3, 5) + } + + "not complete the execution due to infinite collection" in { + val infiniteNumbers = LazyList.from(1) + val lessThan100Future = Future(infiniteNumbers.filter(_ < 100).toList) + val lessThan100 = + intercept[TimeoutException](Await.result(lessThan100Future, 1.seconds)) + lessThan100.getMessage should include("Future timed out") + } + } + + "takeWhile" should { + "select elements until the predicate fails" in { + val numbers = List(1, 2, 3, 4, 5, 6) + val numbersBeforeFirstEven = numbers.takeWhile(_ % 2 != 0) + numbersBeforeFirstEven shouldBe List(1) + } + + "successfully get result from an infinite collection" in { + val infiniteNumbers = LazyList.from(1) + val first100 = infiniteNumbers.takeWhile(_ < 100).toList + first100 shouldBe (1 to 99).toList + } + } +} From d5c359c1990e4192c9994556ef86228dab79b5a2 Mon Sep 17 00:00:00 2001 From: Yadu Krishnan Date: Sun, 12 May 2024 21:17:15 +0200 Subject: [PATCH 2/3] remove test due to non-termination situation --- .../scala/filtertakewhile/FilterAndTakeWhileUnitTest.scala | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/scala-core-collections-modules/scala-core-collections-4/src/test/scala/com/baeldung/scala/filtertakewhile/FilterAndTakeWhileUnitTest.scala b/scala-core-collections-modules/scala-core-collections-4/src/test/scala/com/baeldung/scala/filtertakewhile/FilterAndTakeWhileUnitTest.scala index 80964369c..ac10a4ee5 100644 --- a/scala-core-collections-modules/scala-core-collections-4/src/test/scala/com/baeldung/scala/filtertakewhile/FilterAndTakeWhileUnitTest.scala +++ b/scala-core-collections-modules/scala-core-collections-4/src/test/scala/com/baeldung/scala/filtertakewhile/FilterAndTakeWhileUnitTest.scala @@ -17,12 +17,10 @@ class FilterAndTakeWhileUnitTest extends AnyWordSpec with Matchers { oddNumbers shouldBe List(1, 3, 5) } - "not complete the execution due to infinite collection" in { + "not complete the execution due to infinite collection" ignore { val infiniteNumbers = LazyList.from(1) - val lessThan100Future = Future(infiniteNumbers.filter(_ < 100).toList) val lessThan100 = - intercept[TimeoutException](Await.result(lessThan100Future, 1.seconds)) - lessThan100.getMessage should include("Future timed out") + infiniteNumbers.filter(_ < 100).toList // never completes } } From 295dd0c47323e29f678ef2bd28d21dc0ebe2d674 Mon Sep 17 00:00:00 2001 From: Yadu Krishnan Date: Sat, 18 May 2024 20:54:09 +0200 Subject: [PATCH 3/3] more test --- .../scala/filtertakewhile/FilterAndTakeWhileUnitTest.scala | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/scala-core-collections-modules/scala-core-collections-4/src/test/scala/com/baeldung/scala/filtertakewhile/FilterAndTakeWhileUnitTest.scala b/scala-core-collections-modules/scala-core-collections-4/src/test/scala/com/baeldung/scala/filtertakewhile/FilterAndTakeWhileUnitTest.scala index ac10a4ee5..23cddb503 100644 --- a/scala-core-collections-modules/scala-core-collections-4/src/test/scala/com/baeldung/scala/filtertakewhile/FilterAndTakeWhileUnitTest.scala +++ b/scala-core-collections-modules/scala-core-collections-4/src/test/scala/com/baeldung/scala/filtertakewhile/FilterAndTakeWhileUnitTest.scala @@ -31,6 +31,12 @@ class FilterAndTakeWhileUnitTest extends AnyWordSpec with Matchers { numbersBeforeFirstEven shouldBe List(1) } + "select different list of elements if the list item changes" in { + val numbers = List(1, 3, 4, 5, 6) + val numbersBeforeFirstEven = numbers.takeWhile(_ % 2 != 0) + numbersBeforeFirstEven shouldBe List(1, 3) + } + "successfully get result from an infinite collection" in { val infiniteNumbers = LazyList.from(1) val first100 = infiniteNumbers.takeWhile(_ < 100).toList