Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active May 27, 2023 06:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dacr/8db8b1273bbcee1173cf39e1936b3080 to your computer and use it in GitHub Desktop.
Save dacr/8db8b1273bbcee1173cf39e1936b3080 to your computer and use it in GitHub Desktop.
Collection operations / published by https://github.com/dacr/code-examples-manager #3783b8e4-99a6-44b4-b106-d06cd5646a96/e217a26895cc4f9d14400653e982c0c8ec023f9f
// summary : Collection operations
// keywords : scala, scalatest, collection, cheatsheet, @testable
// publish : gist
// authors : David Crosson
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2)
// id : 3783b8e4-99a6-44b4-b106-d06cd5646a96
// created-on : 2021-03-04T20:00:03Z
// managed-by : https://github.com/dacr/code-examples-manager
// run-with : scala-cli $file
// ---------------------
//> using scala "3.3.0"
//> using dep "org.scalatest::scalatest:3.2.16"
//> using objectWrapper
// ---------------------
import java.text.spi.DecimalFormatSymbolsProvider
import java.text.{DecimalFormat, DecimalFormatSymbols, NumberFormat}
import org.scalatest._
import flatspec._
import matchers._
class CollectionOperations extends AnyFlatSpec with should.Matchers {
override def suiteName="CollectionOperations"
// ---------------------------------------------------------------------------------------------
"A collection" should "support safe get by index with lift" in {
Array(42).lift(0) shouldBe Some(42)
Array(42).lift(42) shouldBe None
Array(42).lift(-42) shouldBe None
List("a","b","c").lift(2) shouldBe Some("c")
List("a","b","c").lift(42) shouldBe None
List("a","b","c").lift(-42) shouldBe None
}
// ---------------------------------------------------------------------------------------------
it should "replace any kind of loop without any constraint" in {
info("iterator .iterate advantageously .from method as it fully generic")
LazyList
.iterate(10L)(_ + 1)
.zip(Iterator.iterate(0L)(_ + 1))
.takeWhile{ case(value, index) => index < 5}
.map{ case (value, index)=>value}
.sum shouldBe 10+11+12+13+14
}
// ---------------------------------------------------------------------------------------------
}
org.scalatest.tools.Runner.main(Array("-oDF", "-s", classOf[CollectionOperations].getName))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment