Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
int s = 0;
for (int i : x) {
  s += i;
}
import java.util.stream.IntStream;
int s = IntStream.of(x).sum();
int i = 0, n = x.length, s = x[i];
while (++i < n) s = s + x[i];
import java.math.BigInteger;
import java.util.Arrays;
BigInteger s
    = Arrays.stream(x)
            .mapToObj(String::valueOf)
            .map(BigInteger::new)
            .reduce(BigInteger::add)
            .get();
import static java.lang.String.valueOf;
import java.math.BigInteger;
BigInteger s = new BigInteger(valueOf(x[0]));
for (int i = 1, n = x.length; i < n; ++i)
    s = s.add(new BigInteger(valueOf(x[i])));
for E of x loop
   S := S + E;
end loop;
int sum = 0;
for (int i = 0; i < n; ++i) {
  sum += x[i];
}
int i,s;
for(i=s=0;i<n;i++)
{
	s+=x[i];
}
List.fold_left (+) 0 l
(defn sum [x] (reduce + x))

(sum [1 20 300])
;;=> 321
#include <ranges>
auto s = std::ranges::fold_left(x, 0, std::plus<int>{});
#include <functional>
#include <numeric>
int s = std::accumulate(x.begin(), x.end(), 0, std::plus<int>());
using System.Collections.Generic;
using System.Linq;
var s = x.Sum();
import std.algorithm.iteration;
auto s = x.sum();
var s = x.fold(0, (a, b) => a + b);
s = Enum.sum(x)
S = lists:sum(X).
   s = sum(x)
s := 0
for _, v := range x {
	s += v
}
s = sum x
var s = x.reduce((a, b) => a + b, 0);
var s = x.reduce((a, b) => a + b)
val numbers = listOf(1, 2, 3, 4)
val sum = numbers.sum()
(reduce #'+ x)
s = 0
for i,v in ipairs(x) do
   s = s + v
end
@import Foundation;
id s=[x valueForKeyPath:@"@sum.self"];
$s = array_sum($x);
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;
use List::Util 'sum';
$s = sum(@x);
s = sum(x)
s = x.reduce(:+)
s = x.sum
x.iter().sum()
let s = x.iter().sum::<i32>();
val s = x.sum()
(define s (fold-right + 0 x))
(define s (apply + x))
(define s (apply fx+ x))
(define s (apply + x))
(define s (foldr + 0 x))
s := x sum.
Dim s As Integer = x.Sum()

New implementation...
< >
deleplace