The Startup
Published in

The Startup

Applying a function to just one previous term in a Scala lazy collection

Photo by freestocks on Unsplash
val fibs: LazyList[BigInt] = BigInt(0) #:: BigInt(1) #:: fibs.zip(fibs.tail).map { n => n._1 + n._2 }
scala> fibs(100)
res12: BigInt = 354224848179261915075
def fibonacci(n: Int): BigInt = n match {
case n if n < 0 => (if (n % 2 == 0) -1 else 1) * fibonacci(-n)
case 0 => 0
case 1 => 1
case n => fibonacci(n - 2) + fibonacci(n - 1)
}
scala> (-10 to 10).map(fibonacci(_))
res13: IndexedSeq[BigInt] = Vector(-55, 34, -21, 13, -8, 5, -3, 2, -1, 1, 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55)
scala> fibonacci(100)
Terminate batch job (Y/N)? y
C:\Users\AL>scala
Welcome to Scala 2.13.1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_251).
Type in expressions for evaluation. Or try :help.
scala> 1 + 1
res0: Int = 2
def fibonacci(n: Int): BigInt = if (n < 0) {
fibs(-n) * (if (n % 2 == 0) -1 else 1)
} else fibs(n)
scala> (-10 to 10).map(fibonacci(_))
res1: IndexedSeq[BigInt] = Vector(-55, 34, -21, 13, -8, 5, -3, 2, -1, 1, 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55)
scala> fibonacci(-100)
res2: BigInt = -354224848179261915075
scala> val pow7Attempt: LazyList[BigInt] = BigInt(1) #:: BigInt(7) #:: pow7Attempt.zip(pow7Attempt.tail).map { n => n._2 * 7 }
pow7Attempt: LazyList[BigInt] = LazyList(<not computed>)
scala> pow7Attempt.takeWhile(_ < Integer.MAX_VALUE).toList
res3: List[BigInt] = List(1, 7, 49, 343, 2401, 16807, 117649, 823543, 5764801, 40353607, 282475249, 1977326743)
scala> val powersOf7: LazyList[BigInt] = LazyList.iterate(1: BigInt)(_ * 7)
powersOf7: LazyList[BigInt] = LazyList(<not computed>)
scala> (0 to 13).map(powersOf7(_))
res4: IndexedSeq[BigInt] = Vector(1, 7, 49, 343, 2401, 16807, 117649, 823543, 5764801, 40353607, 282475249, 1977326743, 13841287201, 96889010407)
scala> val serialSevens: LazyList[Int] = LazyList.iterate(100)(_ - 7)
serialSevens: LazyList[Int] = LazyList(<not computed>)
scala> serialSevens.takeWhile(_ > 0).toList
res7: List[Int] = List(100, 93, 86, 79, 72, 65, 58, 51, 44, 37, 30, 23, 16, 9, 2)
scala> serialSevens
res8: LazyList[Int] = LazyList(100, 93, 86, 79, 72, 65, 58, 51, 44, 37, 30, 23, 16, 9, 2, -5, <not computed>)
class ComplexNumber(val re: Double, val im: Double = 0.0) {  def +(addend: ComplexNumber): ComplexNumber =
new ComplexNumber(this.re + addend.re, this.im + addend.im)
def *(multiplicand: ComplexNumber): ComplexNumber =
new ComplexNumber(this.re * multiplicand.re - this.im *
multiplicand.im,
this.re * multiplicand.im + this.im * multiplicand.re)
override def toString: String = this.re.toString + " + " +
this.im.toString + "i"
}
scala> val zero = new ComplexNumber(0.0)
zero: ComplexNumber = 0.0 + 0.0i
scala> val douadyRabbit = new ComplexNumber(-0.12256, 0.74486)
douadyRabbit: ComplexNumber = -0.12256 + 0.74486i
scala> val mandelEscape: LazyList[ComplexNumber] = LazyList.iterate(zero)(z => z * z + douadyRabbit)
mandelEscape: LazyList[ComplexNumber] = LazyList(<not computed>)
scala> mandelEscape.take(10).toList
res10: List[ComplexNumber] = List(0.0 + 0.0i, -0.12256 + 0.74486i, -0.662355466 + 0.5622799168i, -3.941496537818168E-6 + 1.6473709895103994E-6i, -0.12255999998717844 + 0.7448599999870138i, -0.662355465983797 + 0.5622799168222836i, -3.9415430613254365E-6 + 1.6473596913257893E-6i, -0.12255999998717804 + 0.7448599999870137i, -0.662355465983797 + 0.5622799168222843i, -3.9415430620470815E-6 + 1.6473596904376109E-6i)
scala> val reflections: LazyList[String] = LazyList.iterate("Hello")(s => s + s.reverse)
reflections: LazyList[String] = LazyList(<not computed>)
scala> reflections.takeWhile(_.length < 100).toList
res12: List[String] = List(Hello, HelloolleH, HelloolleHHelloolleH, HelloolleHHelloolleHHelloolleHHelloolleH, HelloolleHHelloolleHHelloolleHHelloolleHHelloolleHHelloolleHHelloolleHHelloolleH)

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alonso Del Arte

is a Java and Scala developer from Detroit, Michigan. AWS Cloud Practitioner Foundational certified