Jump to content

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 =
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}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.