From the course: Kotlin for Android: Best Practices

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Functional programming: find, filter

Functional programming: find, filter - Kotlin Tutorial

From the course: Kotlin for Android: Best Practices

Start my 1-month free trial

Functional programming: find, filter

- [Narrator] Besides map and reduce, there are a lot of other functional programming constructs you can use in Kotlin. Let's look at find and filter. Here, I have the data class Food, which has a name whether it is healthy or whether is a snack. Inside main, I have three Food. I have cabbage, carrot, and chips, and I throw them all into the list and call it groceries. So let's say it's four o'clock, I'm hungry, I want a snack. So I want to look for a snack in my grocery bag. Normally if you're not doing functional programming, you will do a for-loop and check if it is a snack and return early. In functional programming, you can focus on the fact that you want to check it's a snack, and do away with the whole for-loop and return and things of that kind. So, here, this is how I'm looking for my snack. I take the data groceries, and I call .find and I give it a predicate, meaning what is the criteria that you're looking for. The predicate checks that it is a snack. Let's run this. So…

Contents