Szymon Sasin
12/05/2022, 7:02 PMval s = sequenceOf("Lorem", "ipsum", "dolor", "sit", "amet")
What’s the best way to limit elements so total size is smaller than desired.
For example:
val s2 = s.someFunction(maxSize = 17) { it.length }
println(s2.toList()) // [Lorem, ipsum, dolor]
chiroptical
12/05/2022, 7:05 PMs.filter { it.length < 17 }
?chiroptical
12/05/2022, 7:06 PMSzymon Sasin
12/05/2022, 7:06 PMchiroptical
12/05/2022, 7:07 PMPair<Sequence<String>, Int>
or something?Starr
12/05/2022, 7:14 PMlimit(Int)
method?
oh nvm you don't want to limit the number of elements. though maybe flatMap{ it.chars.asSequence() }.limit(17)
would work? though it wouldn't keep the original stringsSzymon Sasin
12/05/2022, 7:27 PMasSequence
does not work.ephemient
12/05/2022, 8:16 PMvar total = 0
val limit = s.indexOfFirst {
total += it.length
total > 17
}
val s2 = s.take(if (limit >= 0) limit else s.size)
ephemient
12/05/2022, 8:19 PMval s2 = buildList {
var total = 0
for (t in s) (
total += t.length
if (total > 17) break
add(t)
}
}
Szymon Sasin
12/05/2022, 9:07 PM