Created
October 31, 2019 13:44
-
-
Save mhashim6/c44ba58b31eac98f916fa90195fced2d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
*@author mhashim6 on 31/10/2019 | |
*/ | |
fun main() { | |
val ns = arrayOf(5, 6, 7) | |
val p1 = Point(5, 7) | |
val p2 = Point(5, 6) | |
val ps = arrayOf(p1, p2) | |
println(sum(ns) { x1, x2 -> (x1 ?: 0) + (x2 ?: 0) }) | |
println(sum(ps) { point1, point2 -> | |
Point( | |
(point1?.x ?: 0) + (point2?.x ?: 0), | |
(point1?.y ?: 0) + (point2?.y ?: 0) | |
) | |
}) | |
} | |
fun <T> sum(ns: Array<T>, algorithm: (T?, T?) -> T): T? { | |
var res: T? = null | |
for (n in ns) | |
res = algorithm(n, res) | |
return res | |
} | |
data class Point(var x: Int, var y: Int) | |
fun Point.square() = Point(x * x, y * y) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment