`zipWithNext` will group items together into pairs...
# stdlib
s
zipWithNext
will group items together into pairs if that works for you
d
sounds interesting, will have a look at it, thank you
I wish it would zip first and last element with null or something like that as well
s
uhh, that’d change your pair type from
T
to
T?
, but couldn’t you more or less do that by bolting on a
sequenceOf(null)
to both ends of your sequence?
d
yeah, my use case is: I need to map through collection of addresses that have only "move in dates" and produces collection of addresses with "end dates" which we infer from the "move in" date of the next address, problem is with the last address which gets dropped rather than letting me do something with it
appending a null would be one solution I guess
just feels a bit hacky and probably a bit difficult to understand for the next person
s
you could pack it into a well-named extension function
d
Have you looked at
chunked
?
p
Or
windowed
d
hmm that might work actually
actually it doesn’t work because instead of doing say (1,2,3) -> ((1,2), (2,3), (3, null)) it will just do ((1,2), (3))
p
windowed(2, partialWindows = true)
will return
[[1, 2], [2, 3], [3]]
you can use
getOrNull(1)
to get second item or
null
but yeah, this doesn’t look good
👍 1