{
  "Id": 202,
  "OrigId": 0,
  "Title": "Sum of squares",
  "LeadParagraph": "Calculate the sum of squares _s of _data, an array of floating point values.",
  "ExtraKeywords": "reduce",
  "Author": "Bart",
  "CreationDate": "2019-09-28T20:37:11.726064Z",
  "LastEditor": "programming-idioms.org",
  "EditSummary": "[Python] edit",
  "LastEditedImplID": 6982,
  "OriginalAttributionURL": "",
  "Picture": "",
  "ImageURL": "",
  "ImageWidth": 0,
  "ImageHeight": 0,
  "ImageAlt": "",
  "Version": 48,
  "VersionDate": "2025-09-11T19:44:12.311593Z",
  "Implementations": [
    {
      "Id": 3466,
      "OrigId": -1,
      "Author": "Bart",
      "CreationDate": "2019-09-28T20:37:11.726064Z",
      "LastEditor": "programming-idioms.org",
      "LanguageName": "Pascal",
      "CodeBlock": "var\n  data: array of double;\n...\n  s := SumOfSquares(data);\n...",
      "OriginalAttributionURL": "",
      "DemoURL": "",
      "DocumentationURL": "",
      "AuthorComment": "",
      "Version": 2,
      "VersionDate": "2021-12-07T10:07:15.952746Z",
      "Rating": 0,
      "Checked": false,
      "ImportsBlock": "uses math;",
      "PictureURL": "",
      "Protected": false
    },
    {
      "Id": 3469,
      "OrigId": 3469,
      "Author": "spectrum",
      "CreationDate": "2019-09-28T21:33:13.106368Z",
      "LastEditor": "spectrum",
      "LanguageName": "Fortran",
      "CodeBlock": "s = sum( data**2 )",
      "OriginalAttributionURL": "",
      "DemoURL": "",
      "DocumentationURL": "",
      "AuthorComment": "_data may be a multidimensional array (e.g. data(:,:)).",
      "Version": 2,
      "VersionDate": "2019-09-28T21:44:19.184373Z",
      "Rating": 0,
      "Checked": false,
      "ImportsBlock": "",
      "PictureURL": "",
      "Protected": false
    },
    {
      "Id": 3475,
      "OrigId": 3475,
      "Author": "steenslag",
      "CreationDate": "2019-09-28T22:04:53.195305Z",
      "LastEditor": "programming-idioms.org",
      "LanguageName": "Ruby",
      "CodeBlock": "s = data.sum{|i| i**2}",
      "OriginalAttributionURL": "",
      "DemoURL": "https://replit.com/@ProgIdioms/TotalEdibleDisk#main.rb",
      "DocumentationURL": "https://ruby-doc.org/core-3.1.2/Enumerable.html#method-i-sum",
      "AuthorComment": "",
      "Version": 5,
      "VersionDate": "2022-08-16T15:23:19.761945Z",
      "Rating": 0,
      "Checked": false,
      "ImportsBlock": "",
      "PictureURL": "",
      "Protected": false
    },
    {
      "Id": 3483,
      "OrigId": 3483,
      "Author": "0bit",
      "CreationDate": "2019-09-29T06:24:24.192688Z",
      "LastEditor": "programming-idioms.org",
      "LanguageName": "Python",
      "CodeBlock": "s = sum(i**2 for i in data)",
      "OriginalAttributionURL": "",
      "DemoURL": "https://replit.com/@ProgIdioms/ClutteredImprobableOutsourcing#main.py",
      "DocumentationURL": "",
      "AuthorComment": "",
      "Version": 2,
      "VersionDate": "2022-08-16T13:58:13.521243Z",
      "Rating": 0,
      "Checked": false,
      "ImportsBlock": "",
      "PictureURL": "",
      "Protected": false
    },
    {
      "Id": 3495,
      "OrigId": 3495,
      "Author": "daxim",
      "CreationDate": "2019-09-29T10:40:05.143611Z",
      "LastEditor": "daxim",
      "LanguageName": "Perl",
      "CodeBlock": "my $s = sum map { $_ ** 2 } @data;",
      "OriginalAttributionURL": "",
      "DemoURL": "",
      "DocumentationURL": "http://p3rl.org/List::Util#sum",
      "AuthorComment": "",
      "Version": 1,
      "VersionDate": "2019-09-29T10:40:05.143611Z",
      "Rating": 0,
      "Checked": false,
      "ImportsBlock": "use List::Util qw(sum);",
      "PictureURL": "",
      "Protected": false
    },
    {
      "Id": 3497,
      "OrigId": 3497,
      "Author": "TravelingTechGuy",
      "CreationDate": "2019-09-29T11:08:19.943854Z",
      "LastEditor": "programming-idioms.org",
      "LanguageName": "JS",
      "CodeBlock": "s = data.reduce((a, c) =\u003e a + c ** 2, 0)",
      "OriginalAttributionURL": "",
      "DemoURL": "https://jsfiddle.net/k2j5cyah/",
      "DocumentationURL": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce",
      "AuthorComment": "Array._reduce uses a function  to reduce the array into a single sum of all the elements' squares. \r\n\r\nThe initial accumulator's value is 0.",
      "Version": 3,
      "VersionDate": "2022-08-16T14:03:06.908977Z",
      "Rating": 0,
      "Checked": false,
      "ImportsBlock": "",
      "PictureURL": "",
      "Protected": false
    },
    {
      "Id": 3642,
      "OrigId": 3642,
      "Author": "Skepter",
      "CreationDate": "2019-10-05T05:04:54.499638Z",
      "LastEditor": "programming-idioms.org",
      "LanguageName": "Java",
      "CodeBlock": "double s = Arrays.stream(data).map(i -\u003e i * i).sum();",
      "OriginalAttributionURL": "",
      "DemoURL": "https://replit.com/@ProgIdioms/CaringShallowHack#Main.java",
      "DocumentationURL": "https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/util/stream/DoubleStream.html#sum()",
      "AuthorComment": "_data has type double[]",
      "Version": 2,
      "VersionDate": "2022-08-16T15:35:23.790533Z",
      "Rating": 0,
      "Checked": false,
      "ImportsBlock": "import java.util.Arrays;",
      "PictureURL": "",
      "Protected": false
    },
    {
      "Id": 3770,
      "OrigId": 3770,
      "Author": "senfoel",
      "CreationDate": "2020-01-06T11:42:22.112398Z",
      "LastEditor": "senfoel",
      "LanguageName": "Haskell",
      "CodeBlock": "sumOfSquares = sum . map (^2)",
      "OriginalAttributionURL": "",
      "DemoURL": "",
      "DocumentationURL": "",
      "AuthorComment": "Because _data is a keyword in Haskell, this is a function for computing the sum of squares.\r\nsumOfSquares :: Num c =\u003e [c] -\u003e c",
      "Version": 1,
      "VersionDate": "2020-01-06T11:42:22.112398Z",
      "Rating": 0,
      "Checked": false,
      "ImportsBlock": "",
      "PictureURL": "",
      "Protected": false
    },
    {
      "Id": 3830,
      "OrigId": 3830,
      "Author": "CeaselessBanana",
      "CreationDate": "2020-03-17T13:20:52.702354Z",
      "LastEditor": "programming-idioms.org",
      "LanguageName": "Rust",
      "CodeBlock": "let s = data.iter().map(|x| x.powi(2)).sum::\u003cf32\u003e();",
      "OriginalAttributionURL": "",
      "DemoURL": "https://play.rust-lang.org/?version=stable\u0026mode=debug\u0026edition=2018\u0026gist=9cd0160869fde631476ac9655a11dab0",
      "DocumentationURL": "https://doc.rust-lang.org/nightly/std/primitive.f32.html#method.powi",
      "AuthorComment": "",
      "Version": 3,
      "VersionDate": "2022-08-16T15:42:59.043376Z",
      "Rating": 0,
      "Checked": false,
      "ImportsBlock": "",
      "PictureURL": "",
      "Protected": false
    },
    {
      "Id": 3948,
      "OrigId": 3948,
      "Author": "misha",
      "CreationDate": "2020-04-29T12:10:45.480614Z",
      "LastEditor": "programming-idioms.org",
      "LanguageName": "Clojure",
      "CodeBlock": "(defn square [x] (* x x))\n\n(def s (reduce + (map square data)))",
      "OriginalAttributionURL": "",
      "DemoURL": "",
      "DocumentationURL": "",
      "AuthorComment": "",
      "Version": 2,
      "VersionDate": "2020-05-03T21:13:48.751653Z",
      "Rating": 0,
      "Checked": false,
      "ImportsBlock": "",
      "PictureURL": "",
      "Protected": false
    },
    {
      "Id": 3979,
      "OrigId": 3979,
      "Author": "programming-idioms.org",
      "CreationDate": "2020-05-03T21:11:02.977256Z",
      "LastEditor": "programming-idioms.org",
      "LanguageName": "Clojure",
      "CodeBlock": "(defn square [x] (* x x))\n\n(def s (-\u003e\u003e data (map square) (reduce +)))",
      "OriginalAttributionURL": "",
      "DemoURL": "",
      "DocumentationURL": "https://clojuredocs.org/clojure.core/-%3E%3E",
      "AuthorComment": "",
      "Version": 1,
      "VersionDate": "2020-05-03T21:11:02.977256Z",
      "Rating": 0,
      "Checked": false,
      "ImportsBlock": "",
      "PictureURL": "",
      "Protected": false
    },
    {
      "Id": 3980,
      "OrigId": 3980,
      "Author": "programming-idioms.org",
      "CreationDate": "2020-05-03T21:12:26.99783Z",
      "LastEditor": "programming-idioms.org",
      "LanguageName": "Clojure",
      "CodeBlock": "(defn square [x] (* x x))\n\n(def s (transduce (map square) + data))",
      "OriginalAttributionURL": "",
      "DemoURL": "",
      "DocumentationURL": "",
      "AuthorComment": "This doesn't create any intermediate collection",
      "Version": 1,
      "VersionDate": "2020-05-03T21:12:26.99783Z",
      "Rating": 0,
      "Checked": false,
      "ImportsBlock": "",
      "PictureURL": "",
      "Protected": false
    },
    {
      "Id": 4107,
      "OrigId": 4107,
      "Author": "Kit Grose",
      "CreationDate": "2020-09-07T01:19:06.234696Z",
      "LastEditor": "programming-idioms.org",
      "LanguageName": "Csharp",
      "CodeBlock": "var s = data.Sum(x =\u003e x * x);",
      "OriginalAttributionURL": "",
      "DemoURL": "https://sharplab.io/#gist:aa459285562f76a450c4cb406868d66b",
      "DocumentationURL": "https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.sum",
      "AuthorComment": "",
      "Version": 2,
      "VersionDate": "2022-08-16T15:49:28.139416Z",
      "Rating": 0,
      "Checked": false,
      "ImportsBlock": "using System.Linq;",
      "PictureURL": "",
      "Protected": false
    },
    {
      "Id": 4354,
      "OrigId": 4354,
      "Author": "breml",
      "CreationDate": "2020-12-05T13:25:11.232429Z",
      "LastEditor": "programming-idioms.org",
      "LanguageName": "Go",
      "CodeBlock": "var s float64\nfor _, d := range data {\n\ts += math.Pow(d, 2)\n}",
      "OriginalAttributionURL": "",
      "DemoURL": "https://go.dev/play/p/a8YP4-ZBhxJ",
      "DocumentationURL": "https://golang.org/pkg/math/#Pow",
      "AuthorComment": "",
      "Version": 3,
      "VersionDate": "2022-03-13T14:11:54.650543Z",
      "Rating": 0,
      "Checked": false,
      "ImportsBlock": "import \"math\"",
      "PictureURL": "",
      "Protected": false
    },
    {
      "Id": 5221,
      "OrigId": 5221,
      "Author": "glaforge",
      "CreationDate": "2021-11-02T13:53:10.027518Z",
      "LastEditor": "programming-idioms.org",
      "LanguageName": "Groovy",
      "CodeBlock": "def s = data.sum { it ** 2 }",
      "OriginalAttributionURL": "",
      "DemoURL": "https://groovyconsole.appspot.com/script/4868807545847808",
      "DocumentationURL": "https://docs.groovy-lang.org/1.8.6/html/groovy-jdk/java/util/Collection.html#sum(groovy.lang.Closure)",
      "AuthorComment": "",
      "Version": 3,
      "VersionDate": "2022-08-16T14:09:28.900601Z",
      "Rating": 0,
      "Checked": false,
      "ImportsBlock": "",
      "PictureURL": "",
      "Protected": false
    },
    {
      "Id": 5529,
      "OrigId": 5529,
      "Author": "Manu",
      "CreationDate": "2022-03-11T18:15:45.090857Z",
      "LastEditor": "programming-idioms.org",
      "LanguageName": "Dart",
      "CodeBlock": "var s = data.map((v) =\u003e v * v).reduce((sum, v) =\u003e sum + v);",
      "OriginalAttributionURL": "",
      "DemoURL": "https://dartpad.dev/?id=b1bc8c158c28374f36c414291ab71aa1",
      "DocumentationURL": "https://api.flutter.dev/flutter/dart-core/Iterable/reduce.html",
      "AuthorComment": "",
      "Version": 3,
      "VersionDate": "2022-03-13T14:26:12.936404Z",
      "Rating": 0,
      "Checked": false,
      "ImportsBlock": "",
      "PictureURL": "",
      "Protected": false
    },
    {
      "Id": 5854,
      "OrigId": 5854,
      "Author": "programming-idioms.org",
      "CreationDate": "2022-08-16T14:13:41.59263Z",
      "LastEditor": "programming-idioms.org",
      "LanguageName": "Ruby",
      "CodeBlock": "s = data.sum{ _1**2 }",
      "OriginalAttributionURL": "",
      "DemoURL": "https://replit.com/@ProgIdioms/ThankfulLimitedLoopfusion#main.rb",
      "DocumentationURL": "https://ruby-doc.org/core-3.1.2/Enumerable.html#method-i-sum",
      "AuthorComment": "Since Ruby 2.7",
      "Version": 2,
      "VersionDate": "2022-08-16T15:23:29.097403Z",
      "Rating": 0,
      "Checked": false,
      "ImportsBlock": "",
      "PictureURL": "",
      "Protected": false
    },
    {
      "Id": 6839,
      "OrigId": 6839,
      "Author": "reilas",
      "CreationDate": "2024-11-08T22:54:02.687539Z",
      "LastEditor": "reilas",
      "LanguageName": "Java",
      "CodeBlock": "double s = of(data).map(x -\u003e x * x).sum();",
      "OriginalAttributionURL": "",
      "DemoURL": "",
      "DocumentationURL": "https://docs.oracle.com/en/java/javase/23/docs/api/java.base/java/util/stream/DoubleStream.html",
      "AuthorComment": "",
      "Version": 3,
      "VersionDate": "2024-11-12T00:41:29.451665Z",
      "Rating": 0,
      "Checked": false,
      "ImportsBlock": "import static java.util.stream.DoubleStream.of;",
      "PictureURL": "",
      "Protected": false
    },
    {
      "Id": 6930,
      "OrigId": 6930,
      "Author": "reilas",
      "CreationDate": "2024-11-13T18:30:20.62538Z",
      "LastEditor": "reilas",
      "LanguageName": "Java",
      "CodeBlock": "BigDecimal s = of(data)\n    .mapToObj(String::valueOf)\n    .map(BigDecimal::new)\n    .map(x -\u003e x.multiply(x))\n    .reduce(BigDecimal::add)\n    .get();",
      "OriginalAttributionURL": "",
      "DemoURL": "",
      "DocumentationURL": "",
      "AuthorComment": "",
      "Version": 2,
      "VersionDate": "2024-11-15T01:15:53.85346Z",
      "Rating": 0,
      "Checked": false,
      "ImportsBlock": "import static java.util.stream.DoubleStream.of;\r\nimport java.math.BigDecimal;",
      "PictureURL": "",
      "Protected": false
    },
    {
      "Id": 6982,
      "OrigId": 6982,
      "Author": "reilas",
      "CreationDate": "2024-11-18T19:00:23.611857Z",
      "LastEditor": "reilas",
      "LanguageName": "Python",
      "CodeBlock": "s = sum(map(mul, data, data))",
      "OriginalAttributionURL": "",
      "DemoURL": "",
      "DocumentationURL": "https://docs.python.org/3/library/functions.html#map",
      "AuthorComment": "",
      "Version": 4,
      "VersionDate": "2025-09-11T19:44:12.307655Z",
      "Rating": 0,
      "Checked": false,
      "ImportsBlock": "from operator import mul",
      "PictureURL": "",
      "Protected": false
    }
  ],
  "ImplCount": 20,
  "Rating": 0,
  "WordsTitle": null,
  "Words": null,
  "Checked": false,
  "RelatedIdiomIds": null,
  "RelatedIdiomTitles": null,
  "Protected": false,
  "Variables": [
    "s",
    "data"
  ],
  "RelatedURLs": null,
  "RelatedURLLabels": null
}