Mark
04/30/2019, 11:53 AMemptyList()
is a fun
and not a val
? I can only guess it’s to do with genericsMarc Knaup
05/08/2019, 12:46 PMdr.dreigh
05/10/2019, 10:51 AMMarc Knaup
05/10/2019, 1:20 PM.isEmpty()
has .isNotEmpty()
.
ClosedRange
doesn't.
I suggest to add it 🙂jw
05/20/2019, 7:07 PMArray.copyOf
returns an array which retains the variance. I.e., Array<out String>
stays as out String
despite me taking a copy to modify its contents. Right now I'm just doing an unsafe cast, but it feels unfortunate (even more than how unfortunate it is to already be using arrays 😢 )Dias
05/21/2019, 4:46 PMjosephivie
05/23/2019, 4:28 PMfun <T> Iterable<T>.forEachBetween(item: (T)->Unit, between: ()->Unit)
which runs the between
block between each call of item
, but doesn't get called at the very beginning or endkarelpeeters
05/25/2019, 9:40 AMkotlin.random.Random
? I don't want to use the Random.Default
since this is in a multithreaded context. The docs (https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.random/-random/index.html) say
To get a seeded instance of random generator use Random function.But that
Random
function just links back to the same page. Am I missing something?raulraja
05/31/2019, 9:04 AMFlow
a user may destructure and compute over the Flow with:
val flow = flowOf(1, 2, 3, 4, 5)
fx {
val n = !flow
n + 1 //n is each of the elements in the flow
}
// flowOf(2, 3, 4, 5, 6)
This same idiom can work with Deferred
, Observable
, IO
, and pretty much anything that contains or can implement a flatMap
method.
For this to work with pure values and across all data types we have to reset the continuation stack so the continuation can be invoked multiple times.
We would like to request that some kind of mechanism for library authors to be able to access the intrinsics of the kotlin.coroutines.jvm.internal.ContinuationImpl
.
At the moment resetting the coroutine stack is an exercise of heavy java reflection when this could be avoided if we had protected access to those fields or a means to obtain/clone the continuation state.
https://github.com/arrow-kt/arrow/blob/7d54b31fe240688b4481b8f91767cbb82b9ad78c/modules/core/arrow-typeclasses/src/main/kotlin/arrow/typeclasses/ContinuationUtils.kt#L7
The use of reflection and manual copying the fields like that imposes unnecessary overhead and does not give us a solid solution going forward.
As far as I understand suspension, suspension has been added to the Kotlin std library so other libraries can build on top of it.
This is great because we don’t have the issues in Kotlin I had in Scala from people abusing Future
just because it was in the std lib and opens the doors for others to create async/concurrent and in general suspended based frameworks just with the suspension apis.
While this in concept is great, at the moment it just seems to be serving KotlinX Coroutines Core library use cases.
There is other libraries like Arrow Fx where single shot / eager async continuations are not enough to cover these use cases.
Can we get access to those fields without reflection or does anyone know of an alternative to accomplish this that does not require dealing with the ContinuationImpl
stack?
We are happy to collaborate with changes in the std lib or whatever it takes to get passed this issue.Mark
06/01/2019, 10:28 AM.also {
targetList.addAll(it)
}
Tom Adam
06/03/2019, 5:11 PM(5 downTo 3 step 3).forEach { println(it) }
or
(5 downTo 3).filter { it % 2 == 0}
.forEach { println(it) }
However, if filter would be infix, one could also write
(5 downTo 3 filter { it % 2 == 0})
.forEach { println(it) }
which would be totally in sync with the infix syntax of step
, and possibly would mean a more consistent API. Any thoughts?ilya.gorbunov
06/06/2019, 1:36 PMMarc Knaup
06/12/2019, 3:12 AMUrl
from ktor.http to stdlib? 🤔
• it's very commonly needed
• it's for more than HTTP or even networking
• Ktor is an implementation detail - could also be another HTTP client
• when using different libraries (networking and other) they all use different Url
classes because there's no common denominatorvoddan
06/13/2019, 12:03 PMlist.take(n)
in a way that would remember the last accessed position and give me the next m
elements on a consecutive list.take(m)
https://stackoverflow.com/questions/56578561/how-can-i-take-varying-chunks-out-of-a-kotlin-sequence
My solution was to produce an Iterator
from the list and use take(n)
on it. Unfortunately that produces a of-by-one error because of the way take
is implemented in stdlib. To fix this one would have to modify a few lines in the [implementation](https://github.com/JetBrains/kotlin/blob/41ce88cb9b8e32da6121e79c229b2563f46fd161/libraries/stdlib/common/src/generated/_Collections.kt#L778 )
replacing
for (item in this) {
if (count++ == n)
break
list.add(item)
}
with
for (item in this) {
list.add(item)
if (++count == n)
break
}
Could such a change be considered?bbaldino
06/13/2019, 7:37 PMVector
which will default instantiate the instances? i want to create a collection of a generic type (from a class generic param) but don't want to have to pass in a lambda to tell the class how to default initialize. i.e.: class Foo<StatType> {
val stats = Vector<StatType>(8)
}
elect
06/14/2019, 8:57 AMonEachIndexed
?elect
06/14/2019, 9:26 AMoperator fun <R> KMutableProperty0<R>.setValue(host: Any?, property: KProperty<*>, value: R) = set(value)
operator fun <R> KMutableProperty0<R>.getValue(host: Any?, property: KProperty<*>): R = get()
for setting local var
from variable referencesHullaballoonatic
06/16/2019, 6:48 PMNumber
changed from Abstract Class to Interface, given it is purely composed of abstract methods.gregorbg
06/25/2019, 8:29 AMMap<K, V>.filterValues
working directly on the values (i.e. it expects (V) -> T
) as opposed to Map<K, V>.mapValues
working on the entire entry (i.e. it expects (Map.Entry<K, V>) -> T
)? Same question applies to the signature of filterKeys
vs. mapKeys
and similar functions.guenther
06/27/2019, 3:24 PMDouble (12.456778)
to a certain precision. This would be something really useful like
12.34567.roundToPrecision(...)
(don't nail me on the function name, I'd just want to express that rounding would be useful. What do you think?Hullaballoonatic
07/01/2019, 8:55 PMclass Foo(bar: Int?, baz: Int?, boz: Int?) {
lateinit var bar: Int = bar // acceptable
lateinit var baz = baz // implicitly assigned as non-nullable
lateinit var boz: Int? = baz // unacceptable
}
bbaldino
07/09/2019, 8:38 PMwithDefault
for a map doesn't actually insert that default into the map when a key which isn't present is found, it just returns an instance of that default?Marc Knaup
07/10/2019, 7:18 PMLocale
yet?
It would be needed for a multiplatform time library to support formatting 🙂arekolek
07/12/2019, 12:14 PMInt.sign
to support this?
val foo = when (a.compareTo(b).sign) {
-1 -> SomeEnum.LEFT
0 -> SomeEnum.NONE
1 -> SomeEnum.RIGHT
}
Does the contract API support something like this?bbaldino
07/13/2019, 6:04 AMMutableMap
, overriding compute
gives the signature override fun compute(key: T, remappingFunction: BiFunction<in T, in U?, out U?>): U?
, but overriding computeIfAbsent
gives override fun computeIfAbsent(key: T, mappingFunction: Function<in T, out U>): U
(note the missing ?
after U
on the return type) but computeIfAbsent
is documented as being able to return null. is this a bug? computeIfPresent
also correctly uses U?
as the return type.Hullaballoonatic
07/19/2019, 6:32 PMoperator fun <T> Pair<T, T>.iterator() = object : Iterator<T> {
var i = 0
override fun hasNext() = i < 2
override fun next() = when (i++) {
0 -> first
1 -> second
else -> throw NoSuchElementException()
}
}
Hullaballoonatic
07/19/2019, 6:57 PM(0 to 0)..(1 to 1)
iterates through 0 to 0, 0 to 1, 1 to 0, 1 to 1
. Might be a stretch, but I think it's intuitive.
operator fun Pair<Int, Int>.rangeTo(that: Pair<Int, Int>) = object : Iterable<Pair<Int, Int>> {
override fun iterator() = object : Iterator<Pair<Int, Int>> {
var i = this@rangeTo.first
var j = this@rangeTo.second
val m = that.first - i
val n = that.second - j
override fun hasNext() = i <= m && j <= n
override fun next(): Pair<Int, Int> {
val res = i to j
if (j == n) {
j = 0
i++
} else {
j++
}
return res
}
}
}
could also be expanded to higher dimensions with List<Int>.rangeTo(other: List<Int>)
, IntArray
, etcHullaballoonatic
07/20/2019, 11:57 PMString::split
to be as great as `joinToString`:
fun String.split(
delimiter: CharSequence = ", ",
prefix: CharSequence = "",
postfix: CharSequence = "",
limit: Int = -1
): List<String>
fun <R> String.split(
delimiter: CharSequence = ", ",
prefix: CharSequence = "",
postfix: CharSequence = "",
limit: Int = -1,
transform: (CharSequence) -> R
): List<R>
serebit
07/23/2019, 11:10 PMjw
07/25/2019, 9:23 PMjw
07/25/2019, 9:23 PMilya.gorbunov
07/25/2019, 9:58 PMjw
07/25/2019, 10:07 PMelapsed()
var expiration = expirationClockMark
val mark = clock.mark()
if (mark.elapsed() >= expiration.elapsed()) {
where it feels like it should be
var expiration = expirationClockMark
val mark = clock.mark()
if (mark >= expiration) {
There's no type system tricks (as far as I know) to restrict this at compile time to two instances which originated from the same Clock
.
But maybe a runtime check is enough? Things like java.nio.path.Path
come to mind where it's tied to a FileSystem
and you cannot meaningfully do operations on two `Path`s from different `FileSystem`s. Despite this, there are methods which work on two instances (like Path.resolve(Path)
) which just runtime-check the invariant.
The only problem I see is that it would become the responsibility of any custom `Clock`/`ClockMark` implementation to ensure they implement this behavior.ilya.gorbunov
07/25/2019, 10:57 PMIf these two marks are from the same clock, this expression would be almost always true or always false, becauseif (mark.elapsed() >= expiration.elapsed())
elapsed
values of both of them increment simultaneously.
What are you trying to achieve by this comparison?jw
07/25/2019, 10:59 PMSystem.nanoTime()
)ilya.gorbunov
07/25/2019, 11:00 PMval expiredClockMark = clock.mark() + timeout
...
if (!expiredClockMark().elapsed.isNegative()) {
// expired
}
jw
07/25/2019, 11:03 PMilya.gorbunov
08/08/2019, 4:55 PMexpirationClockMark
value and how did your code look in the end after rewriting?jw
08/08/2019, 5:01 PMclock.mark() > expirationClockMark
) or do math on them (take this mark plus a duration to produce a new mark, expirationClockMark = currentMark + expirationDuration
). This probably stems from java.time.Clock use which produces Instants which are points on the timeline where this code would have worked.
After re-working, based on your help, the final code is at https://github.com/JakeWharton/SdkSearch/blob/master/backend/dac-proxy/src/main/java/com/jakewharton/sdksearch/proxy/memoize.ktclock: () -> Int = System::nanoTime
and expirationNanos: Int
to Clock
and ClockMark
, so that also led me to naively assume they represented the same concepts