Skip to content

Instantly share code, notes, and snippets.

@Farhandroid
Last active March 22, 2022 08:32
Show Gist options
  • Select an option

  • Save Farhandroid/36b2363e90d6ac3c995877ef98a5b058 to your computer and use it in GitHub Desktop.

Select an option

Save Farhandroid/36b2363e90d6ac3c995877ef98a5b058 to your computer and use it in GitHub Desktop.
Search
class SearchUtil(private val list: List<Int>) {
fun searchItem(element: Int, foundItem: (element: Int?) -> Unit) {
val itemFoundList = list.filter {
it == element
}
if (itemFoundList.isNullOrEmpty())
foundItem(null)
else
foundItem(itemFoundList.first())
}
}
fun search() {
val searchUtil = SearchUtil(listOf(25, 46, 35))
searchUtil.searchItem(element = 25) {
println("Search result : $it")
}
}
fun main() {
search()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment