mg6maciej
03/31/2017, 10:00 AMval (first4, fifthAndBeyond) = list.takeAndDrop(4)
enleur
03/31/2017, 10:08 AMenleur
03/31/2017, 10:11 AMmg6maciej
03/31/2017, 10:12 AMmg6maciej
03/31/2017, 10:13 AMlist.withIndex().partition { it.index < 4 }.map { it.value }
mg6maciej
03/31/2017, 10:14 AMpartition
, but I just want to use take
and drop
inside.enleur
03/31/2017, 10:14 AMval (first4, fifthAndBeyond) = list.withIndex().partition { it.index < 4 }
mg6maciej
03/31/2017, 10:15 AMpartition
returns Pair
, so I can't map on it.mg6maciej
03/31/2017, 10:16 AMsplitAt
, so I'll go with this name.mg6maciej
03/31/2017, 11:00 AMfun <T> List<T>.splitAt(index: Int) = Pair(take(index), drop(index))
Maybe it will appear in #stdlib some day.kirillrakhman
03/31/2017, 11:47 AMmg6maciej
03/31/2017, 11:47 AMsplitAt
?kirillrakhman
03/31/2017, 11:47 AMmg6maciej
03/31/2017, 11:48 AMmg6maciej
03/31/2017, 11:49 AMval (receivedItems, newItems) = items.splitAt(receivedItemsCount)
mg6maciej
03/31/2017, 11:50 AMnewItems
and at the end I return a copy: return copy(..., items = newItems)
kirillrakhman
03/31/2017, 11:50 AMmg6maciej
03/31/2017, 11:53 AMtake
and drop
before refactoring, but I didn't like how it wasn't DRY. In this example there is receivedItemsCount
variable, but in most places it was 1
or 5
. And I got that constant twice in a block of code.kirillrakhman
03/31/2017, 11:54 AMkirillrakhman
03/31/2017, 11:54 AMkirillrakhman
03/31/2017, 11:54 AMmg6maciej
03/31/2017, 11:54 AMmg6maciej
03/31/2017, 11:55 AMfun <T> List<T>.splitAt(index: Int) = Pair(subList(0, index), subList(index, size))
kirillrakhman
03/31/2017, 11:55 AMmg6maciej
03/31/2017, 11:56 AMkirillrakhman
03/31/2017, 11:56 AMsreich
03/31/2017, 12:40 PMkirillrakhman
03/31/2017, 12:40 PMmg6maciej
03/31/2017, 1:06 PMaleix
03/31/2017, 1:09 PM