Logo

Programming-Idioms

Calculate the sum s of the integer list or array x.
Implementation
Smalltalk

Implementation edit is for fixing errors and enhancing with metadata. Please do not replace the code below with a different implementation.

Instead of changing the code of the snippet, consider creating another Smalltalk implementation.

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating material.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
s = sum(x)
s := 0
for _, v := range x {
	s += v
}
use List::Util 'sum';
$s = sum(@x);
$s = array_sum($x);
x.iter().sum()
int i,s;
for(i=s=0;i<n;i++)
{
	s+=x[i];
}
import std.algorithm.iteration;
auto s = x.sum();
var
  _x: Array of integer;
  _s, i: Integer;
begin
  _s := 0;
  //assume _x contains some values
  for i := Low(_x) to High(_x) do _s := _s + _x[i];
end;
var s = x.fold(0, (a, b) => a + b);
s = x.sum
s = Enum.sum(x)
s = sum x
int s = 0;
for (int i = 0; i < x.size(); i++) {
	s += x[i];
}
using System.Collections.Generic;
using System.Linq;
var s = x.Sum();
#include <functional>
#include <numeric>
int s = std::accumulate(x.begin(), x.end(), 0, std::plus<int>());
S = lists:sum(X).
s = 0
for i,v in ipairs(x) do
   s = s + v
end
s = x.reduce(:+)
var s = x.reduce((a, b) => a + b, 0);
(define s (foldr + 0 x))
let s = x.iter().sum::<i32>();
val s = x.sum()
List.fold_left (+) 0 l
(defn sum [x] (reduce + x))

(sum [1 20 300])
;;=> 321
(reduce #'+ x)
   s = sum(x)
var s = x.reduce((a, b) => a + b)
val numbers = listOf(1, 2, 3, 4)
val sum = numbers.sum()
Dim s As Integer = x.Sum()
int sum = 0;
for (int i = 0; i < n; ++i) {
  sum += x[i];
}
@import Foundation;
id s=[x valueForKeyPath:@"@sum.self"];
(define s (fold-right + 0 x))
(define s (apply + x))
(define s (apply fx+ x))
for E of x loop
   S := S + E;
end loop;
(define s (apply + x))