kotlin

How to combine two different length lists in kotlin?

ぐ巨炮叔叔 提交于 2020-12-10 00:37:37
问题 I want to combine two different length lists. For example; val list1 = listOf(1,2,3,4,5) val list2 = listOf("a","b","c") I want to result like this (1,"a",2,"b",3,"c",4,5) Is there any suggestion? 回答1: You may use the .zip function for that list1.zip(list2){ a,b -> listOf(a,b)}.flatten() The only problem is that it will only process elements, with both sets, so if (like in the example) let's have different size - it will not work The alternative could be to add specific markers and filter

How to combine two different length lists in kotlin?

£可爱£侵袭症+ 提交于 2020-12-10 00:33:11
问题 I want to combine two different length lists. For example; val list1 = listOf(1,2,3,4,5) val list2 = listOf("a","b","c") I want to result like this (1,"a",2,"b",3,"c",4,5) Is there any suggestion? 回答1: You may use the .zip function for that list1.zip(list2){ a,b -> listOf(a,b)}.flatten() The only problem is that it will only process elements, with both sets, so if (like in the example) let's have different size - it will not work The alternative could be to add specific markers and filter

How to configure the processResources task in a Gradle Kotlin build

允我心安 提交于 2020-12-08 15:03:38
问题 I have the following in a groovy-based build script. How do I do the same in a kotlin-based script? processResources { filesMatching('application.properties'){ expand(project.properties) } } 回答1: I think task should look like: Edit : According this comment in gradle/kotlin-dsl repository. Task configuration should work this way: import org.gradle.language.jvm.tasks.ProcessResources apply { plugin("java") } (tasks.getByName("processResources") as ProcessResources).apply { filesMatching(

How to configure the processResources task in a Gradle Kotlin build

安稳与你 提交于 2020-12-08 15:00:21
问题 I have the following in a groovy-based build script. How do I do the same in a kotlin-based script? processResources { filesMatching('application.properties'){ expand(project.properties) } } 回答1: I think task should look like: Edit : According this comment in gradle/kotlin-dsl repository. Task configuration should work this way: import org.gradle.language.jvm.tasks.ProcessResources apply { plugin("java") } (tasks.getByName("processResources") as ProcessResources).apply { filesMatching(

KoinAppAlreadyStartedException: A Koin Application has already been started

ぃ、小莉子 提交于 2020-12-08 10:51:42
问题 Using koin-2.0.1 for Android testing and unable to test all 3 test together though each test passes separately. class NumberFormatterUtilImplTest : KoinTest { private val numberFormatterUtil: NumberFormatterUtilImpl by inject() @Before fun setUp() { startKoin { modules(utilsModule) } } @Test fun `does formatter returns two digit faction if supplied one digit value`() { val result = numberFormatterUtil.getAdjustedCurrencyRate(18.0) Assert.assertEquals(result, 18.00, 1.0) } @Test fun `does

KoinAppAlreadyStartedException: A Koin Application has already been started

时光怂恿深爱的人放手 提交于 2020-12-08 10:51:28
问题 Using koin-2.0.1 for Android testing and unable to test all 3 test together though each test passes separately. class NumberFormatterUtilImplTest : KoinTest { private val numberFormatterUtil: NumberFormatterUtilImpl by inject() @Before fun setUp() { startKoin { modules(utilsModule) } } @Test fun `does formatter returns two digit faction if supplied one digit value`() { val result = numberFormatterUtil.getAdjustedCurrencyRate(18.0) Assert.assertEquals(result, 18.00, 1.0) } @Test fun `does

Kotlin smart cast the second value of a pair with filter

允我心安 提交于 2020-12-08 07:51:11
问题 I'm trying to write a function that maps a String and Int? into a pair, then filters for non null second values in the pair, before continuing to map. My code looks like this: val ids: List<String> = listOf("a", "b", "c") val ints: Map<String, Int?> = mapOf("a" to 1, "b" to 2, "c" to null) ids.map { id: String -> Pair(id, ints[id]) }.filter { pair -> pair.second != null}.map { pair: Pair<String, Int> -> func(id, pair.second) } The problem is that the second map has the error: Type inference

Type 'State<List<User>?>' has no method 'getValue(Nothing?, KProperty<*>)' and thus it cannot serve as a delegate

我的未来我决定 提交于 2020-12-08 07:27:55
问题 I'm trying to get a value from LiveData with observeAsState in jetpack compose, but I get a weird error Type 'State<List?>' has no method 'getValue(Nothing?, KProperty<*>)' and thus it cannot serve as a delegate Code @Composable fun UserScreen(userViewModel:UserViewModel){ val items: List<User> by userViewModel.fetchUserList.observeAsState() UserList(userList = items) } ViewModel class UserViewModel: ViewModel() { private val dataSource = UserDataSource() val fetchUserList = liveData { emit

What is the purpose of 'let' keyword in Kotlin [duplicate]

梦想与她 提交于 2020-12-08 06:07:15
问题 This question already has answers here : Example of when should we use run, let, apply, also and with on Kotlin (5 answers) Closed last year . We can write the code with or without let as follows. var str = "Hello World" str.let { println("$it!!") } OR var str = "Hello World" println("$str!!") What is the Actual use of let ?.Is that more memory efficient or more readable? 回答1: let is one of Kotlin's Scope functions which allow you to execute a code block within the context of an object. In

How can I force user to enter emoji only in edittext

守給你的承諾、 提交于 2020-12-07 07:46:29
问题 I'm using androidx.emoji.widget.EmojiEditText. I want to force user to enter only emojis here. And limit him to max 3 emojis. How can I do something like this? I tried to use external emoji keyboard that cancels the soft keyboard and shows up but didn't work properly. <androidx.emoji.widget.EmojiEditText android:id="@+id/etEmoji" android:layout_width="match_parent" android:layout_height="30dp" android:layout_margin="10dp" android:hint="Enter Emoji" android:inputType="textShortMessage" android