Skip to content

Instantly share code, notes, and snippets.

@fedefernandez
Created April 12, 2023 08:03
Show Gist options
  • Save fedefernandez/c60824b4af90fa51243c6b0e19befd35 to your computer and use it in GitHub Desktop.
Save fedefernandez/c60824b4af90fa51243c6b0e19befd35 to your computer and use it in GitHub Desktop.
Test LazyList in scala-compat
ThisBuild / version := "0.1.0-SNAPSHOT"
ThisBuild / scalaVersion := "2.12.17"
ThisBuild / crossScalaVersions := Seq("2.12.17", "2.13.8")
lazy val root = (project in file("."))
.settings(
libraryDependencies += "org.scala-lang.modules" %% "scala-collection-compat" % "2.9.0"
)
sbt +run
[info] welcome to sbt 1.8.0 (Oracle Corporation Java 11.0.2)
[info] loading settings for project global-plugins from plugins.sbt,sbt-updates.sbt ...
[info] loading global plugins from /Users/fede/.sbt/1.0/plugins
[info] loading project definition from /Users/fede/development/workspace/scala-2.12/project
[info] loading settings for project root from build.sbt ...
[info] set current project to root (in build file:/Users/fede/development/workspace/scala-2.12/)
[info] Setting Scala version to 2.12.17 on 1 projects.
[info] Reapplying settings...
[info] set current project to root (in build file:/Users/fede/development/workspace/scala-2.12/)
[info] compiling 1 Scala source to /Users/fede/development/workspace/scala-2.12/target/scala-2.12/classes ...
[info] running TestCollection
3 shouldBe 3
4 shouldBe 4
6 shouldBe 6
[success] Total time: 5 s, completed Apr 12, 2023, 10:02:53 AM
[info] Setting Scala version to 2.13.8 on 1 projects.
[info] Reapplying settings...
[info] set current project to root (in build file:/Users/fede/development/workspace/scala-2.12/)
[info] compiling 1 Scala source to /Users/fede/development/workspace/scala-2.12/target/scala-2.13/classes ...
[info] running TestCollection
3 shouldBe 3
4 shouldBe 4
6 shouldBe 6
[success] Total time: 4 s, completed Apr 12, 2023, 10:02:57 AM
[info] Reapplying settings...
[info] set current project to root (in build file:/Users/fede/development/workspace/scala-2.12/)
import scala.collection.compat._
object TestCollection extends App {
var rec = 0
def llRange(lo: Int, hi: Int): immutable.LazyList[Int] = {
rec = rec + 1
if (lo >= hi) immutable.LazyList.empty
else immutable.LazyList.cons(lo, llRange(lo + 1, hi))
}
llRange(1, 10).take(3).toList
println(s"$rec shouldBe 3")
llRange(1, 10).take(1).toList
println(s"$rec shouldBe 4")
llRange(1, 10).take(2).toList
println(s"$rec shouldBe 6")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment