39 lines
834 B
Kotlin
Raw Normal View History

package fixtures
data class Person(
val id: Int,
val name: String,
val email: String
)
class Repository<T> {
private val items = mutableMapOf<Int, T>()
fun add(key: Int, item: T) {
items[key] = item
}
fun find(key: Int): T? = items[key]
fun getAll(): List<T> = items.values.toList()
}
fun add(a: Int, b: Int): Int = a + b
fun <T> filter(items: List<T>, predicate: (T) -> Boolean): List<T> {
return items.filter(predicate)
}
fun main() {
val repo = Repository<Person>()
repo.add(1, Person(1, "Alice", "alice@example.com"))
repo.add(2, Person(2, "Bob", "bob@example.com"))
repo.getAll().forEach { println(it) }
println("3 + 4 = ${add(3, 4)}")
val numbers = listOf(1, 2, 3, 4, 5)
val evens = filter(numbers) { it % 2 == 0 }
println("Evens: $evens")
}