Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active December 23, 2015 00:58
Show Gist options
  • Save dacr/6556768 to your computer and use it in GitHub Desktop.
Save dacr/6556768 to your computer and use it in GitHub Desktop.
Two doubles generator function
scala> def doubler(start:Long):Stream[Long]=start#::doubler(2*start)
doubles: (start: Long)Stream[Long]
scala> doubler(1250).take(12).toList
res21: List[Long] = List(1250, 2500, 5000, 10000, 20000, 40000, 80000, 160000, 320000, 640000, 1280000, 2560000)
scala> def doubler[N](start:N)(implicit n: Numeric[N]):Stream[N]=start#::doubler(n.plus(start,start))
doubles: [N](start: N)(implicit n: Numeric[N])Stream[N]
scala> doubler(1250d).take(6).toList
res23: List[Double] = List(1250.0, 2500.0, 5000.0, 10000.0, 20000.0, 40000.0)
scala> doubler(1250).take(6).toList
res24: List[Int] = List(1250, 2500, 5000, 10000, 20000, 40000)
scala> doubler(BigDecimal(1250)).take(6).toList
res25: List[scala.math.BigDecimal] = List(1250, 2500, 5000, 10000, 20000, 40000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment