allan.conda
06/18/2020, 4:48 AMenum
in Kotlin 1️⃣, or do you just use sealed class
for everything 2️⃣? Is there a disadvantage from using sealed classes?Mr.NiceGuy
06/18/2020, 8:51 AMStefan Beyer
06/18/2020, 9:51 AMinline fun <reified T> ObjectMapper.read(json: String) = readValue<T>(json)
inline fun <reified T> ObjectMapper.readList(json: String) = read<List<T>>(json)
val mapper = ObjectMapper().registerKotlinModule() // fasterxml.jackson
data class Person(val name: String, val role: String)
val json = """[{"name":"Alice","role":"transmitter"},{"name":"Bob","role":"receiver"}]"""
val value1 = mapper.read<List<Person>>(json)
val value2 = mapper.readList<Person>(json)
println(value1) // value1 is a List<Person>
println(value2) // value2 is a List<Map<String, Any>>
When composing a reified type through multiple inline functions, the generic part seems to get gobbled up by the erasure, like it wasn't reified...Philipp Mayer
06/18/2020, 10:24 AMit.receiver
can either be of type ABC or DEF. If its ABC, collect to the first list, if its DEF, collect to the second list
val payments = findOpenPayments.get()
.groupBy { it.receiver == "ABC" }
val somePayments = payments.filter { it.key }.flatMap { it.value }
val otherPayments = payments.filter { !it.key }.flatMap { it.value }
Pacane
06/18/2020, 1:39 PMJakub
06/18/2020, 1:47 PMmapOf(
LIST_ID to listId,
eventId?.let { EVENT_ID to eventId }
)
dave08
06/18/2020, 3:49 PMval fixture = listOf(
mapOf(
"a" to listOf(1,2,3),
"b" to listOf(4)
),
mapOf(
"a" to listOf(5,6,7),
"c" to listOf(8)
)
)
// Should become
val result = mapOf(
"a" to listOf(1,2,3,5,6,7),
"b" to listOf(4),
"c" to listOf(8)
)
turansky
06/18/2020, 8:48 PM// must be invisible in autocomplete
fun first() {}
fun second() {}
Pierre Marais
06/19/2020, 10:40 AM.group
on a collection? i.e.
val list = List(1,2,3,4,5,6,7,8,9,10)
val grouped = list.group(2)
// List(List(1,2), List(3,4), List(5,6), List(7,8) , List(9,10))
Slackbot
06/19/2020, 11:03 AMPaul Stott
06/19/2020, 12:11 PMmy.FixedThreadFactory::class.java
Kelvin Law
06/19/2020, 12:52 PMThrowable
instead of `Exception`Does anyone know why runCatching
for kotlin.Result
is catching Throwable
instead of Exception
?
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/run-catching.htmlVa3000
06/19/2020, 1:55 PMRita Curado
06/19/2020, 1:58 PMbuild.gradle.kts
file but it keeps saying that I have mismatch types:
Type mismatch: inferred type is String but Action<KaptExtension> was expected
plugins {
id( "com.android.library")
id("dev.icerock.mobile.multiplatform-resources")
kotlin("multiplatform")
kotlin("kapt")
}
kotlin {
sourceSets {
val androidMain by getting {
dependencies {
implementation("com.github.bumptech.glide:glide:4.11.0")
kapt("com.github.bumptech.glide:compiler:4.11.0")
}
}
}
}
Does anybody know how to use kapt
?Robert Jaros
06/19/2020, 4:04 PMdependencies {
compile fileTree(dir: 'lib', include: ['*.jar'])
}
Guy Bieber
06/19/2020, 7:59 PMjava.security.cert.CertificateException: com.android.org.conscrypt.OpenSSLX509CertificateFactory$ParsingException: com.android.org.conscrypt.OpenSSLX509CertificateFactory$ParsingException: java.lang.RuntimeException: error:0c0000be:ASN.1 encoding routines:OPENSSL_internal:WRONG_TAG
at com.android.org.conscrypt.OpenSSLX509CertificateFactory.engineGenerateCertificate(OpenSSLX509CertificateFactory.java:284)
at java.security.cert.CertificateFactory.generateCertificate(CertificateFactory.java:366)
at com.nikolamotor.crypto.crypto.PublicKeyToX509Cert(Crypto.kt:316)
at com.nikolamotor.crypto.crypto.storeKeyPair(Crypto.kt:374)
at com.nikolamotor.crypto.crypto.generateKeyPair(Crypto.kt:433)
at com.nikolamotor.crypto.crypto.generateTestKeyPair(Crypto.kt:287)
at com.nikolamotor.crypto.CryptoTest.<init>(CryptoTest.kt:47)
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:217)
at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:266)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at androidx.test.ext.junit.runners.AndroidJUnit4.run(AndroidJUnit4.java:104)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:392)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2196)
Caused by: com.android.org.conscrypt.OpenSSLX509CertificateFactory$ParsingException: com.android.org.conscrypt.OpenSSLX509CertificateFactory$ParsingException: java.lang.RuntimeException: error:0c0000be:ASN.1 encoding routines:OPENSSL_internal:WRONG_TAG
at com.android.org.conscrypt.OpenSSLX509CertificateFactory$Parser.generateItem(OpenSSLX509CertificateFactory.java:122)
at com.android.org.conscrypt.OpenSSLX509CertificateFactory.engineGenerateCertificate(OpenSSLX509CertificateFactory.java:282)
... 34 more
Caused by: com.android.org.conscrypt.OpenSSLX509CertificateFactory$ParsingException: java.lang.RuntimeException: error:0c0000be:ASN.1 encoding routines:OPENSSL_internal:WRONG_TAG
at com.android.org.conscrypt.OpenSSLX509Certificate.fromX509PemInputStream(OpenSSLX509Certificate.java:161)
at com.android.org.conscrypt.OpenSSLX509CertificateFactory$1.fromX509PemInputStream(OpenSSLX509CertificateFactory.java:227)
at com.android.org.conscrypt.OpenSSLX509CertificateFactory$1.fromX509PemInputStream(OpenSSLX509CertificateFactory.java:223)
at com.android.org.conscrypt.OpenSSLX509CertificateFactory$Parser.generateItem(OpenSSLX509CertificateFactory.java:101)
... 35 more
Caused by: java.lang.RuntimeException: error:0c0000be:ASN.1 encoding routines:OPENSSL_internal:WRONG_TAG
at com.android.org.conscrypt.NativeCrypto.PEM_read_bio_X509(Native Method)
at com.android.org.conscrypt.OpenSSLX509Certificate.fromX509PemInputStream(OpenSSLX509Certificate.java:155)
... 38 more
Guy Bieber
06/19/2020, 7:59 PMval certArray : Array<Certificate> = arrayOf(PublicKeyToX509Cert(keyPair.public))
keyStore.setKeyEntry(alias, keyPair.private.encoded, certArray)
private fun PublicKeyToX509Cert(pubKey: PublicKey): Certificate{
var pem = ecPublicKeyToCert(pubKey as ECPublicKey)
Log.v(TAG,"PEM:\n${pem}")
val targetStream: InputStream = pem.byteInputStream()
return CertificateFactory.getInstance("X.509").generateCertificate(targetStream)
}
I get the following error trying to store the key:groostav
06/19/2020, 9:17 PMx1 + x2 - max(thirdVar, fourthVar) * cos(y6)
). Thus I got to write a little compiler and I did it on top of antlr. I recently converted it to generate a little stack assembly language (implemented with a sealed class, eg (sealed class ASM()
, data class PushI(val int: Int): ASM()
, object Multiply: ASM()
, data class InvokeBinary(val name: String)
), expecting that to be fast as hell. its not. Its actually slower than when I just did a tree evaluation. I'm going to stripe the sealed classes such that they use enums, in hopes that makes things run faster. I want to be able to evaluate these expressions at the rate of ~100,000 per sec. Any idea how i might get there? this is https://github.com/EmpowerOperations/babel\Simon Lin
06/20/2020, 8:31 AMflow
?
For example,
val flow = flow {
var i = 0
while(true) {
delay(1000)
println("Emit $i")
emit(i++)
}
}
launch { flow.collect { println("A: got $it") } }
launch { flow.collect { println("B: got $it") } }
I want only B can get data and if B stop collect than A can get data.Nikky
06/20/2020, 10:19 AMToddobryan
06/21/2020, 6:58 PMSlackbot
06/22/2020, 2:44 AMLoganDark
06/22/2020, 7:17 AMAnimesh Sahu
06/22/2020, 11:54 AMLoganDark
06/22/2020, 12:03 PMkenkyee
06/22/2020, 12:22 PMSlackbot
06/22/2020, 1:38 PMdarkmoon_uk
06/23/2020, 3:41 AMAnimesh Sahu
06/23/2020, 10:33 AMXuc Xiem
06/23/2020, 11:21 AMIterable<A>
and want to create a Map<A, Int>
with a function f: A -> Int?
. The values in the map cannot be null. So I write something like this:
iterable
.map { Pair(it, f(it)) } // Iterable<Pair<A, Int?>>
.filter { it.second != null } // still Iterable<Pair<A, Int?>> but we can be sure the Int is not null
.map { Pair(it.first, it.second!!) } // now it is Iterable<Pair<A, Int>>
.toMap()
Is there any better way?Xuc Xiem
06/23/2020, 11:21 AMIterable<A>
and want to create a Map<A, Int>
with a function f: A -> Int?
. The values in the map cannot be null. So I write something like this:
iterable
.map { Pair(it, f(it)) } // Iterable<Pair<A, Int?>>
.filter { it.second != null } // still Iterable<Pair<A, Int?>> but we can be sure the Int is not null
.map { Pair(it.first, it.second!!) } // now it is Iterable<Pair<A, Int>>
.toMap()
Is there any better way?Chantry Cargill
06/23/2020, 11:23 AMiterable.mapNotNull { thing ->
f(thing)?.let { thing to it }
}
James Fagan
06/23/2020, 11:26 AMiterable
.associateWith { f(it) }
.filterValues { it != null }
diesieben07
06/23/2020, 11:26 AMMap<A, Int?>
instead of Map<A, Int>
James Fagan
06/23/2020, 11:29 AMMap<A, Int>
iterable
.associateWith { f(it) }
.filterValues { it != null }
.mapValues { it ?: 0 }
diesieben07
06/23/2020, 11:29 AMJames Fagan
06/23/2020, 11:31 AMXuc Xiem
06/23/2020, 11:54 AMiterable.mapNotNull { thing -> f(thing)?.let { thing to it } }
is excellent. I always wonder what's the equivalent of Scala's .collect
in Kotlin. mapNotNull ?.let
might not be that powerful but good enough.diesieben07
06/23/2020, 11:55 AMmapNotNull
is the closest equivalent