Created
August 19, 2017 06:07
-
-
Save minibugdev/5abb19edb98fd8e40f969a72ab9ae070 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// implement search function that can be filter by name,priceRange or category | |
fun List<Product>.search(name: String? = null, | |
priceRange: Pair<Double, Double>? = null, | |
categoryId: String? = null): List<Product> = TODO("Make it happen") |
pitiphong-p
commented
Aug 19, 2017
•
// implement search function that can be filter by name,priceRange or category
fun List<Product>.search(name: String? = null,
priceRange: Pair<Double, Double>? = null,
categoryId: String? = null): List<Product> {
return this.filter { product -> name?.let { product.name.startsWith(it) } ?: true }
.filter { product -> priceRange?.let { product.price in it.first..it.second } ?: true }
.filter { product -> categoryId?.let { product.category.id == it } ?: true }
}
Admin solution
fun List<Product>.search(name: String? = null,
priceRange: Pair<Double, Double>? = null,
categoryId: String? = null): List<Product> = this
.let { filteredList -> if (name != null) filteredList.filter { it.name.startsWith(name) } else filteredList }
.let { filteredList -> if (priceRange != null) filteredList.filter { it.price in priceRange.first..priceRange.second } else filteredList }
.let { filteredList -> if (categoryId != null) filteredList.filter { it.category.id == categoryId } else filteredList }
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment