Gus
07/20/2020, 5:41 PMsequence {}
but it feels like an overkill to use a couroutine for this.
Consider the code below:
data class MessageWithExpiry(
val messageSerialized: ByteArray,
val expiryDate: ZonedDateTime
)
data class Batch(
val messages: Array<ByteArray>,
val latestMessageExpiryDate: ZonedDateTime
)
fun batchMessages(messages: Sequence<MessageWithExpiry>): Sequence<Batch> = TODO()
Each Batch
should contain an array of messages (taken from MessageWithExpiry.messageSerialized
) which collectively don't exceed 8 MiB. If messages
is empty, the result should be empty too. If all the messages
fit in one batch (i.e., they don't exceed the 8 MiB limit collectively), then only one batch should be output -- otherwise, multiple `Batch`es should be output. Additionally, each batch should have the latest expiry date of any message in the batch.
Intuitively, I think I should split this in two parts: One for the batching itself (using something like Sequence.scan
but not that per se), and another for the computation of each Batch.latestMessageExpiryDate
(using Sequence.map
). But I don't think Sequence.scan
, Sequence.reduce
, Sequence.fold
or any of their variations would help me do the first part.
Any pointers on whether/how I can do this using a functional approach?Tobias Berger
07/20/2020, 5:55 PMGus
07/20/2020, 5:57 PMSo if I understood correctly, you don't want to group by a certain property, but you just want to build batches of multiple messages (up to a certain size).That's right
The only way I see to achieve this is implementing a custom Sequence that combines multiple elements of the original sequence into one when the next element is fetched.Sorry, what do you mean by custom sequence? Using
sequence {}
and `yield`ing the values?Tobias Berger
07/20/2020, 6:05 PMSequence
in kotlin is actually just an interface that requires you to provide an interator. You can implement it as a class without any predefined kotlin functions. But that's rarely necessary.Gus
07/20/2020, 6:21 PMTobias Berger
07/20/2020, 6:24 PMsequence{}
and yield
). I have not tested this at all but it looks like it should work
https://gist.github.com/tobiberger/178f436d23fa40f388f4c891921cf0f7Gus
07/20/2020, 6:31 PM