• Hullaballoonatic

    Hullaballoonatic

    2 years ago
    fun <K, V> Array<K>.associateWith(valueSelector: (K) -> V): Map<K, V>
    why is this not in stdlib already? i'm very confused. all other versions of associate from
    Iterable
    exist on
    Array
    except this one. my use case:
    myEnumClass.values().associateWith { ... }
    Hullaballoonatic
    ilya.gorbunov
    +1
    10 replies
    Copy to Clipboard
  • Hullaballoonatic

    Hullaballoonatic

    2 years ago
    Just as
    fold
    and
    reduce
    have
    foldRight
    ,
    reduceRight
    , I would like
    windowedRight
    and
    chunkedRight
    . My current use case is also maybe the best example:
    val Int.digitBlocks get() = toString().chunkedRight(3).map(String::toInt) // 1_234_567 -> [[567], [234], [1]]
    I noticed
    CharSequence
    also has a version of
    windowed
    that accepts a transform operation, and I would like that for
    Iterable
    as well. In general why don't most Iterable operations also have a version with a transform operation tacked on? I don't think that hurts readability, and only partially conflicts in places where the operation already receives a lambda, like
    partition
    Hullaballoonatic
    Zach Klippenstein (he/him) [MOD]
    4 replies
    Copy to Clipboard
  • l

    Leon K

    2 years ago
    Proposal: I'd love to have versions of
    require
    that allow for specifying a custom Exception type to throw, instead of just throwing
    IllegalArgumentException
    l
    karelpeeters
    +1
    13 replies
    Copy to Clipboard
  • asad.awadia

    asad.awadia

    2 years ago
    Is there anything i can use that is like apply but doesn’t mutate the original object and instead returns a copy?
    asad.awadia
    r
    +1
    3 replies
    Copy to Clipboard
  • Xavier F. Gouchet

    Xavier F. Gouchet

    2 years ago
    I have the following method that I used a bunch of times already, which could find it's place in the stdlib maybe :
    operator fun Char.times(i: Int): String {
        check(i >= 0) { "Can't repeat character negative times " }
        return String(CharArray(i) { this })
    }
    
    val s = '_' * 8
    Xavier F. Gouchet
    d
    2 replies
    Copy to Clipboard
  • j

    jimn

    2 years ago
    is this still currently accurate info?
    j
    Burkhard
    6 replies
    Copy to Clipboard
  • LeoColman

    LeoColman

    2 years ago
    Why is there a
    String?.isNullOrBlank()
    but there is no
    String?.isNotNullOrBlank()
    while there is a
    String.isBlank()
    and
    String.isNotBlank()
    LeoColman
    a
    +3
    9 replies
    Copy to Clipboard
  • Simon Schubert

    Simon Schubert

    2 years ago
    I try to get the image url of the first object that exist in the database based on a list of object ids. My current solution looks like the following:
    var ids = listOf("123", "456", ...)
    var imageUrl = getFirstImageUrl(ids)
    
    fun getFirstImageUrl(list: List<String>): String? { 
        list.forEach {
           var item = database.getItemById(it)
           if(item != null) {
              return item.imageUrl
           }
        }
        return null
    }
    Is there a better way to do it in kotlin? I could image a solution something like this:
    var imageUrl = ids.firstNotNullFlat {
      var item = database.getItemById(it)
      item?.imageUrl
    }
    Simon Schubert
    s
    +1
    7 replies
    Copy to Clipboard
  • l

    Leon K

    2 years ago
    small stdlib proposals to improve smart-casting concerning nullability: we need a way to do
    if(foo != null)
    for multiple values and collection contents. Proposal part one add a
    ifNotNull
    function that takes vararg arguments of type
    Any?
    . given
    val a: Int? = 1
    val b: String? = null
    val c: Boolean? = false
    this would allow to replace this:
    if (a != null && b != null && c != null) {
        println("$a, $b, $c")
    }
    with this:
    ifNotNull(a, b, c) {
        println("$a, $b, $c")
    }
    which imo is a lot more readable, and also easier to type. (small note/question: would these checks - if the function was just implemented in terms of contracts and the if-statement - short-circuit, like they do with the if-statement?) proposal part two higher-order-functions on collections should do smart-casting. (I guess this should be doable via contracts?) given
    fun useList(nums: List<Int>) = println(nums)
    val foo: List<Int?> = listOf(12, 13, null, 14)
    this would allow to do something like this:
    if (foo.all { it != null } ) {
    	useList(foo)
    }
    currently, afaik, this check is not possible with compiler-guarantees, as there is no
    Collection<T>.hasNoNulls()
    check. there are some simmilar functions, but nothing that actually does this. there is-
    Iterable<T?>.requireNoNulls()
    - the most simmilar, but this throws exceptions and isn't as general as making
    all
    and
    any

    -calls smart-cast

    Iterable<T?>.filterNotNull()
    - could be currently used in combination with the all-check to "simulate" a smart-casting all, but as it just filters out the null-values, this could to easily be broken if someone isn't careful when modifiying the preceding if-statement.
    l
    karelpeeters
    +1
    4 replies
    Copy to Clipboard
  • j

    jimn

    2 years ago
    is
    "string".size
    unreasonable as a stdlib feature?
    j
    Thomas
    +5
    26 replies
    Copy to Clipboard