Matthias R
01/08/2020, 10:02 AMJoffrey
01/08/2020, 11:37 AMmap[key] ?: defaultValue
or map.getOrDefault(key, defaultValue)
?jimn
01/08/2020, 12:21 PMval stepsize = piv.size() / Runtime.getRuntime().availableProcessors()
(0 until piv.size() ) .step( stepsize).map {
it until minOf(piv.size(),it+stepsize)
}
is there an easier way of getting chunked IntRange sequences ?iex
01/08/2020, 1:36 PMgson
lib question is correct here. Anyway. My problem: I'm parsing diverse JSON objects which come wrapped in a generic JSON structure. I have a function where I've to "unwrap" the nested objects, i.e. extract them from the generic structure and parse the nested object with a generic type T
.Ben Madore
01/08/2020, 6:52 PMclass Person(val email: String?)
inline class Email(val value: String)
fun Person.email() {
return this.email?.let {
Email(email)
}
}
basically an extension method on a class that will null-check a value and return it wrapped in an inline class.
how do i tell the compiler that if the function Person.email()
returns non-null then the instance.email
property is also not null.
is that possible? am i fundamentally misunderstanding contracts?Andrew
01/08/2020, 10:09 PMJiri Bruchanov
01/09/2020, 9:22 AMAdriano Celentano
01/09/2020, 10:26 AMIve Vasiljevic
01/09/2020, 12:22 PMiex
01/09/2020, 12:48 PMuli
01/09/2020, 1:03 PMsimon.vergauwen
01/09/2020, 2:41 PMinternal
anymore from the test
folder of the module. Is this not allowed anymore?Vinicius Araujo
01/09/2020, 2:42 PMval users = getUsers().apply {
val tags = <get list of Pair<userid, tag>>
forEach { user ->
user.tags.addAll(tags.filter { it.first == user.id }.map { it.second })
}
}
Ofir Bar
01/09/2020, 3:22 PMzak.taccardi
01/09/2020, 4:27 PMtypealias
naming - which style do you prefer?
() -> Int
// 1
typealias ProvideNumber = () -> Int
// 2
typealias NumberProvider = () -> Int
Usage then becomes either provideNumber()
or `numberProvider()`/`numberProvider.invoke()`Jakub
01/09/2020, 5:19 PMval items: Set<Any>
that contains different types including EventItem
. I wanna find in my set the index of EventItem
with the specific date like:
val event = state.items
.filterIsInstance(EventItem::class.java)
.find { it.start == date.atStartOfDay() }
val index = state.items.indexOf(event)
However, it gives me Type inference failed. The value of the type parameter T should be mentioned in input types (argument types, receiver type or expected type). Try to specify it explicitly.
What’s missing?gcx11
01/09/2020, 9:19 PMimplementation "org.jetbrains.kotlinx:kotlinx-io-common:$kotlinx_io_version"
inside the commonMain block but gradle keeps saying it couldn't find such library
Could not find org.jetbrains.kotlinx:kotlinx-io-common:0.1.16.
Searched in the following locations:
- <https://jcenter.bintray.com/org/jetbrains/kotlinx/kotlinx-io-common/0.1.16/kotlinx-io-common-0.1.16.pom>
- <https://jcenter.bintray.com/org/jetbrains/kotlinx/kotlinx-io-common/0.1.16/kotlinx-io-common-0.1.16.jar>
- <https://dl.bintray.com/kotlin/ktor/org/jetbrains/kotlinx/kotlinx-io-common/0.1.16/kotlinx-io-common-0.1.16.pom>
- <https://dl.bintray.com/kotlin/ktor/org/jetbrains/kotlinx/kotlinx-io-common/0.1.16/kotlinx-io-common-0.1.16.jar>
- <https://repo.maven.apache.org/maven2/org/jetbrains/kotlinx/kotlinx-io-common/0.1.16/kotlinx-io-common-0.1.16.pom>
- <https://repo.maven.apache.org/maven2/org/jetbrains/kotlinx/kotlinx-io-common/0.1.16/kotlinx-io-common-0.1.16.jarCould> not find org.jetbrains.kotlinx:kotlinx-io-common:0.1.16.
Searched in the following locations:
- <https://jcenter.bintray.com/org/jetbrains/kotlinx/kotlinx-io-common/0.1.16/kotlinx-io-common-0.1.16.pom>
- <https://jcenter.bintray.com/org/jetbrains/kotlinx/kotlinx-io-common/0.1.16/kotlinx-io-common-0.1.16.jar>
- <https://dl.bintray.com/kotlin/ktor/org/jetbrains/kotlinx/kotlinx-io-common/0.1.16/kotlinx-io-common-0.1.16.pom>
- <https://dl.bintray.com/kotlin/ktor/org/jetbrains/kotlinx/kotlinx-io-common/0.1.16/kotlinx-io-common-0.1.16.jar>
- <https://repo.maven.apache.org/maven2/org/jetbrains/kotlinx/kotlinx-io-common/0.1.16/kotlinx-io-common-0.1.16.pom>
- <https://repo.maven.apache.org/maven2/org/jetbrains/kotlinx/kotlinx-io-common/0.1.16/kotlinx-io-common-0.1.16.jar>
According to ktor docs, it should work https://ktor.io/kotlinx/io/setup.htmlursus
01/10/2020, 3:25 AMnitrog42
01/10/2020, 9:55 AMOlekss
01/10/2020, 10:31 AMEarthCitizen
01/10/2020, 10:32 AMrunBlocking
from a coroutine scope or does it just take over the thread while executing?spand
01/10/2020, 12:04 PMKFunction1<T,R>
from a closure ?spand
01/10/2020, 1:06 PMfun LocalDate.set(year: Int?, month: Int?, day: Int?) : LocalDate {
var tmp = this
if (year != null){
tmp = tmp.withYear(year)
}
if (month != null){
tmp = tmp.withMonth(month)
}
if (day != null){
tmp = tmp.withDayOfMonth(day)
}
return tmp
}
iex
01/10/2020, 1:24 PMenum class MyEnum {
@SerializedName("my_foo") FOO // "my_foo" -> FOO (for deserialization)
...
}
MyEnum.FOO.string // FOO -> "my_foo"
Andreas Unterweger
01/10/2020, 2:29 PMvoben
01/10/2020, 2:51 PMwhen
expression to test some cases, why do I have to use the is
keyword sometimes, and other times I don’t?Michał Kalinowski
01/11/2020, 9:38 AMLastExceed
01/11/2020, 1:15 PMif
is a top-level function with the following signature?
fun if(condition: Boolean, action: () -> Unit)
LastExceed
01/11/2020, 2:03 PMfun main() {
val elements = listOf('A', 'B', 'C', 'D', 'E', 'F')
val subsets = elements.getAllSubsets(3)
subsets.forEach {
println(it)
}
}
desired output:
[A, B, C]
[A, B, D]
[A, B, E]
[A, C, D]
[A, C, E]
[A, D, E]
[B, C, D]
[B, C, E]
[B, D, E]
[C, D, E]
here's my implementation:
fun <T> List<T>.getAllSubsets(size: Int): List<List<T>> {
fun <T> getAllSubsetsRecursive(
elements: List<T>,
currentSubset: MutableList<T>,
remaining: Int,
minimumIndex: Int,
results: MutableList<List<T>>
) {
for (i in minimumIndex until elements.size - remaining) {
val currentSubsetCopy = currentSubset.toMutableList()
currentSubsetCopy.add(elements[i])
if (remaining == 0) {
results.add(currentSubsetCopy.toList())
continue
}
getAllSubsetsRecursive(
elements,
currentSubsetCopy,
remaining - 1,
i + 1,
results
)
}
}
val results = mutableListOf<List<T>>()
getAllSubsetsRecursive(
this,
mutableListOf(),
size - 1,
0,
results
)
return results.toList()
}
it does the job, however this still feels a bit verbose and messy to me, especially considering how often I create a copy of a list.
is there any cleanup potential here?Sebouh Aguehian
01/11/2020, 6:32 PMclass MyClass {
val map = mutableMapOf<String, Any?>().withDefault { null }
var name: String by map
}
I cannot set name
to null
, but getting it returns null
. This seems strange, given that the property has type String
and not String?
. Also, the IDE tells me the question mark in name?.toString()
is unnecessary.