more on scala Streams
Been a crazy week of Hackathon with no energy for standard in
! Back today with some more fundamentals. The Scala Stream
class is very handy for problem-solving like with Project Euler. I’ve been using it as an alternative to the imperative style looping, i.e. using a mutable counter and incrementing it until a condition is met.
Use case one: Stream
all numbers from 1 until a condition is met
Example in the wild: Project Euler Problem 52
Turn a number into a canonical representation:
def getCanonical(n: Int): String = n.toString.split("").sorted.mkString
(e.getCanonical(125874) == e.getCanonical(251748)) should be(true)
Given n: Int
, for i <- 2 to 6
, return true if getCanonical(n * i)
is equal in all cases
def allAreSame(n: Int): Boolean = {
(2 to 6).map(_ * n).map(getCanonical).distinct.size == 1
}
Stream lazily until the condition is met
Stream.from(1).find(allAreSame)
// Option[142857]
–
Use case two: given a map function f = (Int) => Int
and a value, max: Int
, find all values of n
where f(n) < max
Example in the wild: Project Euler Problem 42
Map function
def computeTriangle(n: Int): Int = ((0.5 * n) * (n + 1)).toInt
Return Stream
where f(n) < max
def triNumsUnder(max: Int): Stream[Int] = {
Stream.from(1).map(computeTriangle).takeWhile(_ < max)
}
e.triNumsUnder(56).toList should be(List(1, 3, 6, 10, 15, 21, 28, 36, 45, 55))
Rest of my solution here