lab
08/16/2017, 8:52 PMlab
08/16/2017, 8:52 PMlab
08/16/2017, 9:42 PMList<String>
like so:
{ "Hello", "ABC", "yo", "This is", "There are", "An App", "Hey", Help" }
I have a query : String = "Hl"
in Kotlin, how would I construct a filter such that the output is:
{ "Hello", Help" }
If I have to implement the algorithm, what would be a good one? Or is the only way to do so is to loop through each char in the query string?
Secondly, if the query is query : String = "a"
, and let's say the output is
{ "ABC", "There are", "An App"}
I would like to sort this list based on the first index of query
, e.g:
{ "ABC", "An App", "There are"}
How should I do so in Kotlin?
My attempt:
val listOfFilteredString = listOfString
.filter { s-> s.contains(query, ignoreCase = true) }
.sortedWith(compareBy { s -> s.label.indexOf(query) })
It's not working 😕karelpeeters
08/16/2017, 9:46 PMtimrijckaert
08/16/2017, 9:51 PMget
function that wraps around a getString
, getInt
, methods.
I wrote this https://try.kotlinlang.org/#/UserProjects/hcd4agpck875pl6a8ad31vg5vg/h83porfurr8cru60q1gv95kntm
I'm not sure what is going on because this code seems to work fine for Kotlin v1.0.7 but not on v1.1.3.
Anyone care to take a look?damien5314
08/16/2017, 9:58 PMjoshuaavalon
08/17/2017, 6:10 AMstefano.maffullo
08/17/2017, 7:22 AMjelloranger
08/17/2017, 7:51 AMjelloranger
08/17/2017, 7:51 AMjelloranger
08/17/2017, 7:52 AMkarelpeeters
08/17/2017, 7:57 AMarguments.getInt().takeUnless{ it == 0 }?.let{action()}
karelpeeters
08/17/2017, 7:57 AMtakeIf
for the opposite case.jelloranger
08/17/2017, 8:00 AMjelloranger
08/17/2017, 8:00 AMkarelpeeters
08/17/2017, 8:00 AMkarelpeeters
08/17/2017, 8:01 AMalso
and apply
.jelloranger
08/17/2017, 8:01 AMmenegatti
08/17/2017, 8:01 AMlet
works for referencing it
menegatti
08/17/2017, 8:01 AMtakeIf
returns the object in case the condition was met, null otherwisemenegatti
08/17/2017, 8:02 AMpublic inline fun <T> T.takeIf(predicate: (T) -> Boolean): T? = if (predicate(this)) this else null
note that the return type is T?
jelloranger
08/17/2017, 8:03 AMmenegatti
08/17/2017, 8:03 AMsetText
receives a String
mannodermaus
08/17/2017, 8:03 AM@StringRes int
jelloranger
08/17/2017, 8:03 AMjelloranger
08/17/2017, 8:04 AMmenegatti
08/17/2017, 8:05 AMjelloranger
08/17/2017, 8:05 AMmenegatti
08/17/2017, 8:05 AMarguments.getInt(TEXT_KEY).takeIf { it != 0 }?.let {
text.setText(it)
}