Search code examples
listkotlindictionaryapply

Apply a function to elements in a list based on condition in Kotlin


I have a list of elements, and I wish to apply functions to them based on a certain criteria related to them.

I'd like to know if this is possible the way I'm imagining it, something like this hypothetical code. Each function x and y would return an element:

list.map{
 if (it == something)
  functionx(it)
 else
  functiony(it) 
}

And the list would end up with all its original elements transformed by those functions.

Is this possible?


Solution

  • The code you provided should work exactly as you said. The map function "returns a list containing the results of applying the given transform function to each element in the original array".

    If this code doesn't work, please provide more information about exactly what you're trying to achieve.