Zoltan Demant
08/10/2022, 10:51 AMif(contains) remove else add
but feel dirty everytime.Sam Stone
08/12/2022, 5:31 AMsreich
08/12/2022, 5:09 PMcliOptions += CliOption()
clioptions is a java List
Assignment operators ambiguity. All these functions match.
public operator fun <T> Collection<CliOption!>.plus(element: CliOption!): List<CliOption!> defined in kotlin.collections
public inline operator fun <T> MutableCollection<in CliOption>.plusAssign(element: CliOption): Unit defined in kotlin.collections
Zoltan Demant
08/13/2022, 5:58 AMrun
always result in kotlin.run {}
whereas with apply
(and others) you just end up with a nice apply {}
?Zoltan Demant
08/14/2022, 3:08 AM@JvmInline
will be deprecated/unnecessary in the near or far future?jeff
08/17/2022, 7:41 PMval seq: Sequence<Int> = ...
seq.takeWhile { it < 100 }.forEach { println("small: $it") }
seq.drop(???).takeWhile { it < 200 }.forEach { println("medium: $it") }
seq.drop(???).forEach { it < 300 }.forEach { println("large: $it" }
but I’m wondering if there’s something more idiomatic/elegant than the `drop()`s. I tried having the Sequence maintain the same iterator, but that skips the items for which the takeWhile {}
returns false (because next()
is called regardless). Any ideas?Rob Elliot
08/23/2022, 4:10 PMfun <K, V> Iterable<Pair<K, V>>.groupByKey(): Map<K, List<V>> = this.groupBy({ it.first }, { it.second})
Is it a bad idea?Zoltan Demant
08/24/2022, 3:50 AMClass<T>.newInstance()
to invoke a no-args constructor of T. Is it possible to do something similar for an object in kotlin while only having access to Class<T>
where I know that T is an object, without using reflection?xxfast
09/06/2022, 11:51 PMRob Elliot
09/12/2022, 10:51 AMList<T>
into a T?
as so:
return when (size) {
0 -> null
1 -> this[0]
else -> throw IllegalArgumentException("List has more than one element.")
}
And should it be in the stdlib? We have single
(which throws if size != 1) and singleOrNull
(which returns null if size != 1) but I find myself wanting this function more than singleOrNull
. I'm often calling some kind of query API which returns a Collection but where I know I expect 0..1, and if it returns >1 I want to fail and find out, not hide it as null.elect
09/26/2022, 1:08 PMtoRadians
and toDegrees
. Both are present in java.lang.Math
but for MP it's a problemPHondogo
09/28/2022, 7:45 PMjobot0
10/11/2022, 1:25 PM1.6.10
however I’m running against this issue [KT-48918] which seems to be fixed in 1.5.31
. Does someone have any insight about it?Settingdust
10/16/2022, 3:02 AMInt
?Sam Stone
10/23/2022, 2:46 AM2.repeat(init = 10) { acc -> acc * 10 } == 1_000
(to repeat 10*10 twice, or 10*10*10) instead of (0..1).fold(10) { acc, _ -> acc * 10 } = 1_000
Szymon Jeziorski
10/26/2022, 10:05 AMfun <T> List<T>.cut(cutIndex: Int): Pair<List<T>, List<T>> {
if (cutIndex !in indices) throw IndexOutOfBoundsException("cutIndex $cutIndex out of bounds for List indices $indices")
return subList(0, cutIndex) to subList(cutIndex, size)
}
fun <T> List<T>.cutInHalf(): Pair<List<T>, List<T>> {
val midIndex = size / 2
return subList(0, midIndex) to subList(midIndex, size)
}
Having val sampleList = listOf(1, 8, 3, 9, 12, 4)
sampleList.cutInHalf()
would result in ([1, 8, 3], [9, 12, 4])
sampleList.cut(4)
would result in ([1, 8, 3, 9], [12, 4])
etc.
cutInHalf
wouldn't be ideal for Lists with odd number of elements, but I guess that should be obviousSam Stone
10/26/2022, 5:29 PMelect
10/27/2022, 9:04 AMcapitalize()
: Gradle is using capitalized()
elect
10/28/2022, 9:06 PMMichael Marshall
10/31/2022, 2:01 AMfun <A, B> myFunction(a: A, b: B) where A !: B {
...
}
Sam Stone
11/03/2022, 10:20 PMdata class Foo(a: String, b: String)
val foo: Foo = listOf("one", "two").mapToClass<Foo>()
There wouldn’t necessarily be type-safety, but it would be a cool language feature. It comes up often when parsing CSVs. I end up doing
Files.readAllLines(path).map {
it.split(",").let {
Foo(
it[0],
it[1]
...
)
}
}
Instead of being able to do Files.readAllLines(path).toCSV<Foo>()
or Files.readAllLines(path).map { it.split(",").mapToClass<Foo>() }
elect
11/06/2022, 9:31 PMfilterIsInstance
inline reified like
filterIsInstance<Type.Struct>()
instead of
filterIsInstance(Type.Struct::class.java)
?jeggy
11/10/2022, 5:04 PMfun main() {
val num: Number = 25
println(num > 10)
}
Shouldn't something like this be possible by the stdlib?Klitos Kyriacou
11/18/2022, 4:20 PMpartitionTo
function?
fun <T, C1 : MutableCollection<in T>, C2 : MutableCollection<in T>> Iterable<T>.partitionTo(
trueDestination: C1,
falseDestination: C2,
predicate: (T) -> Boolean
): Pair<C1<T>, C2<T>>
This will return the same value as Pair(this.filterTo(trueDestination, predicate), this.filterTo(falseDestination, predicate))
but obviously more efficiently, without iterating twice.elect
11/30/2022, 3:05 PMcontains
on List<String>
with caseInsensitive: Boolean
?dave08
12/04/2022, 1:06 PMval str = "some-string"
val (key: String, value: String?) = str.split('=', limit = 2)
// value should contain null -- but this crashes saying it tried accessing the list's 1 index which doesn't exist...
does that make any sense? I'm not sure this is really possible though -- and if the String?
is not specified, then I would have expected it to default to String
and crash... otherwise this kind of thing could cause subtle bugs... (?)Big Chungus
12/05/2022, 11:36 AMMutableList<T>.removeLast(count: Int): List<T>
? 😄Szymon Jeziorski
12/05/2022, 12:41 PMArrayDeque<E>
and its implementation of MutableList<E>.removeAll(elements: Collection<E>)
, it doesn't work when elements
are view created by subList
. Method's invocation doesn't produce any kind of RuntimeException, but no elements are actually removed.
example:
val arrayDeque = ArrayDeque((1..10).toList())
val subList = arrayDeque.subList(arrayDeque.size - 3, arrayDeque.size)
arrayDeque.removeAll(subListł)
println(arrayDeque) // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
when passed new list object as an argument for example by calling slice
, it all works fine:
val arrayDeque = ArrayDeque((1..10).toList())
val subList = arrayDeque.slice(arrayDeque.size - 3..arrayDeque.lastIndex)
arrayDeque.removeAll(subList)
println(arrayDeque) // [1, 2, 3, 4, 5, 6, 7]
Using different kind of MutableList also works just fine:
val mutableList = (1..10).toMutableList()
val subList = mutableList.subList(mutableList.size - 3, mutableList.size)
mutableList.removeAll(subList)
mutableList // [1, 2, 3, 4, 5, 6, 7]
Also, invoking .subList(...).clear(...)
on ArrayDeque results in the same issue, no elements are being removed
Should this be reported as a bug?Giorgi
12/10/2022, 5:01 PMfun Any?.println() = println(this)
function? I know there is already a .sout
thing, but why would one use it if there will be println extensionBig Chungus
12/27/2022, 10:20 AM