groostav
10/01/2020, 9:57 PMconciseExpr(listOf("A", "B"), 5) //output: [A, B, A, B, A]
conciseExpr(listOf("A", "B"), 2) //output: [A, B]
conciseExpr(listOf("A", "B"), 1) //output: [A]
you are gaurenteed that the input list is non-empty
so far I'm working with
val output = inputList
.let { partial -> (0 until 1+(inputTargetSize / partial.size)).map { partial }.flatten() }
.take(inputTargetSize)
but i feel like 0 until 1+expr
is cloogygroostav
10/01/2020, 10:00 PMPositiveIntegersInfSeq
.map { idx -> inputList[idx % inputList.size] }
.take(inputTargetSize)
.toList()
which seems pretty elegantgroostav
10/01/2020, 10:01 PMMapIndexed
operator...nanodeath
10/01/2020, 10:17 PM(0..inputTargetSize).map { inputList[it % inputList.size] }
thingNir
10/01/2020, 11:35 PMNir
10/01/2020, 11:36 PMNir
10/01/2020, 11:38 PMNir
10/01/2020, 11:38 PMNir
10/01/2020, 11:49 PMNir
10/01/2020, 11:49 PMNir
10/01/2020, 11:53 PM0..4.map { inputList [it % myList.length] }
nanodeath
10/02/2020, 12:07 AMnanodeath
10/02/2020, 12:08 AMNir
10/02/2020, 12:49 AMephemient
10/02/2020, 1:02 AMfun <T> Iterable<T>.cycle(): Sequence<T> = sequence {
while (true) {
yieldAll(this@cycle)
}
}
listOf("A", "B").cycle().take(5).toList() // => [A, B, A, B, A]
nanodeath
10/02/2020, 1:04 AMwhile(true) {
to repeat(2) {
and you have the .bicycle
extensionNir
10/02/2020, 1:05 AMNir
10/02/2020, 1:05 AMNir
10/02/2020, 1:06 AMNir
10/02/2020, 1:06 AMNir
10/02/2020, 1:20 AMthis@cycle
syntax is doingephemient
10/02/2020, 2:13 AMsequence(block: suspend SequenceScope<T>.() -> Unit)
lambda, this
would refer to the SequenceScope<T>
receiverephemient
10/02/2020, 2:13 AMthis@cycle
means "the this
from the cycle
scopeephemient
10/02/2020, 2:14 AMOuter.this
in class Outer { class Inner { { System.out.println(Outer.this); } } }
Nir
10/02/2020, 2:25 AMNir
10/02/2020, 2:26 AMNir
10/02/2020, 2:26 AMNir
10/02/2020, 2:27 AM