Accumulator factory: Difference between revisions

Content added Content deleted
(Added Kotlin)
(→‎{{header|Kotlin}}: Added code and output previously omitted)
Line 1,531: Line 1,531:


=={{header|Kotlin}}==
=={{header|Kotlin}}==
Overloads would be needed for all six primitive numeric types but, in the interests of brevity, only two overloads of 'foo' have been coded:

<lang scala>// version 1.0.6
<lang scala>// version 1.0.6


fun foo(n: Double): (d: Double) -> Double {
var nn = n
return { nn += it; nn }
}

fun foo(n: Int): (i: Int) -> Int {
var nn = n
return { nn += it; nn }
}

fun main(args: Array<String>) {
val x = foo(1.0) // calls 'Double' overload
x(5.0)
foo(3.0)
println(x(2.3))
val y = foo(1) // calls 'Int' overload
y(5)
foo(5)
println(y(2))
}
</lang>
</lang>


{{out}}
{{out}}
<pre>
<pre>
8.3

8
</pre>
</pre>