elect
11/11/2020, 12:39 PMsumOf{..}: Float
, there are Int, Long, UInt, ULong, Double, BigDecimal, BigInteger but Float is missingholgerbrandl
11/29/2020, 6:40 PMNumber.toDouble()
"involve rounding" as suggested by its API docs?poohbar
11/29/2020, 9:13 PMnkiesel
12/04/2020, 8:11 PMbuildString
which takes a separator as an optional parameter? Right now I use e.g. buildList { ... add(..) }.joinToString(" ")
but buildString(" ") { ... add(...) }
would be nicer. And perhaps also supports the "prefix" and "suffix" from Java's StringJoiner
?Abduqodiri Qurbonzoda [JB]
12/08/2020, 9:46 AMtapchicoma
12/08/2020, 8:14 PMjava.nio.file.Path
extensions in stdlib will it make sense to add new PathTreeWalk
class similar to FileTreeWalk
? I know nio has walkFileTree()
method, but it does not produces Sequence
.Derek Peirce
12/10/2020, 4:37 AMpublic inline fun <T1, T2: Comparable<T2>> compareBy(crossinline selector: (T1) -> T2): Comparator<T1> =
Comparator { a, b -> selector(a).compareTo(selector(b)) }
This looks similar to the existing `compareBy`:
public inline fun <T> compareBy(crossinline selector: (T) -> Comparable<*>?): Comparator<T> =
Comparator { a, b -> compareValuesBy(a, b, selector) }
But the type ambiguity and nullability make it inefficient, as it needs to box primitives. I ran a benchmark comparing the two compareBy
implementations for finding the maximum of100,000 objects by their random int fields, and got these results:
Benchmark (N) Mode Cnt Score Error Units
ComparatorBenchmarkPrimitiveHelper.maxKt 100000 avgt 10 2.824 ± 0.102 ms/op
ComparatorBenchmarkPrimitiveHelper.maxPrimitive 100000 avgt 10 0.381 ± 0.155 ms/op
The results speak for themselves, the mass boxing and unboxing in the existing implementation has major performance implications and ought to be prevented with a more specialized compareBy
method.
This naturally extends to the methods that use it such as sortBy
and sortedBy
(I ran the benchmark using maxBy
because it gave the most contrast, sortBy
spent more time on intermediate operations). Strangely, maxBy
and similar already require non-null values and work as they are.Marc Knaup
12/17/2020, 2:49 PMClosedRange<T>.mapBounds()
val range = 1 .. 10
val shiftedRange = range.mapBounds { it + 10 } // = 11 .. 20
val characters = "a" .. "z"
val uppercaseCharacters = characters.mapBounds { it.toUpperCase() } // == "A" .. "Z"
fun <T : Comparable<T>, R : Comparable<R>> ClosedRange<T>.mapBounds(transform: (T) -> R): ClosedRange<R> =
transform(start) .. transform(endInclusive)
// plus overloads for primitive ranges
Slackbot
12/18/2020, 10:26 PMBen
12/22/2020, 12:03 AMmbonnin
01/01/2021, 6:22 PMMutableList.removeLast
? The IDE finds it but I get an error at compile time: Unresolved reference: removeLast
. I'm using Kotlin 1.4.10 (And Happy New Year !! 🎉 🌟 )holgerbrandl
01/09/2021, 8:38 PMrepeat
that allows to assign the result: val created = repeat(n){ create_something()}
? Doing (1..n).map{ create_something() }
does not feel elegant.
I can certainly create it myself with fun <T> repeat(n:Int, builder: (Int) -> T) = (1..n).map{ builder(it)}
but I wonder why it's not part of stdlib? It also does not really work well, because somehow it's not resolved when being placed in some other source file that is different from that which contains the call site.holgerbrandl
01/10/2021, 1:46 PMmbonnin
01/13/2021, 4:27 PMSet<T>.map {}
returns a List
and not a Set
?LeoColman
01/22/2021, 2:43 AMmap.merge
function? (or if there is, why can't I find it? xD) in plain Java it exists . I'm in a Kotlin JVM projectholgerbrandl
01/22/2021, 8:29 PMmap.getOrDefault(key){ ... build default value lazily here .. }
similar to how the message is evaluated in require
?holgerbrandl
01/22/2021, 9:04 PMkotlin.random.Random.Default
so that running listOf(1,2,3).shuffled()
twice would give the same result. Controlling randomess is an important feature of every language that is intended for data-science (see https://www.w3schools.com/python/ref_random_seed.asp for python or https://www.rdocumentation.org/packages/simEd/versions/1.0.3/topics/set.seed for Rsimon.vergauwen
02/05/2021, 10:05 AMpublic fun <T> maxOf(a: T, vararg other: T, comparator: Comparator<in T>): T
is conflicting with public actual fun <T : Comparable<T>> maxOf(a: T, vararg other: T): T
for me 🤔 Even when T
isn't : Comparable<T>
. This is only fixed for me when moving the comparator
parameter before the vararg
one. Is this a known issue?
I'm trying to define sort
for Comparable
and Comparator
in Arrow to deprecate the Order
typeclass into something more Kotlin idiomatic, but following the same patterns as the Kotlin Std I ran into this issue when writing tests.simon.vergauwen
02/05/2021, 12:33 PM(Double.NaN == Double.NaN).let(::println) // false
(listOf(Double.NaN) == listOf(Double.NaN)).let(::println) // true
elect
02/08/2021, 11:51 AMZac Sweers
02/08/2021, 6:29 PMpoohbar
02/10/2021, 3:30 AMinline fun succeeded(block: () -> Unit): Boolean {
return try {
block()
true
} catch (e: Exception) {
false
}
}
Dmitry Kandalov
02/10/2021, 11:27 PMList
but lowercase `sequence`:
List(size = 1) { "foo" }
sequence { yield("foo") }
holgerbrandl
02/18/2021, 7:19 PMNumber
that trims it at 0, that is if(it<0) 0 else it
?pniederw
03/08/2021, 5:39 AMilya.gorbunov
03/10/2021, 12:33 PMbuildList
, buildSet
and buildMap
, introduced as experimental in 1.3.70? Do you use them in your code? What are your impression of them?nkiesel
03/15/2021, 6:27 PMString.replaceLast
which does the same as String.replaceFirst
but for the last occurrence? We have "first/last" matching pairs for other methods.sergio mongelat
03/18/2021, 12:12 AMfun Int.coerceIn(minMax: Int): Int {
val abs = minMax.absoluteValue
return coerceIn(-abs, abs)
}
scaventz
03/19/2021, 2:04 PMlouiscad
03/26/2021, 1:49 PMval TODO: Nothing inline get() = TODO()
The lowercase version could also be nice.louiscad
03/26/2021, 1:49 PMval TODO: Nothing inline get() = TODO()
The lowercase version could also be nice.Zach Klippenstein (he/him) [MOD]
03/26/2021, 2:01 PMTOOD
because I always typo it that way 😅tddmonkey
03/26/2021, 3:03 PMlouiscad
03/26/2021, 3:04 PMasync
and don't await
each other.Shawn
03/26/2021, 4:00 PMlouiscad
03/26/2021, 4:04 PMtab
and enter
with one hand, so it's not so much a hardware problem 😉Shawn
03/26/2021, 5:11 PMlouiscad
03/26/2021, 5:13 PMTODO()
properly so the code is not all red is counter-productive.Fleshgrinder
03/27/2021, 8:31 AMtddmonkey
03/27/2021, 12:29 PMMarc Knaup
03/29/2021, 12:55 PMFIXME
.
The IDE supports both.
And for me FIXME
is urgent while TODO
is not.louiscad
03/29/2021, 12:58 PMMarc Knaup
03/29/2021, 12:58 PMlouiscad
03/29/2021, 12:59 PMTODO()
calls beyond WIP branches, plus you can use find usages.Marc Knaup
03/29/2021, 12:59 PMlouiscad
03/29/2021, 1:00 PM//TODO
in it, it will find all the ones in EOL (end of line) comment.Marc Knaup
03/29/2021, 1:02 PMTODO
, is it?
Also, you can only use TODO()
if actual code is // FIXME There's an occasional race condition.
louiscad
03/29/2021, 1:03 PMTODO()
in code will crash when reached, plus all the code after is marked as unreachable, I think that's good enough.
If people cannot understand the difference, I'd question giving them the power to do whatever on your codebase.Marc Knaup
03/29/2021, 1:04 PMlouiscad
03/29/2021, 1:04 PMMarc Knaup
03/29/2021, 1:04 PMlouiscad
03/29/2021, 1:04 PMMarc Knaup
03/29/2021, 1:05 PMYeah, then just use the commentsBut then I do need a FIXME instead of TODO?
louiscad
03/29/2021, 1:05 PMMarc Knaup
03/29/2021, 1:05 PMlouiscad
03/29/2021, 1:06 PMMarc Knaup
03/29/2021, 1:06 PMlouiscad
03/29/2021, 1:08 PM//TODO (DONT DEPLOY)
, then great 😉Marc Knaup
03/29/2021, 1:10 PMTODO
quite weak for something that’s completely missing.louiscad
03/29/2021, 1:15 PMMarc Knaup
03/29/2021, 1:16 PMlouiscad
03/29/2021, 1:16 PM@RequiresOptIn
YourSuperToDo(…)
, FixMeBeforeProdDeploy()
, etc.Marc Knaup
03/29/2021, 1:17 PMfun TODO()
in Kotlin it would just be a regular function (or val
) with a special annotation?louiscad
03/29/2021, 1:18 PM@DslMarker
. Example name: @TodoMarker
Marc Knaup
03/29/2021, 1:19 PMlouiscad
03/29/2021, 1:19 PMMarc Knaup
03/29/2021, 1:20 PMlouiscad
03/29/2021, 1:21 PMMarc Knaup
03/29/2021, 1:21 PMlouiscad
03/29/2021, 1:23 PMFleshgrinder
03/30/2021, 6:26 AMgildor
04/09/2021, 5:35 AMTODO()
, it always top suggestion if you use capital letterslouiscad
04/09/2021, 8:10 AMgildor
04/09/2021, 8:14 AMlouiscad
04/09/2021, 8:19 AMTO
+ hit the tab key quickly. Result: TO
gildor
04/09/2021, 8:28 AM