What is a higher order function ?
A higher order function does one, or both, of the following:
fun foo(firstParam: String, someFunction: (String) -\> String) {
println(someFunction(firstParam))
}fun bar(firstParam: String): (String) -\> String = {
println("The first parameter is $firstParam")
}foo(“FIRST”, bar() )
Why would you want a function
to return another function ?
There are 2 common reasons to have
a function return another function:
Create a variable that
contains a function.
val isEven: (Int) -> Boolean = modulo(2)
OR
val isEven: { k: Int -> k % 2 == 0 }
listOf(1,2,3,4,5,6).filter(isEven)
What is a closure ?
A function that has access to variables
and parameters defined in outer scopes.
It is said that they “close over” these
variables.
What is an anonymous function ?
What is a top-level function ?
Top-level functions are defined outside the scope of a class or interface.
fun isEven(k) = k % 2 == 0
val intList = listOf(1,2,3,4,5,6)
intList.filter { isEven(it) }
// OR
intList.filter(::isEven)Create an extension function
on Int that identifies an odd number.
fun Int.isOdd() = this % 2 != 0
val intList = listOf(1,2,3,4,5,6)
intList.filter { it.isOdd() }
// OR
intList.filter(Int ::isOdd)Create an extension function
on Int that returns a to the power of b.
fun foo(a: Double, b: Double, f: (Double, Double) -\> Double) = f(a,b)
foo(2.0, 3.0, { a, b -\> Math.pow(a,b) })
// OR
foo(2.0, 3.0, Math.pow)Explain a bound function reference.
fun String.equalsIgnoreCase(other: String) =
this.toLowerCase() == other.toLowerCase()
val stringList = listOf("foo", "bar", "baz", "buz")
stringList.filter("bar"::equalsIgnoreCase)
// Instead of
stringList.filter { (String:equalsIgnoreCase) ("bar", it) }Explain a function-literal receiver.
fun foo(fn: String.() -\> Boolean) {
var someBoolean = "some string".fn()
...
}Explain inline functions ?
inline fun foo() { }
What is noinline and
when would you use it ?
* sometimes you do not want a function to be inlined * typically used when creating an inline higher-order function, but you don't want a function parameter to be inlined
inline fun foo(noinline fn: (Int) -> Boolean) { }
Explain function currying.
Why is function currying useful?
What is the arity of a function ?
The number of parameters.
Provide an example of
function currying.
fun compute(logger: (String) -\> Unit) : Unit { }
fun logger(level: Level, appender: Appendable, message: String) : Unit { }Without currying:
fun compute { message -> log(Level.Warn, Appender.Console, message) }
With currying:
val myLogger = ::logger.curried() (Level.SEVERE) (System.out)
fun compute(myLogger)
Explain memoization.
fun memoize(fn: (A) -\> R) : (A) -\> R {
val map = ConcurrentHashMap()
return { a -\>
map.getOrPut(a) {
fn(a)
}
}
}val memquery = memoize(::query)
What is a type alias ?
typealias String1 = String
What is either ?
sealed class Either<out></out>
Left<out> (value: L) : Either<l>() { }<br></br>class Right<out> (value: R) : Either<nothing>() { }</nothing></out></l></out>
Explain sealed classes.
Explain declaration-site variance.