Kotlin Koans 22 All, Any and other predicates

Contents

All, Any and other predicates (Playground)

Description

Implement all the functions below using all, any, count, find.

all, any, count, findを使用して、以下のすべての関数を実装してください。

val numbers = listOf(-1, 0, 2)
val isZero: (Int) -> Boolean = { it == 0 }
numbers.any(isZero) == true
numbers.all(isZero) == false
numbers.count(isZero) == 1
numbers.find { it > 0 } == 2

Code

// Return true if all customers are from the given city
fun Shop.checkAllCustomersAreFrom(city: City): Boolean = customers.all { it.city == city }

// Return true if there is at least one customer from the given city
fun Shop.hasCustomerFrom(city: City): Boolean = customers.any { it.city == city }

// Return the number of customers from the given city
fun Shop.countCustomersFrom(city: City): Int = customers.count { it.city == city }

// Return a customer who lives in the given city, or null if there is none
fun Shop.findAnyCustomerFrom(city: City): Customer? = customers.find { it.city == city }

Memo

  • all ... 全要素がラムダ式にマッチする(trueである)場合、trueを返す
  • any ... 1つ以上の要素がラムダ式にマッチする(trueである)場合、trueを返す
  • count ... ラムダ式にマッチする(trueである)要素の数を返す
  • find ... ラムダ式にマッチする(trueである)最初の要素を返す

ラムダ式についてはLambdasをおさらい。

← Posts