WukongRework.exe
07/24/2023, 8:48 PMrunningReduceRight
and `runningFoldRight`/`scanRight` for symmetry with the reduce
and reduceRight
/ fold
and foldRight
functions. Is there a technical reason behind this? I often find myself ad-hoc-ing foldRight
to be a scanRight
for LeetCode and other competitive programming problems.Zoltan Demant
07/26/2023, 11:21 AM@JvmInline
value class Token(
private val value: String,
) {
constructor() : this("1337") {
// **THIS**
println(value)
}
}
Marco Pierucci
07/31/2023, 2:16 PMYoussef Shoaib [MOD]
07/31/2023, 2:24 PMcontext(A) fun <A> given(): A = this@A
to be used like given<List<String>>()
. I think this is better than the this@
syntax since it supports generics (like List<String>
) and can "summon" supertypes of a context receiver which gets resolved to the apt subtype context
Edit: I realise that maybe stdlib can't have context
declarations. Instead, it can be defined equivalently as:
fun <A> A.given(): A = this
The only issue is that it pollutes the namespace of Any.
Harold Martin
08/04/2023, 4:11 PMput
on MutableMaps. I have a simple extension function:
internal fun MutableMap<String, JsonElement>.putAsJsonElement(key: String, value: String?) {
value?.let { this.put(key, JsonPrimitive(it)) }
}
which does not appear to put the value in my map as I would expect. However! If I add a single print statement the map does get correctly updated:
internal fun MutableMap<String, JsonElement>.putAsJsonElement(key: String, value: String?) {
value?.let {
this.put(key, JsonPrimitive(it))
println(this)
}
}
What the heck? Why does the second work when the first doesn’t?Atul Gupta
08/06/2023, 10:06 AM(1.0, 10.0]
or some way to achieve <..
in `1.0 <.. 10.0`operator?Zac Sweers
08/09/2023, 7:24 AMenumEntries()
function to the stdlib, but it appears this didn't actually ship in 1.9.0 and curious if that was intentional or an accident. Happy to file a bug if it's missing
https://github.com/Kotlin/KEEP/issues/283#issuecomment-1670803079groostav
08/17/2023, 3:15 PMkotlinshould delegate to specializedIntArray.sorted()
instead of generic (boxed => slower)copyOf(); j.l.Arrays.sort(int[] array)
sort(a: Array<Int>)
currently
```/**
* Returns a list of all elements sorted according to their natural sort order.
*/
public fun IntArray.sorted(): List<Int> {
return toTypedArray().apply { sort() }.asList()
}
```
should be:
```/**
* Returns a list of all elements sorted according to their natural sort order.
*/
public fun IntArray.sorted(): List<Int> {
return copyOf().apply { sort() }.asList()
}
```• should be faster... I have done no work to verify this, but the sort() code gets to skip a pair of dereferences on comparison. • puts you in the same boat as most other code WRT valhala and
IntArray.asList()
and its List<Integer>
vs List<int>
coolness
• if java ever pushes their SIMD implementation to sort
, then you could see a free perf improvement there too.
I can probably do this myself (+ other specializations) and open a PR, which would be neat, I'd love to do something like this for kotlin 🙂
If you really want performance numbers too i suppose I can boot up JMH to state my (performance) case.
edit: I also realize sort
and sorted
are different, the former does infact delegate to j.l.A.sort()
without a copy, but I still think this is a worthwhile change.
edit 2: unrelated: am i the only one who prefers (::method)
over { method() }
?groostav
08/17/2023, 3:16 PMgroostav
08/18/2023, 6:14 PMstdlib
cross-platform implementation of BitSet
? That is, something that lets me do common bit maniuplations but without a fixed size?
The java.util.BitSet
has all sorts of ugly java (read: old) conventions around cloning, blyeh, i dont like it.Mark
09/07/2023, 7:51 AMbuildString
is used but the StringBuilder
receiver is never accessed. Same for buildList
, buildSet
etc. My motivation for this is that after some refactoring, I made this mistake:
buildString {
items.joinToString(separator = " ") {
foo(it)
}
}
CLOVIS
09/11/2023, 3:26 PM<ERROR CLASS>
(they should be Duration
)윤동환
09/11/2023, 11:58 PMEugen Martynov
09/18/2023, 4:21 PMDerek Peirce
10/23/2023, 7:18 PMsleep(Duration)
method or even Duration.sleep()
in Kotlin (similar to the TimeUnit.sleep
method), instead of having to use Thread.sleep(duration.inWholeMilliseconds)
. The less anyone has to make assumptions about the system's default time unit, the better.윤동환
11/01/2023, 12:51 AMazabost
11/15/2023, 1:35 PM::class
of a nullable reference?
I can do it this way: nullableVal?.let { it::class }
but I would like to know if there is a shorter version. It's a shame I can't just nullableVal?::class
instead, like I can with nullableVal?.javaClass
😅Erik
11/28/2023, 11:11 AMkotlin.time
, is there a stdlib package to work with file sizes? E.g. I'd like to define how many bytes is 100MB, or 100MiB. That's 100 * 1000 * 1000
, or 100 * 1024 * 1024
respectively. Wouldn't it be nice to have extensions and utility functions for? Could maybe be inside <http://kotlin.io|kotlin.io>
.Eugen Martynov
12/05/2023, 10:36 AMDownloadPizza
12/10/2023, 3:44 PMfun findAllWithOverlap(input: CharSequence, startIndex: Int = 0): Sequence<MatchResult> {
if (startIndex < 0 || startIndex > input.length) {
throw IndexOutOfBoundsException("Start index out of bounds: $startIndex, input length: ${input.length}")
}
return generateSequence({ find(input, startIndex) }, { find(input, it.range.first + 1) })
}
Mark
12/20/2023, 1:00 PMerror(message: Any)
accepting type Any
? Doesn’t this open up the potential bug of accidentally passing a Throwable
which we would normally expect to do throw IllegalStateException(cause: Throwable)
?Marcin Wisniowski
01/02/2024, 6:23 PMcombine(flowA, flowB) { a, b -> a to b }.collect { (a, b) -> println("$a, $b") }
I'm looking to remove the "useless" transform step that the combine
function requires.CLOVIS
01/03/2024, 1:24 PMoperator fun <T> List<T>?.plus(element: T) =
if (this != null) this + element
else listOf(element)
I've seen (nullableList ?: emptyList()) + element
being used in a few places, and I don't think it's particularly readable (and it adds some useless logic when concatenating the empty list).Sam Stone
01/16/2024, 2:21 AMJavier
01/25/2024, 1:07 PMGenerated
annotation, it should be interesting to have a standard Generated
annotation that works in KMP. It can be used by multiple libs/frameworks like Dagger, instead of creating its own annotations.
For example, it would be simpler to configure Kover by ignoring that annotation instead of adding all Generated
annotations of each lib/framework.Aljoscha Burgmaier
02/09/2024, 1:15 PMswap
and the and the toSynchonized
from the Java utils. The swap
I created smaller utilities to be able to swap an element within a MutableList
and to retrieve a copy of a List
with the swapped elements.
I had the idea to crate a PR to provide swap
functionalities in Kotlin, what do you think?
I also discovered that you can swap elements within a normal List
with the Collection utils. Is that a Bug?
import java.util.Collections
val list = listOf("A", "B", "C")
println(list) // [A, B, C]
Collections.swap(list, 0, 2)
println(list) // [C, B, A]
Thomas
02/15/2024, 3:35 AMlastIndex
(computed property) for Collection
in `Collections.kt`; lastIndex
is available for List
.
I see no reason why Collections
cannot have lastIndex
, when:
1. size
is a property declared by Collection
, so there is no issue with implementation, which is simply size - 1
.
2. indices
which calls 0..size - 1
under the hood is available for Collections
, so we have no issue with "last index might be something other than size-1".
Could there be something that I didn't consider?Peter
02/16/2024, 8:09 AMerror()
to put exception argument as a cause? For example error(Exception())
.mbonnin
02/21/2024, 11:08 AMinline fun <reified T : Any> Any?.cast(): T = this as T
in the stdlib?
It's been in addToStdLib.kt for a while but looks like it's now deprecated? Is there a fundamental reason this is not possible?Thomas
03/19/2024, 1:50 AMIterator
producer, instead of a simple function that takes a single Iterator
?