I'm working on a problem in Scala where I have two input Seq[Int]
, A
and B
, and an value function f
and the goal is to find the max value of f
for any pair of elements (a, b)
in A
and B
.
We can do this naively with a for-comprehension:
val A = Seq(1, 2, 3, 4, 5)
val B = Seq(6, 7, 8, 9, 10)
def f(a: Int, b: Int): Int = a * b // f can be more complex than just a multiplication. This is just an example.
val results = for {
a <- A
b <- B
} yield f(a, b)
results.max
The problem that I'm running into is that A
and B
can be very large and computing every combination of (a, b)
is causing memory errors. I don't actually care about the entire list of (a, b)
or even the entire list of results
either. I only really care about the maximum value.
Is is possible to stream the list of values produced by the for-comprehension into f
and then into the max function so that I don't have to hold the entire list in memory?
Notes
- The problem that I'm working on is Leetcode #11 - Container with Most Water. I know about the linear time solution, but what I'm really curious about here is whether we can stream a logical sequence of values into an aggregator function in Scala.
- In my research, I found scala
Streams
, which look like the previous implementation of the currentLazyList
class, but these don't look like they will give me the behavior that I want. I don't think that 'stream' is the right word to describe the behavior I want in scala, but I'm not sure what to search for next.