Ayfri
02/09/2022, 10:13 PMlist.sortedBy { it }
, for a list of strings or numbers ? I know it's already pretty short but the fact that there's only it
in the lambda feels kinda wrongbrabo-hi
02/10/2022, 4:44 AMKenneth
02/10/2022, 11:21 AMRescribet
02/10/2022, 1:54 PMjames
02/11/2022, 7:56 AMdata class Reply(id: Int, parentId: Int, replies: List<Reply>)
... and I'm attempting to:
{
"replies": [
{ id: 1, parent: 0 } <- turn this 1
{ id: 2, parent: 1 } into this -> └── 2
{ id: 3, parent: 2 } ├── 3
{ id: 4, parent: 3 } │ └── 4
{ id: 5, parent: 2 } ├── 5
{ id: 6, parent: 2 } ├── 6
{ id: 7, parent: 2 } └── 7
]
}
does anyone have any experience in "unflattening" a list into a tree like this? I start out by using associateBy
on my flat list and turn it into a mutable map, at which point I iterate the flat list itself and append the current item to any value in the map whose id
matches the current iterator item's parentId
. doing this I end up with a list where the first and second levels are correct, but third and deeper are empty
I've spent about 10 hours on this over the past 3 days and I've really gotten nowhere, just going in circles really. would love some guidance from anyone who has done this kind of thing with KotlinGustav Elmgren
02/11/2022, 11:15 AMclass Test {
val firstValue: String
val secondValue: Long
}
class TestWrapper {
val firstValue: Wrapper<String>
val secondValue: Wrapper<Long>
}
Would it be possible somehow to create a common interface/class for these two classes?
For example, if it would be possible, using union types:
interface InterfaceTest<T : Wrapper<T>, T>
But given that is not possible, is it some other way Kotlin can handle this?Luis
02/11/2022, 3:09 PMfun find(session: Session, query: Query) {}
fun find(query: Query) {}
Is there a cleaner way than this to pick which one to use?
fun caller(session: Session?) {
val query = Query()
if (session == null) {
return find(query)
}
return find(session, query)
}
Thanks!xii
02/11/2022, 4:17 PMJohannes Fahrenkrug
02/11/2022, 6:09 PM./gradlew --version
I get
------------------------------------------------------------
Gradle 6.8.1
------------------------------------------------------------
Build time: 2021-01-22 13:20:08 UTC
Revision: 31f14a87d93945024ab7a78de84102a3400fa5b2
Kotlin: 1.4.20
Groovy: 2.5.12
Ant: Apache Ant(TM) version 1.10.9 compiled on September 27 2020
JVM: 11.0.10 (JetBrains s.r.o. 11.0.10+0-b96-7249189)
OS: Mac OS X 12.1 aarch64
Where does the Kotlin version 1.4.20
come from and how to I update that version? I don’t have that version specified anywhere in my project. In fact, I’m using 1.6.20
for kotlin-multiplatform. How do I set Kotlin to the same version? Thanks!Milse113
02/11/2022, 8:53 PM@Register
annotation which allows the manager class to automatically register it. While I could do this with reflection, I want to be able to include and exclude some of these modules at compile-time (IE if I have a beta vs prod build), and preferably also generate the list at compile-time. from what I can tell, the best way to do this is with a compiler plugin, but I can’t find a framework which allows this as most major Kotlin compiler plugin libraries don’t allow modifying existing files, but instead only creating new ones. How should I go about doing this?Andrew Reed
02/12/2022, 10:42 AMVampire
02/12/2022, 8:09 PMpublic interface Transformer<OUT, IN> {
OUT transform(IN in);
}
And a Java method
filter(Transformer<String, String> var1)
And Kotlin wrongly assumes the second parameter to be non-null
.
How can you call it with a lambda that returns null
?Turyin
02/13/2022, 7:33 PMTuryin
02/13/2022, 7:53 PMAyfri
02/14/2022, 5:26 AMinline fun <T : Any> T.create(block: T.() -> Unit) = jso(block)
But the receiver type is of the companion object of the class and doesn't work if the class doesn't have any companion object, so this is not what I want
I want to be able to do AnyClass.create { }
and have the receiver equal to the receiver when doing AnyClass().apply { }
Eivind
02/14/2022, 8:22 AMNahuel Canavy
02/14/2022, 8:51 AMzain
02/14/2022, 9:38 AMThe logic for creating the fixture is in pairs. You have to form a pair of two teams in a random
fashion. For example - If I have 4 teams - T1, T2, T3, T4. I can have:
(T1 vs T2) & (T3 vs T4)
OR
(T1 vs T3) & (T2 vs T4)
OR
(T1 vs T4) & (T2 vs T3)
*Your function should accept any number n, where n is an even number > 0 and return you n/2
pairs/tuples*
Naveed
02/14/2022, 7:37 PMAyfri
02/14/2022, 10:21 PMeventemitter3
library with a pretty ugly (I think) but simple solution
sealed interface DisplayObjectEvents<T : Any> {
object added : DisplayObjectEvents<Container>
object click : DisplayObjectEvents<FederatedPointerEvent>
object clickcapture : DisplayObjectEvents<FederatedPointerEvent>
object destroyed : DisplayObjectEvents<Nothing>
object mousedown : DisplayObjectEvents<FederatedPointerEvent>
...
(there are almost 100 elements in total)
}
But I want to be able to use it like that myObject.on<DisplayObjectEvents.added> { // it is Container here }
, so I created this
inline fun <reified T : DisplayObjectEvents<T>> DisplayObject.on(noinline callback: (T) -> Unit) = on(T::class.simpleName!!, callback as ListenerFn, null)
// ListenerFn is just a function type that take Any arguments Any number of time
// Which is the default type for my library for events in TypeScript
But
myObject.on<DisplayObject.added> {
// it is of type DisplayObject.added
}
What I've done wrong ?james
02/14/2022, 10:38 PMoriginalString
.replace("cat", "dog")
.replace("goat", "cow")
.replace("horse", "pig")
..but chaining those replace()
calls would become very inefficient I assume, even if I created a map to iterate over.
does Kotlin have any functions I can use to do a single pass over originalString
and replace X number of items?zain
02/15/2022, 9:53 AMprivate fun simulateFixtures(list: List<List<Team>>) = list.map { fixture ->
fixture.toList().random()
}.shuffled().chunked(2)
Lukasz Kalnik
02/15/2022, 2:59 PMList<Int>
to an IntArray
(not Array<Int>
)?Lukasz Kalnik
02/15/2022, 4:32 PM0..1
) to another range (say 1_000..5_000
)?
Basically the equivalent of this:
fun getRangeValueAtInterval(start: Int, end: Int, interval: Float): Int =
start + (interval * (end - start)).roundToInt()
Dan O'Brien
02/15/2022, 8:30 PM{}
block after instantiating the class?Jan
02/15/2022, 9:17 PMTheOnlyTails
02/16/2022, 7:32 PMval list = listOf<<T>(x: T) -> Unit>()
Jerv Norsk
02/17/2022, 9:13 AMUğur
02/17/2022, 1:59 PMBernard Ng
02/17/2022, 3:42 PMBernard Ng
02/17/2022, 3:42 PMRichard Gomez
02/17/2022, 3:43 PMBernard Ng
02/17/2022, 3:48 PMelizarov
02/18/2022, 9:38 AMflygerian.eagle
03/14/2022, 3:25 AM