Complete the code to declare a generic function that returns the input value.
fun <T> identity(value: [1]): T { return value }
The generic type T is used as the parameter type to make the function generic.
Complete the code to declare a generic function that swaps two values in a Pair.
fun <T> swap(pair: Pair<T, T>): Pair<T, T> {
return Pair(pair.second, [1])
}The function returns a new Pair with the first and second values swapped. So the second value is paired with the original first value.
Fix the error in the generic function declaration that returns the first element of a list.
fun <T> firstElement(list: List<[1]>): T? { return list.firstOrNull() }
The generic type parameter T must be used inside the List type to match the function's return type.
Fill both blanks to declare a generic function that returns the size of a collection.
fun <[1]> collectionSize(collection: Collection<[2]>): Int { return collection.size }
The generic type parameter is declared as T and used as the element type of the collection.
Fill all three blanks to declare a generic function that returns a map from keys to values filtered by a predicate.
fun <[1], [2]> filterMap( map: Map<[1], [2]>, predicate: ([3], [2]) -> Boolean ): Map<[1], [2]> { return map.filter { (key, value) -> predicate(key, value) } }
The generic types are declared as K and V for key and value respectively. They are used consistently in the Map and predicate types.