Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading