menegatti
04/21/2017, 12:04 PMfilterIsInstance()
is just acting as a cast heremenegatti
04/21/2017, 12:05 PMpair.filter { it.second != null }.toMap() as Map<String, Double>
enleur
04/21/2017, 12:05 PMenleur
04/21/2017, 12:05 PMbeholder
04/21/2017, 12:17 PMenleur
04/21/2017, 12:23 PMdmitry.petrov
04/21/2017, 12:23 PMpairs.mapNotNull { it.takeIf { it.second != null }?.let { it as Pair<String, Double> } }.toMap()
Unsafe (relying on nullability check under takeIf
), but doesn't create an extra list for filterIsInstance.dmitry.petrov
04/21/2017, 12:24 PMassociateByNotNull
could be helpful here if it was in stdlibbeholder
04/21/2017, 12:40 PMval map = mutableMapOf<String, Double>().apply {
for ((first, second) in list) if (second != null) this[first] = second
}
louiscad
04/22/2017, 10:34 AMkevinmost
04/22/2017, 3:26 PMvar foo by mutableLazy { 1 }
init {
foo = 2
print(foo)
}
What would the value of foo
be when it's printed now? 1
or 2
? If it's 1, that's confusing. If it's 2, did the block ever get invoked (because it could potentially have side-effects)? Why or why not?mg6maciej
04/22/2017, 3:52 PMI don't find mutableLazy useful :)▾
louiscad
04/22/2017, 7:50 PMsetValue
on `foo`'s delegate, which drops the non executed intializer and sets to the passed value, which is 2 and this is what will be printed.miha-x64
04/24/2017, 11:14 AM(Something)->R?
instead of (Something)->R
?
private inline fun <reified K : Enum<K>, V, R> EnumMap<out K, V>.mapValuesIfNotNull(transform: (Map.Entry<K, V>) -> R?): Map<K, R> {
return mapValuesTo(EnumMap<K, R>(K::class.java), transform) // nullable return type ^
}
ilya.gorbunov
04/24/2017, 11:30 AMEnumMap<K, R>
is MutableMap<K!, R!>
and therefore it's assignable both to MutableMap<in K, in R>
and MutableMap<in K, in R?>
(//cc @denis.zharkov)miha-x64
04/24/2017, 11:37 AMHashMap<K, R>()
causes expected error, but, on the other hand,
private typealias EnumMap<K, V> = java.util.EnumMap<K, V>
does not help.ilya.gorbunov
04/24/2017, 11:40 AMmiha-x64
04/24/2017, 11:40 AMmg6maciej
04/24/2017, 11:51 AMpublic operator fun <T> Iterable<T>.minus(element: T): List<T>
be defined with element as nullable?
public operator fun <T> Iterable<T>.minus(element: T?): List<T>
I.e. if you call listOf<String>(...) - null as String?
it would just return the same List<String>
and not List<String?>
as it currently does.
1 reply
Anyone has any input on this idea? I think such a change could be implemented without breaking any code using minus
currently. It could even be changed to element: Any?
, but I'm not sure this would be very useful.mg6maciej
04/24/2017, 12:15 PMpublic operator fun <T> Iterable<T>.minus(element: T): List<T>
be defined with element as nullable?
public operator fun <T> Iterable<T>.minus(element: T?): List<T>
I.e. if you call listOf<String>(...) - null as String?
it would just return the same List<String>
and not List<String?>
as it currently does.
8 replies
val list1: List<Int?> = arrayListOf(1, null).minusNullable(null)
println("list1: $list1")
val list2: List<Int> = arrayListOf(1, 2).minusNullable(null)
println("list2: $list2")
compiles and prints:
list1: [1]
list2: [1, 2]
where minusNullable
is a slightly changed minus
operator (essentially argument type changed to T?
):
fun <T> Iterable<T>.minusNullable(element: T?): List<T> {
val result = ArrayList<T>()
var removed = false
return this.filterTo(result) { if (!removed && it == element) { removed = true; false } else true }
}
ilya.gorbunov
04/24/2017, 12:54 PMpublic operator fun <T> Iterable<T>.minus(element: T): List<T>
be defined with element as nullable?
public operator fun <T> Iterable<T>.minus(element: T?): List<T>
I.e. if you call listOf<String>(...) - null as String?
it would just return the same List<String>
and not List<String?>
as it currently does.
13 replies
This has to investigated, whether it's possible to add something like this: https://github.com/JetBrains/kotlin/blob/1.1.0/libraries/stdlib/src/kotlin/collections/MutableCollections.kt#L15
so that type of element to be removed could be any direct supertype of element type of the collection.mg6maciej
04/24/2017, 2:37 PMpublic operator fun <T> Iterable<T>.minus(element: T): List<T>
be defined with element as nullable?
public operator fun <T> Iterable<T>.minus(element: T?): List<T>
I.e. if you call listOf<String>(...) - null as String?
it would just return the same List<String>
and not List<String?>
as it currently does.
23 replies
https://youtrack.jetbrains.com/issue/KT-17569ilya.gorbunov
04/24/2017, 3:34 PMMutableLazy
implementation is here: https://gist.github.com/LouisCAD/9b4ef46c12f1f6fe9b45658a8eff194c
3 replies
Regarding the issue itself. I'm not against including it in stdlib, though the use cases have to be discussed. If you have some, do not hesitate to share in that issue's comments.mg6maciej
04/25/2017, 12:02 PMfold
on Map
in Kotlin".miha-x64
04/25/2017, 12:05 PMmap.entries.reduce
?mg6maciej
04/25/2017, 12:06 PMfold
on entries
. Like that I can deconstruct it { acc, (key, value) ->
mesquka
04/26/2017, 2:26 PMjohn.shelley
04/26/2017, 6:15 PMchalup
04/27/2017, 6:12 AMfun foo(key: Bar) = cache.getOrPut(key) { return somethingSomething(key) }
2 replies
Yes, removing the return
solves the problem.chalup
04/27/2017, 6:13 AMgetOrPut
early with return
statement?