• e

    elect

    11 months ago
    what about changing on
    Map<K, V>
    the
    getOrElse
    by passing as argument the key which wasn't found?
    public inline fun <K, V> Map<K, V>.getOrElse(key: K, defaultValue: () -> V): V
    to
    public inline fun <K, V> Map<K, V>.getOrElse(key: K, defaultValue: (V) -> V): V
    My use case is to search for the same key somewhere else
    e
    e
    +2
    11 replies
    Copy to Clipboard
  • s

    smichel17

    11 months ago
    What would you think about an indexed
    Sequence.takeWhile
    ? E.g. one of these:
    fun <T>Sequence<T>.takeThenWhile(count: Int, predicate: (T) -> Boolean): Sequence<T> {
        var i = 0
        return this.takeWhile{ i < count || predicate(it) }.onEach { i++ }
    }
    
    
    fun <T>Sequence<T>.takeWhileIndexed(predicate: (Int, T) -> Boolean): Sequence<T> {
        var i = 0
        return takeWhile { predicate(i, it) }.onEach { i++ }
    }
    Both
    take
    and
    takeWhile
    could be implemented in terms of this function
    fun <T>Sequence<T>.take(count: Int) = takeThenWhile(count) { false }
    fun <T>Sequence<T>.takeWhile(predicate: (T) -> Boolean) = takeThenWhile(0, predicate)
    
    fun <T>Sequence<T>.take(count: Int) = takeWhileIndexed { i, _ -> i < count }
    fun <T>Sequence<T>.takeWhile(predicate: (T) -> Boolean) = takeWhileIndexed { _, t -> predicate(t) }
    s
    g
    +1
    18 replies
    Copy to Clipboard
  • r

    rocketraman

    11 months ago
    partition
    only supports two outputs. What about adding something like this to the stdlib?
    /**
     * Similar to the stdlib partition method, but supports an arbitrary number of output lists. The number
     * of outputs must be known in advance.
     */
    @Throws(IndexOutOfBoundsException::class)
    fun <T> List<T>.partitionInto(size: Int, predicate: (T) -> Int): List<List<T>> {
      val lists = listOf(*(0.until(size).map { mutableListOf<T>() }.toTypedArray()))
      for (element in this) {
        val index = predicate(element)
        lists[index].add(element)
      }
      return lists
    }
    r
    mbonnin
    +2
    34 replies
    Copy to Clipboard
  • groostav

    groostav

    11 months ago
    uhh, has there been any discussion about using something like
    inline class
    to simplify java'scompareTo function? I was thinking that an
    inline class
    that's wrapping the results would be handy and do a lot for readability. If not, can we
    typealias ComparisonResult = Int
    and put some docs there? I feel like kotlin can do a lot to add sugar and/or lipstick to this pig in java.
    groostav
    e
    6 replies
    Copy to Clipboard
  • natpryce

    natpryce

    11 months ago
    Is there a function in the kotlin stdlib to split a list into a list of sublists with a predicate to choose where to separate each sublist, and where the result preserves the order of elements.
    List::chunked
    doesn’t do what I want, nor does
    List::windowed
    E.g. if I have a list, x = [“a”, “b”, “C”, “d”, “E”, “F”, “g”] and I want to split it into sublists where the case is the same within the sublist, I could pass in a predicate to this function something like…
    x.split { x, b -> a.isUpperCase() != b.isUpperCase() }
    giving me:
    [["a", "b"], ["C"], ["d"], ["E", "F"], ["g"]]
    I often need this operation, but always end up writing it with imperative code.  Is there something in the stdlib I’m missing.
    natpryce
    mcpiroman
    +3
    24 replies
    Copy to Clipboard
  • mazorius

    mazorius

    11 months ago
    Hi, I see that
    .capitalize()
    is be replaced by
    .replaceFirstChar()
    but the latter one is not shown in my IDE? I only got
    replaceFirst
    mazorius
    j
    +1
    5 replies
    Copy to Clipboard
  • r

    Ruckus

    11 months ago
    Currently we have
    enumValues<T>(): Array<T>
    and
    enumValueOf<T>(name: String): T
    for enums. It would be nice if there was also
    enumValueAt<T>(ordinal: Int): T
    . It's pretty easy to work around with
    enumValues()[ordinal]
    , so not a huge deal, but it feels a bit clunky. Another nice to have for would be something like
    enumSize<T>(): Int
    . Again, easy to work around with
    enumValues().size
    , but same argument as above.
    r
    e
    +1
    4 replies
    Copy to Clipboard
  • h

    holgerbrandl

    10 months ago
    Just of curiosity: Why is
    ceil()
    not an extension on Double?
    h
    c
    +2
    14 replies
    Copy to Clipboard
  • maxmello

    maxmello

    10 months ago
    joinToString
    has
    truncated: CharSequence = "..."
    as an optional parameter. It would be nice if
    String.take(n: Int)
    could have the same optional parameter that is appended in case
    n
    is smaller than the length of the String. Maybe it could also be a different method name, as this would be more a use case for logging/displaying a string similar to joinToString, rather than “working on a string” by using substring for maybe some logical operation on the string. It could be something like
    abbreviate
    or maybe
    cut
    maxmello
    arekolek
    2 replies
    Copy to Clipboard
  • Ansh Tyagi

    Ansh Tyagi

    10 months ago
    Hey everyone! so i was creating a file during runtime and then accessing it (File.readLines()) but it throws an error of file not found? Is it because we cannot read the file created during runtime? If so is there any work around happy to discuss
    Ansh Tyagi
    Big Chungus
    14 replies
    Copy to Clipboard