• p

    Pedro González

    2 years ago
    I understand the kotlin "equivalent" of java's try-with-resources is the use() library function However, if exceptions occurs, use will just fail silently. Is there any alternative function/idiom that allows to manage exceptions explicitly without having to manually close the used resource?
    p
    Derek Peirce
    +2
    7 replies
    Copy to Clipboard
  • gildor

    gildor

    2 years ago
    I would just use if/else
    gildor
    Mark
    11 replies
    Copy to Clipboard
  • miha-x64

    miha-x64

    2 years ago
    public infix fun IntProgression.step(step: Int): IntProgression {
        checkStepIsPositive(step > 0, step)
        return IntProgression.fromClosedRange(first, last, if (this.step > 0) step else -step)
    }
    else
    branch is dead here
    miha-x64
    Erik Christensen
    2 replies
    Copy to Clipboard
  • b

    bod

    2 years ago
    Hello, World! Is it still a fact that there is no MultiMap in the stdlib?
    b
    gildor
    +3
    28 replies
    Copy to Clipboard
  • asad.awadia

    asad.awadia

    2 years ago
    is there no equivalent
    asad.awadia
    Shawn
    +1
    19 replies
    Copy to Clipboard
  • maxmello

    maxmello

    2 years ago
    Regarding maxBy, I have the following situation: I want to use maxBy for a collection on a value which is nullable, and if the value is null I don’t actually want it but instead return null. The selector of maxBy requires the return value to be not null, so I have to write
    maxBy { it.someNullableValue ?: Long.MIN_VALUE }
    . This will return me the first value if the list is not empty, so I would have to append
    .takeIf{ it?.someNullableValue != null}
    , which is fine for short cases like this, but when the value to compare is not top level but itself created by a more complex expression like
    it.someList.filter{…}.maxBy{…}?.someNullableValue
    you need to write that same complex expression twice in the
    takeIf
    part. So I wrote
    maxByOrNullIfNull
    (kind of a bad name I know)
    inline fun <T, R : Comparable<R>> Iterable<T>.maxByOrNullIfNull(selector: (T) -> R?): T? { // Allows selector to return R?
        val iterator = iterator()
        if (!iterator.hasNext()) return null
        var maxElem: T? = iterator.next()
        var maxValue: R? = selector(maxElem!!) // Here, we know maxElem is not null
        if(maxValue == null) {
            maxElem = null
        }
        if (!iterator.hasNext()) return maxElem
        do {
            val e: T = iterator.next()
            val v: R? = selector(e)
            if(v != null) {
                if(maxValue == null) {
                    // If we don't have any non-null element yet, set the current element / value
                    maxElem = e
                    maxValue = v
                } else if (maxValue < v) {
                    // Here, we know both values are not null, so we do the usual comparison
                    maxElem = e
                    maxValue = v
                }
            }
        } while (iterator.hasNext())
        return maxElem
    }
    I would argue that this would be a nicer default behavior for
    maxBy
    , as it doesn’t really change the behavior for non-null cases, but makes clear that null values will always be considered “less” than any actual value in the comparison (no more need for
    ?: Long.MIN_VALUE
    or something) and that we want an element with an actual value, not one that has no value for the comparison.
    maxmello
    Derek Peirce
    +1
    3 replies
    Copy to Clipboard
  • Derek Peirce

    Derek Peirce

    2 years ago
    IntRange.forEach
    uses
    Iterable.forEach
    , so
    (1..n).forEach { ... }
    creates an
    Iterator
    , while
    for (k in (1..n)) { ... }
    is optimized to use a while-loop, not even creating the original
    IntRange
    object. Could
    IntRange
    receive its own
    forEach
    extension method to preserve this optimization, and perhaps receive additional optimizations for common methods like
    map
    ?
    Derek Peirce
    r
    +1
    3 replies
    Copy to Clipboard
  • gsala

    gsala

    2 years ago
    Is there a better way to write these bit masks?
    val impulseDownAllowed: Boolean = capabilitiesByte and 0x10.toByte() != 0.toByte()
    val bleMovementAllowed: Boolean = capabilitiesByte and 0x20.toByte() != 0.toByte()
    gsala
    e
    18 replies
    Copy to Clipboard
  • Bornyls Deen

    Bornyls Deen

    2 years ago
    I've found myself implementing this in a couple of different projects now... do you guys think there might be a place for it in the standard library? The closest I've got using the existing functions is chaining
    filterIsInstance
    and
    first
    but I imagine this is a little better performance wise as we are returning after the first match. It's like
    first
    is to
    filter
    ... firstIsInstance/filterIsInstance 😄
    inline fun <reified T> Iterable<*>.firstIsInstance(): T? {
        for (element in this) {
            if (element is T) {
                return element
            }
        }
    
        return null
    }
    Bornyls Deen
    Dominaezzz
    +1
    4 replies
    Copy to Clipboard
  • asad.awadia

    asad.awadia

    2 years ago
    what is the most idiomatic way of going through an array of json and setting a field on the object with the max on a particular field - and removing that field from everyone else
    asad.awadia
    Dominaezzz
    3 replies
    Copy to Clipboard