jdiaz
06/27/2020, 1:54 PMtoString
method of data classes deterministric? Or the order of fields isn't guaranteed?leo_mendez
06/27/2020, 3:01 PMSlackbot
06/27/2020, 4:48 PMandylamax
06/28/2020, 9:54 AMPhani Mahesh
06/28/2020, 11:51 AMIfvwm
06/28/2020, 2:12 PMinterface IS<T> {fun <T>T.fmap():T}
sealed class IIS<T>: IS<T>
data class IntIS<Int>(val x:Int): IIS<Int>(){
fun Int.fmap(){
return x+1
}
}
Gopal S Akshintala
06/28/2020, 3:50 PMkotlinc -version
shows kotlinc-jvm 1.4-M2
?kenkousen
06/28/2020, 7:53 PMGson().fromJson(json, MyDataClass::class.java)
and I realize I could write
inline fun <reified T> Gson.fromJson(json: String): T = this.fromJson(json, T::class.java)
That way I can reduce the above expression to
Gson().fromJson<MyDataClass>(json)
My question is, is it worth doing that? Do I really gain anything? What do you think?Joakim Tengstrand
06/29/2020, 8:15 AMsourceSets.main {
withConvention(org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet::class) {
// Put components and bases here (src)
kotlin.srcDirs(
"kotlin/bases/cli/src",
"kotlin/components/file/src",
"kotlin/components/srcreader/src"
)
}
}
manlan
06/29/2020, 9:09 AMwilliam
06/29/2020, 2:36 PMsomeComputationUsingA(a)
while still being able to pass it to the super constructor and being able to access it in FooSubType
class FooSubType(
val a: Int,
val b: String
): FizzSuperType(
someComputationUsingA(a) + " $b"
)
In the above its only passed along and I can't access the original result of the computation. I'm a little unsure about how to retain it in FooSubType
to access at a later timeConstantin Muraru
06/29/2020, 2:54 PMfun main() {
val sender = Sender(GlobalScope.coroutineContext)
GlobalScope.launch {
sender.send("Message1") // this is processed
sender.send("Message2") // this is not processed anymore :(
}
Thread.sleep(2000)
}
class Sender(
coroutineContext: CoroutineContext
) {
private val channel = Channel<String>(32768)
private val supervisor = SupervisorJob(coroutineContext[Job])
private val scope = CoroutineScope(coroutineContext + supervisor)
init {
val handler = CoroutineExceptionHandler { _, e ->
println(e.message)
}
with(scope) {
launch(handler) {
MyProcessor(channel).run() // <--- how can we restart/recreate it when exception is thrown?
}
}
}
suspend fun send(message: String) {
channel.send(message)
}
}
class MyProcessor(
private val channel: Channel<String>
) {
suspend fun run() {
for (message in channel) {
process(message)
}
}
private suspend fun process(message: String) {
// something that occasionally throws Exception
println("Processing $message")
throw RuntimeException("Failed to process $message")
}
}
hooliooo
06/29/2020, 2:56 PM/**
* Given a collection of integers and a target integer (target),
* find all unique combinations of integers where the sums of the combinations equal the target.
* @param integers The integers used in the combinations
* @param target The target sum
* @return List<List<Int>> which represents all the possible combinations of integers that sum up to the target
*/
private fun combinationSumII(integers: IntArray, target: Int, limit: Int): List<List<Int>> {
fun backtrack(
integers: IntArray,
target: Int,
limit: Int,
index: Int,
trace: List<Int>,
allCombinations: MutableList<List<Int>>
) {
for (i in index until integers.size) {
val current = integers[i]
if (i > index && current == integers[i - 1]) continue
if (trace.size >= limit) break
val remainingValue = target - current
when {
(remainingValue == 0) -> {
val set = ArrayList<Int>(trace)
set.add(current)
allCombinations.add(set)
}
(remainingValue > 0) -> {
val newTrace = ArrayList<Int>(trace)
newTrace.add(current)
backtrack(integers, remainingValue, limit, i + 1, newTrace, allCombinations)
}
}
}
}
val allCombinations = mutableListOf<List<Int>>()
val result = mutableListOf<Int>()
integers.sort()
backtrack(
integers = integers,
target = target,
limit = limit,
index = 0,
trace = result,
allCombinations = allCombinations
)
return allCombinations
}
For small sets of data this will suffice but there are edge cases integers is a big array, and target is a bigger int.
Is there anyway to make this more efficient? I need this algorithm to be as fast as possible.
Test data with long computation time:
Scenario 1
val integers = 43..166.toList().toIntArray()
val target = 600
val limit = 5
Scenario 2
val integers = (22..166).toList().toIntArray()
val target = 554
val limit = 5
References:
Problem: https://leetcode.com/problems/combination-sum-ii/
Algorithm: https://github.com/topwu/leetkotlin/blob/master/src/main/kotlin/leetcode/CombinationSumII.ktIfvwm
06/30/2020, 1:31 AMuser
06/30/2020, 9:30 AMbod
06/30/2020, 11:33 AMval
which is initialized in init
, but the super's init
calls a method that accesses it (so: before it's initialized). In that method I can do a if (myVar != null) { ...
which works. But now I get a Unnecessary safe call on a non-null receiver
warning 🙂 The compiler tells me this null check is useless since this val is non nullable... But here it can be null
.
I can of course ignore / suppress the warning but I was wondering if there's a deeper truth to this 🙂
Ideas?Davide Giuseppe Farella
06/30/2020, 1:49 PMinfix fun A.function(b): X
,
then I have operator fun X.invoke(f: () -> Unit)
so I’m trying to call a function b { ... }
But I get B cannot get invoked as a function, I’ve tried to use plus
a function b +{ ... }
But I got something similar.
If it use infix infix operator fun X.invoke(f: () -> Unit)
is fine ( a function b invoke { ... }
), but cannot get rid of that explicit invoke
If I do like (a function b) { ... }
is also fine, but still ugly 🙂
Any solution?
Thanks 🙂Wesley Acheson
06/30/2020, 2:56 PMMap<ContactDetailsType, ContactDetails>
val email = source.customer.customerEmail?.let { Customer.ContactDetailsType.EMAIL to Customer.EmailAddress(it)}
val phone = source.customer.telephone?.let { Customer.ContactDetailsType.GENERAL_PHONE to it.toPhoneNumber() }
val mobile = source.cardholderInfo?.mobilePhone?.let { Customer.ContactDetailsType.MOBILE to it.toPhoneNumber()}
val landLine = source.cardholderInfo?.homePhone?.let { Customer.ContactDetailsType.MOBILE to it.toPhoneNumber()}
val contactDetails: Map<Customer.ContactDetailsType, Customer.ContactDetails> = listOf(email, phone, mobile, landLine).filterNotNull().toMap()
The only reason to convert to a list is filteringNotNull() which I get a warning that is useless in a collection anyway.Hadi Tok
06/30/2020, 6:15 PMAhmed Mourad
06/30/2020, 7:28 PMcopy
method.
https://github.com/AhmedMourad0/no-copyandylamax
07/01/2020, 12:05 AMSteve Ramage
07/01/2020, 4:52 AMval moduleCache : java.util.Map<ModuleInfo, WeakReference<ModuleInfo>> = Collections.synchronizedMap(WeakHashMap<ModuleInfo, WeakReference<ModuleInfo>>())
[TYPE_INFERENCE_EXPECTED_TYPE_MISMATCH] Type inference failed. Expected type mismatch:
required:
java.util.Map<ModuleInfo, WeakReference<ModuleInfo>>
found:
(MutableMap<ModuleInfo!, WeakReference<ModuleInfo>!>..kotlin.collections.Map<ModuleInfo!, WeakReference<ModuleInfo>!>?)
louiscad
07/01/2020, 8:15 AMenum ThatEnum(val thatProperty: Float) {
FirstEntry(thatProperty = 10),
AnotherEntry(thatProperty = 20),
LastEntry(thatProperty = Float.POSITIVE_INFINITY);
companion object {
init {
check(values().contentEquals(values().apply { sortBy { it.thatProperty } }))
}
}
}
That requires to companion object
to be accessed for the check to execute, which is fine in my case since I use it to get instances of the enum early.jeggy
07/01/2020, 4:06 PMCaused by: org.jetbrains.org.objectweb.asm.tree.analysis.AnalyzerException: Argument 2: expected R, but found I
along side a bunch of bytecode.
Does anyone know how to read this and how I can figure out what's actually going wrong?Shabinder Singh
07/02/2020, 7:37 AMvar countryList= mutableSetOf<String>()
I want to somehow create empty sets as following -
var us= mutableSetOf<String>()
var in= mutableSetOf<String>()
var br= mutableSetOf<String>()
but i get elements in countryList set dynamically from internet , so how do i create empty sets named as above dynamically?
us, in br are fetched from internet and added to countryList set dynamically.LastExceed
07/02/2020, 9:36 AMMarc Knaup
07/02/2020, 1:06 PMSuspendCloseable
? 🤔
I have to delete a temporary file in close()
but don’t want it to be a blocking call.oscarg798
07/02/2020, 3:06 PMval flow1 = flow<Int> {
emit(1)
}
val flow2 = flow<Int> {
throw IOException()
}
merge(flow1, flow2).onEach {
print(it.toString() + "\n")
}.catch {
emit(3)
}.launchIn(CoroutineScope(<http://Dispatchers.IO|Dispatchers.IO>))
Do I have to catch the flows before merging them or there is something wrong in the example ?xii
07/02/2020, 9:49 PMcrummy
07/03/2020, 3:59 AM