diff --git a/build.sbt b/build.sbt index 211cdfa3c..36f4ec310 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..23cddb503 --- /dev/null +++ b/scala-core-collections-modules/scala-core-collections-4/src/test/scala/com/baeldung/scala/filtertakewhile/FilterAndTakeWhileUnitTest.scala @@ -0,0 +1,46 @@ +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" ignore { + val infiniteNumbers = LazyList.from(1) + val lessThan100 = + infiniteNumbers.filter(_ < 100).toList // never completes + } + } + + "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) + } + + "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 + first100 shouldBe (1 to 99).toList + } + } +}