Streaming

Learn how to implement the streaming of our products.

Take 1 - Merging products

Because we believe ourselves to be clever, we pick the simple sledgehammer approach and just run some accumulator on the stream. We need a helper function and some code changes on the stream (e.g., in the route).

Press + to interact
def merge(ps: List[Product])(p: Product): List[Product] =
ps.headOption.fold(List(p)) { h =>
if (h.id === p.id)
h.copy(names = h.names ::: p.names) :: ps.drop(1)
else
p :: ps
}

So this function will take a list (that may be empty) and a Product, and will merge the topmost element (the head) of the list with the given Product. It will return an updated ...

Access this course and 1400+ top-rated courses and projects.