// Created on savesnippets.com ยท https://savesnippets.com/DQbP23ZgR9r8Qa // Takes a function as a parameter fun retry(times: Int, action: () -> T): T { var lastError: Throwable? = null repeat(times) { try { return action() } catch (e: Throwable) { lastError = e } } throw lastError!! } // Returns a function fun multiplier(factor: Int): (Int) -> Int = { it * factor } fun main() { val triple = multiplier(3) println(triple(7)) // 21 val result = retry(3) { if (Math.random() < 0.7) throw Exception("flaky") "success" } println(result) // Pass method reference instead of a lambda val upper: (String) -> String = String::uppercase println(upper("hello")) // HELLO }