Sum of squares: Difference between revisions

Content added Content deleted
(→‎{{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: Line 1,845:
<lang nim>import math, sequtils
<lang nim>import math, sequtils


proc sumSquares[T: SomeNumber](a: openArray[T]): T =
echo sum(map(@[1,2,3,4,5], proc (x: int): int = x*x))</lang>
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}}==
=={{header|Objeck}}==