Gus07/20/2020, 5:41 PM
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()
should contain an array of messages (taken from
) which collectively don't exceed 8 MiB. If
is empty, the result should be empty too. If all the
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
but not that per se), and another for the computation of each
). But I don't think
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 Berger07/20/2020, 5:55 PM
Gus07/20/2020, 5:57 PM
So 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
and `yield`ing the values?
Tobias Berger07/20/2020, 6:05 PM
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.
Gus07/20/2020, 6:21 PM
Tobias Berger07/20/2020, 6:24 PM
). I have not tested this at all but it looks like it should work https://gist.github.com/tobiberger/178f436d23fa40f388f4c891921cf0f7
Gus07/20/2020, 6:31 PM