Daniele B
02/07/2023, 9:21 PMval listA = listOf("A","B","C","D","E","F","G","H","I","J","K","L")
val listB = listOf("C","E","H","K")
I would like to run the function myfunc(String)
on all elements of listA that are not on listB.
What is the most idiomatic way in Kotlin to achieve that?Laertes Moustakas
02/07/2023, 9:26 PMlistA.filter { it !in listB }.forEach(::myfunc)
Or map(::myfunc)
instead of forEach
, depends on whether you want to store whatever myfunc
outputs in a new list.Daniele B
02/07/2023, 9:32 PMSaharath Kleips
02/07/2023, 9:48 PM(listA - listB).forEach(::myfunc)
works for what I believe you’re asking for as well I believeDaniele B
02/07/2023, 9:51 PMLaertes Moustakas
02/07/2023, 9:53 PMminus()
operator function.