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:
Given n: Int
, for i <- 2 to 6
, return true if getCanonical(n * i)
is equal in all cases
Stream lazily until the condition is met
–
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
Return Stream
where f(n) < max
Rest of my solution here