Concept Flow - Take, drop, and chunked
Start with List
Apply operation
Return new List
End
Start with a list, pick one of take, drop, or chunked operations, apply it, and get a new list as result.
val list = listOf(1, 2, 3, 4, 5) val taken = list.take(3) val dropped = list.drop(2) val chunked = list.chunked(2)
| Step | Operation | Input List | Parameter | Result | Explanation |
|---|---|---|---|---|---|
| 1 | take | [1, 2, 3, 4, 5] | 3 | [1, 2, 3] | Take first 3 elements from the list |
| 2 | drop | [1, 2, 3, 4, 5] | 2 | [3, 4, 5] | Drop first 2 elements from the list |
| 3 | chunked | [1, 2, 3, 4, 5] | 2 | [[1, 2], [3, 4], [5]] | Split list into chunks of size 2 |
| 4 | End | - | - | - | All operations done, program ends |
| Variable | Start | After take | After drop | After chunked |
|---|---|---|---|---|
| list | [1, 2, 3, 4, 5] | [1, 2, 3, 4, 5] | [1, 2, 3, 4, 5] | [1, 2, 3, 4, 5] |
| taken | N/A | [1, 2, 3] | [1, 2, 3] | [1, 2, 3] |
| dropped | N/A | N/A | [3, 4, 5] | [3, 4, 5] |
| chunked | N/A | N/A | N/A | [[1, 2], [3, 4], [5]] |
Take, drop, and chunked are Kotlin list functions. - take(n): returns first n elements or fewer if list is smaller. - drop(n): skips first n elements, returns rest or empty if n too big. - chunked(n): splits list into sublists of size n, last may be smaller. Use these to easily slice and group lists.