chalup
04/27/2017, 6:04 AMfun foo(key: Bar) = cache.getOrPut(key) { return somethingSomething(key) }
jw
05/12/2017, 1:26 AMisName
specifically which determines whether or not a string is a keyword in the languagevoddan
05/22/2017, 6:22 PMSequence
function like File.readLinesInSequence(): Sequence<String>
. Is it possible to implement it?jw
05/27/2017, 3:38 AM@InlineOnly
?miha-x64
05/28/2017, 3:10 PMinfix fun Int.has(flag: Int) = this and flag == flag
infix fun Int.flagIf(condition: Boolean) = if (condition) this else 0
Haven't I reinvented a bicycle (or a wheel)? Or there's something similar in stdlib?ilya.gorbunov
05/31/2017, 6:55 PMmg6maciej
06/02/2017, 10:09 AMIterable<BigDecimal / BigInteger>.sum()
ever considered?mg6maciej
06/02/2017, 10:11 AMIterable<Long>.sum()
, but not List<*>.sumByLong
(like we have sumBy
for ints and sumByDouble
).mg6maciej
06/02/2017, 11:25 AMDouble.doubleToLongBits
/ Double.longBitsToDouble
😕voddan
06/03/2017, 3:17 PM+
on Iterable
is not it since it returns a listmarcinmoskala
06/19/2017, 1:40 PMmarcinmoskala
06/19/2017, 3:10 PMilya.gorbunov
06/22/2017, 12:39 PMmiha-x64
06/23/2017, 10:04 AMCollections.unmodifiableList(someArray.toList())
?pavlospt
06/26/2017, 9:30 AMselectedChoices.mapIndexed { index, isSelected -> if (isSelected) index else -1 }.filter { it != -1 }
. Is there a prettier way to write this ? 🙂 In short what I want to do is get the indexes from selectedChoices
, which is a BooleanArray, where the value is true
and then map those indexes in something different, hence the .filter { it != -1}
(also not sure if I am on the right channel)gildor
06/30/2017, 2:02 AMlistOf(2, 3, 4).flatMap { item ->
if (item % 2 == 0) {
List(2) { if (it == 0) item else -item }
} else {
listOf(item)
}
}
voddan
07/03/2017, 4:19 PMPair
is not an optimization of Triple
, they have different semantics. In addition, "pairwise" is unique in how obscure a naming it isbrk
07/07/2017, 8:09 AMto<Type>OrNull()
string extensions https://github.com/Kotlin/KEEP/blob/master/proposals/stdlib/string-to-number.md
But don't you think it would a good idea to add generalized version of 'try-or-null' to stdlib? like:
val date = tryOrNull { myAwesomeDateTimeParser.parse("2017-13-32") }
which is equal to:
val date = try { myAwesomeDateTimeParser.parse("2017-13-32") } catch(e: Exception) { null }
pavlospt
08/08/2017, 7:44 AMdmcg
08/10/2017, 2:05 PMjw
08/10/2017, 11:17 PM@Throws
be renamed @JvmThrows
and a @Deprecated typealias
be added to map it back to Throws
?voddan
08/11/2017, 7:05 AMdmcg
08/21/2017, 1:28 PMkarelpeeters
08/28/2017, 9:38 AMval items = ingredentsList.flatMap { listOf(ListItem.HeaderItem(it.type), ListItem.IngredientItem(it)) }
? HeaderItem
will need a correctly implemented `equals`/`hascode` for this.beholder
08/31/2017, 2:55 PMAutoClosable.use()
instead of Closable.use()
when class implements both interfaces? Implementation for AutoClosable is more simple and efficient.ddsoyka
08/31/2017, 5:20 PMassertEquals(byteArray, otherByteArray)
and receive the expected result? Or do I need to call Arrays.equals()
instead?ddsoyka
08/31/2017, 5:38 PMelect
09/03/2017, 2:16 PMkarelpeeters
09/08/2017, 9:20 AMmyMap.mapValuesTo(myMap) { ... }
might work, never tried it before.dimsuz
09/08/2017, 9:20 AMinline fun <K, V> MutableMap<K,V>.replaceOrPut(key: K, updateOp: (V?) -> V): MutableMap<K, V> {
val current = this[key]
this[key] = updateOp(current)
return this
}
dimsuz
09/08/2017, 9:20 AMinline fun <K, V> MutableMap<K,V>.replaceOrPut(key: K, updateOp: (V?) -> V): MutableMap<K, V> {
val current = this[key]
this[key] = updateOp(current)
return this
}
Nir
12/01/2020, 9:26 PMinline fun <K, V> MutableMap<K,V>.replaceOrPut(key: K, updateOp: (V?) -> V): MutableMap<K, V> {
val current = this[key]
this[key] = updateOp(current)
return this
}
I prefer to call this function update personally but the idea is the same. It's very useful. Without this I've found it's rather awkward to do certain things in Kotlin; updating for example integers in MutableMap<*, Int> is annoying, you can't even do m["hello"] += 1
This is quite similar to Java hashmaps compute function: https://www.geeksforgeeks.org/hashmap-compute-method-in-java-with-examples/ which is absent in Kotlin's stdlib. Note that Python and C++ to take two examples, allow compound arithmetic on their mutable Maps, so this is fairly awkward relative to quite a number of existing mainstream languages