Anonymous user
Sum of squares: Difference between revisions
Put the code in a procedure. Made the code generic. Changed "map" to "mapIt". Added an example with an empty sequence. Added output.
(→{{header|Tcl}}: Cleaned it up.) |
(Put the code in a procedure. Made the code generic. Changed "map" to "mapIt". Added an example with an empty sequence. Added output.) |
||
Line 1,845:
<lang nim>import math, sequtils
proc sumSquares[T: SomeNumber](a: openArray[T]): T =
sum(a.mapIt(it * it))
let a1 = [1, 2, 3, 4, 5]
echo a1, " → ", sumSquares(a1)
let a2: seq[float] = @[]
echo a2, " → ", sumSquares(a2)</lang>
{{out}}
<pre>[1, 2, 3, 4, 5] → 55
@[] → 0.0</pre>
=={{header|Objeck}}==
|