Bird
0
0

How can you use Kotlin's first-class functions to create a reusable filter function that takes a list and a condition function, returning only elements that satisfy the condition?

hard📝 Application Q8 of 15
Kotlin - Functions
How can you use Kotlin's first-class functions to create a reusable filter function that takes a list and a condition function, returning only elements that satisfy the condition?
ADefine a function that accepts a List and a predicate function, then returns a filtered list using the predicate
BCreate a class with a method that filters the list internally without parameters
CUse a global variable to store the condition and filter the list manually
DWrite a function that only accepts lists of integers and filters by fixed condition
Step-by-Step Solution
Solution:
  1. Step 1: Understand first-class functions usage

    Passing a condition function as a parameter allows flexible filtering.
  2. Step 2: Define a function accepting a list and a predicate function

    The function applies the predicate to filter elements and returns the filtered list.
  3. Final Answer:

    Define a function that accepts a List and a predicate function, then returns a filtered list using the predicate -> Option A
  4. Quick Check:

    Reusable filter = function + predicate parameter [OK]
Quick Trick: Pass condition functions to customize behavior [OK]
Common Mistakes:
MISTAKES
  • Hardcoding filter conditions inside function
  • Not passing condition as a function parameter
  • Using global variables for conditions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes