[{"data":1,"prerenderedAt":1444},["ShallowReactive",2],{"blog:2014:sequence-averages-in-scala":3,"blogMore-Development":622,"comments-sequence-averages-in-scala":635},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"date":10,"category":11,"tags":12,"excerpt":15,"body":30,"_type":613,"_id":614,"_source":615,"_file":616,"_stem":617,"_extension":618,"url":619,"wordCount":620,"minutes":621,"commentCount":621},"/blog/2014/sequence-averages-in-scala","2014",false,"en","Sequence averages in Scala","I’ve been learning Scala and decided to put together a C# to Scala cheat sheet. All is going well but then I got stuck on the equivalent of Average.","2014-12-11T20:42:11+00:00","Development",[13,14],"C#","Scala",{"type":16,"children":17},"root",[18,25],{"type":19,"tag":20,"props":21,"children":22},"element","p",{},[23],{"type":24,"value":9},"text",{"type":19,"tag":20,"props":26,"children":27},{},[28],{"type":24,"value":29},"Enumerable.Average in .NET calculates a mean average from your sequence by summing up all the values and counting them in a single pass then returning the sum divided by the count in a floating point format (or decimal).",{"type":16,"children":31,"toc":606},[32,36,40,47,52,102,107,127,133,138,239,245,250,263,282,294,299,438,443,449,454,591,600],{"type":19,"tag":20,"props":33,"children":34},{},[35],{"type":24,"value":9},{"type":19,"tag":20,"props":37,"children":38},{},[39],{"type":24,"value":29},{"type":19,"tag":41,"props":42,"children":44},"h2",{"id":43},"the-problem",[45],{"type":24,"value":46},"The problem",{"type":19,"tag":20,"props":48,"children":49},{},[50],{"type":24,"value":51},"Given that Scala has nothing built-in there are more than a few suggestions online that boil down to:",{"type":19,"tag":53,"props":54,"children":59},"pre",{"className":55,"code":56,"language":57,"meta":58,"style":58},"language-scala shiki shiki-themes everforest-light dracula","val average = seq.sum / seq.length\n","scala","",[60],{"type":19,"tag":61,"props":62,"children":63},"code",{"__ignoreMap":58},[64],{"type":19,"tag":65,"props":66,"children":69},"span",{"class":67,"line":68},"line",1,[70,76,82,87,92,97],{"type":19,"tag":65,"props":71,"children":73},{"style":72},"--shiki-default:#F57D26;--shiki-dark:#FF79C6",[74],{"type":24,"value":75},"val",{"type":19,"tag":65,"props":77,"children":79},{"style":78},"--shiki-default:#5C6A72;--shiki-dark:#F8F8F2",[80],{"type":24,"value":81}," average ",{"type":19,"tag":65,"props":83,"children":84},{"style":72},[85],{"type":24,"value":86},"=",{"type":19,"tag":65,"props":88,"children":89},{"style":78},[90],{"type":24,"value":91}," seq.sum ",{"type":19,"tag":65,"props":93,"children":94},{"style":72},[95],{"type":24,"value":96},"/",{"type":19,"tag":65,"props":98,"children":99},{"style":78},[100],{"type":24,"value":101}," seq.length\n",{"type":19,"tag":20,"props":103,"children":104},{},[105],{"type":24,"value":106},"This has a few problems:",{"type":19,"tag":108,"props":109,"children":110},"ol",{},[111,117,122],{"type":19,"tag":112,"props":113,"children":114},"li",{},[115],{"type":24,"value":116},"Visiting a sequence twice can be inefficient",{"type":19,"tag":112,"props":118,"children":119},{},[120],{"type":24,"value":121},"Sum can overflow as it is the same type as the sequence",{"type":19,"tag":112,"props":123,"children":124},{},[125],{"type":24,"value":126},"Applied to an integer without casting it returns an integer average",{"type":19,"tag":41,"props":128,"children":130},{"id":129},"a-solution",[131],{"type":24,"value":132},"A solution",{"type":19,"tag":20,"props":134,"children":135},{},[136],{"type":24,"value":137},"Scala provides a useful high-order function called foldLeft. Its job is to take an initial state and a function then keep applying the function with each value to the state. So one more efficient solution to the problem is:",{"type":19,"tag":53,"props":139,"children":141},{"className":55,"code":140,"language":57,"meta":58,"style":58},"val average = seq.foldLeft((0.0, 1)) ((acc, i) => ((acc._1 + (i - acc._1) / acc._2), acc._2 + 1))._1\n",[142],{"type":19,"tag":61,"props":143,"children":144},{"__ignoreMap":58},[145],{"type":19,"tag":65,"props":146,"children":147},{"class":67,"line":68},[148,152,156,160,165,171,176,181,186,191,196,201,206,211,216,220,225,229,234],{"type":19,"tag":65,"props":149,"children":150},{"style":72},[151],{"type":24,"value":75},{"type":19,"tag":65,"props":153,"children":154},{"style":78},[155],{"type":24,"value":81},{"type":19,"tag":65,"props":157,"children":158},{"style":72},[159],{"type":24,"value":86},{"type":19,"tag":65,"props":161,"children":162},{"style":78},[163],{"type":24,"value":164}," seq.foldLeft((",{"type":19,"tag":65,"props":166,"children":168},{"style":167},"--shiki-default:#DF69BA;--shiki-dark:#BD93F9",[169],{"type":24,"value":170},"0.0",{"type":19,"tag":65,"props":172,"children":173},{"style":78},[174],{"type":24,"value":175},", ",{"type":19,"tag":65,"props":177,"children":178},{"style":167},[179],{"type":24,"value":180},"1",{"type":19,"tag":65,"props":182,"children":183},{"style":78},[184],{"type":24,"value":185},")) ((acc, i) ",{"type":19,"tag":65,"props":187,"children":188},{"style":72},[189],{"type":24,"value":190},"=>",{"type":19,"tag":65,"props":192,"children":193},{"style":78},[194],{"type":24,"value":195}," ((acc._1 ",{"type":19,"tag":65,"props":197,"children":198},{"style":72},[199],{"type":24,"value":200},"+",{"type":19,"tag":65,"props":202,"children":203},{"style":78},[204],{"type":24,"value":205}," (i ",{"type":19,"tag":65,"props":207,"children":208},{"style":72},[209],{"type":24,"value":210},"-",{"type":19,"tag":65,"props":212,"children":213},{"style":78},[214],{"type":24,"value":215}," acc._1) ",{"type":19,"tag":65,"props":217,"children":218},{"style":72},[219],{"type":24,"value":96},{"type":19,"tag":65,"props":221,"children":222},{"style":78},[223],{"type":24,"value":224}," acc._2), acc._2 ",{"type":19,"tag":65,"props":226,"children":227},{"style":72},[228],{"type":24,"value":200},{"type":19,"tag":65,"props":230,"children":231},{"style":167},[232],{"type":24,"value":233}," 1",{"type":19,"tag":65,"props":235,"children":236},{"style":78},[237],{"type":24,"value":238},"))._1\n",{"type":19,"tag":41,"props":240,"children":242},{"id":241},"how-does-this-work",[243],{"type":24,"value":244},"How does this work?",{"type":19,"tag":20,"props":246,"children":247},{},[248],{"type":24,"value":249},"What we do here is calculate an average as we go, adding the new weighted average each time.",{"type":19,"tag":20,"props":251,"children":252},{},[253,255,261],{"type":24,"value":254},"It achieves this by setting up a tuple to contain our initial state with ",{"type":19,"tag":256,"props":257,"children":258},"strong",{},[259],{"type":24,"value":260},"(0.0, 1)",{"type":24,"value":262},". This specifies our starting average of 0.0 and our starting position of 1.",{"type":19,"tag":20,"props":264,"children":265},{},[266,268,273,275,280],{"type":24,"value":267},"The next part specifies the function that takes that state as ",{"type":19,"tag":256,"props":269,"children":270},{},[271],{"type":24,"value":272},"acc",{"type":24,"value":274}," (for accumulator) and the next value in the sequence as ",{"type":19,"tag":256,"props":276,"children":277},{},[278],{"type":24,"value":279},"i",{"type":24,"value":281}," and calculates our rolling average for each value and increases the position as it goes along.",{"type":19,"tag":20,"props":283,"children":284},{},[285,287,292],{"type":24,"value":286},"Finally at the end of our call we specify ",{"type":19,"tag":256,"props":288,"children":289},{},[290],{"type":24,"value":291},"._1",{"type":24,"value":293}," which tells the compiler we want the first value from the tuple (the average) as we no longer care about the position.",{"type":19,"tag":20,"props":295,"children":296},{},[297],{"type":24,"value":298},"If you wanted to make this function more reusable you could do this:",{"type":19,"tag":53,"props":300,"children":302},{"className":55,"code":301,"language":57,"meta":58,"style":58},"def average(s: Seq[Int]): Double = s.foldLeft((0.0, 1)) ((acc, i) => ((acc._1 + (i - acc._1) / acc._2), acc._2 + 1))._1\n",[303],{"type":19,"tag":61,"props":304,"children":305},{"__ignoreMap":58},[306],{"type":19,"tag":65,"props":307,"children":308},{"class":67,"line":68},[309,315,321,326,332,337,343,348,353,358,363,368,373,378,382,386,390,394,398,402,406,410,414,418,422,426,430,434],{"type":19,"tag":65,"props":310,"children":312},{"style":311},"--shiki-default:#F85552;--shiki-dark:#FF79C6",[313],{"type":24,"value":314},"def",{"type":19,"tag":65,"props":316,"children":318},{"style":317},"--shiki-default:#8DA101;--shiki-dark:#50FA7B",[319],{"type":24,"value":320}," average",{"type":19,"tag":65,"props":322,"children":323},{"style":78},[324],{"type":24,"value":325},"(",{"type":19,"tag":65,"props":327,"children":329},{"style":328},"--shiki-default:#5C6A72;--shiki-default-font-style:inherit;--shiki-dark:#FFB86C;--shiki-dark-font-style:italic",[330],{"type":24,"value":331},"s",{"type":19,"tag":65,"props":333,"children":334},{"style":78},[335],{"type":24,"value":336},": ",{"type":19,"tag":65,"props":338,"children":340},{"style":339},"--shiki-default:#DFA000;--shiki-dark:#8BE9FD",[341],{"type":24,"value":342},"Seq",{"type":19,"tag":65,"props":344,"children":345},{"style":78},[346],{"type":24,"value":347},"[",{"type":19,"tag":65,"props":349,"children":350},{"style":339},[351],{"type":24,"value":352},"Int",{"type":19,"tag":65,"props":354,"children":355},{"style":78},[356],{"type":24,"value":357},"])",{"type":19,"tag":65,"props":359,"children":360},{"style":72},[361],{"type":24,"value":362},":",{"type":19,"tag":65,"props":364,"children":365},{"style":339},[366],{"type":24,"value":367}," Double",{"type":19,"tag":65,"props":369,"children":370},{"style":72},[371],{"type":24,"value":372}," =",{"type":19,"tag":65,"props":374,"children":375},{"style":78},[376],{"type":24,"value":377}," s.foldLeft((",{"type":19,"tag":65,"props":379,"children":380},{"style":167},[381],{"type":24,"value":170},{"type":19,"tag":65,"props":383,"children":384},{"style":78},[385],{"type":24,"value":175},{"type":19,"tag":65,"props":387,"children":388},{"style":167},[389],{"type":24,"value":180},{"type":19,"tag":65,"props":391,"children":392},{"style":78},[393],{"type":24,"value":185},{"type":19,"tag":65,"props":395,"children":396},{"style":72},[397],{"type":24,"value":190},{"type":19,"tag":65,"props":399,"children":400},{"style":78},[401],{"type":24,"value":195},{"type":19,"tag":65,"props":403,"children":404},{"style":72},[405],{"type":24,"value":200},{"type":19,"tag":65,"props":407,"children":408},{"style":78},[409],{"type":24,"value":205},{"type":19,"tag":65,"props":411,"children":412},{"style":72},[413],{"type":24,"value":210},{"type":19,"tag":65,"props":415,"children":416},{"style":78},[417],{"type":24,"value":215},{"type":19,"tag":65,"props":419,"children":420},{"style":72},[421],{"type":24,"value":96},{"type":19,"tag":65,"props":423,"children":424},{"style":78},[425],{"type":24,"value":224},{"type":19,"tag":65,"props":427,"children":428},{"style":72},[429],{"type":24,"value":200},{"type":19,"tag":65,"props":431,"children":432},{"style":167},[433],{"type":24,"value":233},{"type":19,"tag":65,"props":435,"children":436},{"style":78},[437],{"type":24,"value":238},{"type":19,"tag":20,"props":439,"children":440},{},[441],{"type":24,"value":442},"Be aware you might need multiple overloads for each numeric sequence type you want to be able to average given the lack of a common numeric trait that allows for the subtraction and division.",{"type":19,"tag":41,"props":444,"children":446},{"id":445},"precision-and-rounding",[447],{"type":24,"value":448},"Precision and rounding",{"type":19,"tag":20,"props":450,"children":451},{},[452],{"type":24,"value":453},"There is some slight variance in results between this approach and the total / count due to rounding precision. If you wanted to preserve that you could always add and then divide at the end still in a single pass much like .NET does but with Scala’s foldLeft rather than a foreach.",{"type":19,"tag":53,"props":455,"children":457},{"className":55,"code":456,"language":57,"meta":58,"style":58},"def average(s: Seq[Int]): Double = { val t = s.foldLeft((0.0, 0)) ((acc, i) => (acc._1 + i, acc._2 + 1)); t._1 / t._2 }\n",[458],{"type":19,"tag":61,"props":459,"children":460},{"__ignoreMap":58},[461],{"type":19,"tag":65,"props":462,"children":463},{"class":67,"line":68},[464,468,472,476,480,484,488,492,496,500,504,508,512,517,521,526,530,534,538,542,547,551,555,560,564,569,573,577,582,586],{"type":19,"tag":65,"props":465,"children":466},{"style":311},[467],{"type":24,"value":314},{"type":19,"tag":65,"props":469,"children":470},{"style":317},[471],{"type":24,"value":320},{"type":19,"tag":65,"props":473,"children":474},{"style":78},[475],{"type":24,"value":325},{"type":19,"tag":65,"props":477,"children":478},{"style":328},[479],{"type":24,"value":331},{"type":19,"tag":65,"props":481,"children":482},{"style":78},[483],{"type":24,"value":336},{"type":19,"tag":65,"props":485,"children":486},{"style":339},[487],{"type":24,"value":342},{"type":19,"tag":65,"props":489,"children":490},{"style":78},[491],{"type":24,"value":347},{"type":19,"tag":65,"props":493,"children":494},{"style":339},[495],{"type":24,"value":352},{"type":19,"tag":65,"props":497,"children":498},{"style":78},[499],{"type":24,"value":357},{"type":19,"tag":65,"props":501,"children":502},{"style":72},[503],{"type":24,"value":362},{"type":19,"tag":65,"props":505,"children":506},{"style":339},[507],{"type":24,"value":367},{"type":19,"tag":65,"props":509,"children":510},{"style":72},[511],{"type":24,"value":372},{"type":19,"tag":65,"props":513,"children":514},{"style":78},[515],{"type":24,"value":516}," { ",{"type":19,"tag":65,"props":518,"children":519},{"style":72},[520],{"type":24,"value":75},{"type":19,"tag":65,"props":522,"children":523},{"style":78},[524],{"type":24,"value":525}," t ",{"type":19,"tag":65,"props":527,"children":528},{"style":72},[529],{"type":24,"value":86},{"type":19,"tag":65,"props":531,"children":532},{"style":78},[533],{"type":24,"value":377},{"type":19,"tag":65,"props":535,"children":536},{"style":167},[537],{"type":24,"value":170},{"type":19,"tag":65,"props":539,"children":540},{"style":78},[541],{"type":24,"value":175},{"type":19,"tag":65,"props":543,"children":544},{"style":167},[545],{"type":24,"value":546},"0",{"type":19,"tag":65,"props":548,"children":549},{"style":78},[550],{"type":24,"value":185},{"type":19,"tag":65,"props":552,"children":553},{"style":72},[554],{"type":24,"value":190},{"type":19,"tag":65,"props":556,"children":557},{"style":78},[558],{"type":24,"value":559}," (acc._1 ",{"type":19,"tag":65,"props":561,"children":562},{"style":72},[563],{"type":24,"value":200},{"type":19,"tag":65,"props":565,"children":566},{"style":78},[567],{"type":24,"value":568}," i, acc._2 ",{"type":19,"tag":65,"props":570,"children":571},{"style":72},[572],{"type":24,"value":200},{"type":19,"tag":65,"props":574,"children":575},{"style":167},[576],{"type":24,"value":233},{"type":19,"tag":65,"props":578,"children":579},{"style":78},[580],{"type":24,"value":581},")); t._1 ",{"type":19,"tag":65,"props":583,"children":584},{"style":72},[585],{"type":24,"value":96},{"type":19,"tag":65,"props":587,"children":588},{"style":78},[589],{"type":24,"value":590}," t._2 }\n",{"type":19,"tag":20,"props":592,"children":593},{},[594],{"type":19,"tag":595,"props":596,"children":597},"em",{},[598],{"type":24,"value":599},"[)amien",{"type":19,"tag":601,"props":602,"children":603},"style",{},[604],{"type":24,"value":605},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":58,"searchDepth":607,"depth":607,"links":608},2,[609,610,611,612],{"id":43,"depth":607,"text":46},{"id":129,"depth":607,"text":132},{"id":241,"depth":607,"text":244},{"id":445,"depth":607,"text":448},"markdown","content:blog:2014:sequence-averages-in-scala.md","content","blog/2014/sequence-averages-in-scala.md","blog/2014/sequence-averages-in-scala","md","/blog/2014/sequence-averages-in-scala/",518,3,[623,627,631],{"title":624,"date":625,"url":626},"HTML5 Video Cheatsheet: Optimizing videos for the web","2025-12-05T00:00:00Z","/blog/2025/html5-video-cheatsheet/",{"title":628,"date":629,"url":630},"Transactions in the MongoDB EF Core Provider","2025-10-25","/blog/2025/mongodb-explicit-transactions/",{"title":632,"date":633,"url":634},"Queryable Encryption with the MongoDB EF Core Provider","2025-09-22","/blog/2025/mongodb-queryable-encryption/",[636,840,1312],{"_path":637,"_dir":638,"_draft":6,"_partial":6,"_locale":7,"title":639,"description":640,"id":641,"name":642,"email":643,"avatar":644,"url":645,"date":646,"body":647,"_type":613,"_id":837,"_source":615,"_file":838,"_stem":839,"_extension":618},"/comments/sequence-averages-in-scala/123673","sequence-averages-in-scala","123673","@Rick That's close to the second option although I think you want that initial state declared in the Aggregate function rather than on the select? For fun here's the C# version of the first one that calculates a running average:",123673,"Damien Guard","damien@envytech.co.uk","https://www.gravatar.com/avatar/dc72963e7279d34c85ed4c0b731ce5a9?r=pg&d=retro","https://damieng.com/","2014-12-12T11:05:41",{"type":16,"children":648,"toc":835},[649,653,831],{"type":19,"tag":20,"props":650,"children":651},{},[652],{"type":24,"value":640},{"type":19,"tag":53,"props":654,"children":658},{"className":655,"code":656,"language":657,"meta":58,"style":58},"language-csharp shiki shiki-themes everforest-light dracula","var average = s.Aggregate(Tuple.Create(0.0, 1), (acc, i) => Tuple.Create((acc.Item1 + (i - acc.Item1) / acc.Item2), acc.Item2 + 1)).Item1;\n","csharp",[659],{"type":19,"tag":61,"props":660,"children":661},{"__ignoreMap":58},[662],{"type":19,"tag":65,"props":663,"children":664},{"class":67,"line":68},[665,671,675,679,684,689,694,699,703,707,711,715,720,724,728,732,737,741,746,750,755,761,766,770,774,779,783,787,791,795,800,805,809,813,817,822,826],{"type":19,"tag":65,"props":666,"children":668},{"style":667},"--shiki-default:#3A94C5;--shiki-dark:#FF79C6",[669],{"type":24,"value":670},"var",{"type":19,"tag":65,"props":672,"children":673},{"style":78},[674],{"type":24,"value":81},{"type":19,"tag":65,"props":676,"children":677},{"style":72},[678],{"type":24,"value":86},{"type":19,"tag":65,"props":680,"children":681},{"style":78},[682],{"type":24,"value":683}," s.",{"type":19,"tag":65,"props":685,"children":686},{"style":317},[687],{"type":24,"value":688},"Aggregate",{"type":19,"tag":65,"props":690,"children":691},{"style":78},[692],{"type":24,"value":693},"(Tuple.",{"type":19,"tag":65,"props":695,"children":696},{"style":317},[697],{"type":24,"value":698},"Create",{"type":19,"tag":65,"props":700,"children":701},{"style":78},[702],{"type":24,"value":325},{"type":19,"tag":65,"props":704,"children":705},{"style":167},[706],{"type":24,"value":170},{"type":19,"tag":65,"props":708,"children":709},{"style":78},[710],{"type":24,"value":175},{"type":19,"tag":65,"props":712,"children":713},{"style":167},[714],{"type":24,"value":180},{"type":19,"tag":65,"props":716,"children":717},{"style":78},[718],{"type":24,"value":719},"), (",{"type":19,"tag":65,"props":721,"children":722},{"style":328},[723],{"type":24,"value":272},{"type":19,"tag":65,"props":725,"children":726},{"style":78},[727],{"type":24,"value":175},{"type":19,"tag":65,"props":729,"children":730},{"style":328},[731],{"type":24,"value":279},{"type":19,"tag":65,"props":733,"children":734},{"style":78},[735],{"type":24,"value":736},") ",{"type":19,"tag":65,"props":738,"children":739},{"style":72},[740],{"type":24,"value":190},{"type":19,"tag":65,"props":742,"children":743},{"style":78},[744],{"type":24,"value":745}," Tuple.",{"type":19,"tag":65,"props":747,"children":748},{"style":317},[749],{"type":24,"value":698},{"type":19,"tag":65,"props":751,"children":752},{"style":78},[753],{"type":24,"value":754},"((acc.",{"type":19,"tag":65,"props":756,"children":758},{"style":757},"--shiki-default:#35A77C;--shiki-dark:#F8F8F2",[759],{"type":24,"value":760},"Item1",{"type":19,"tag":65,"props":762,"children":763},{"style":72},[764],{"type":24,"value":765}," +",{"type":19,"tag":65,"props":767,"children":768},{"style":78},[769],{"type":24,"value":205},{"type":19,"tag":65,"props":771,"children":772},{"style":72},[773],{"type":24,"value":210},{"type":19,"tag":65,"props":775,"children":776},{"style":78},[777],{"type":24,"value":778}," acc.",{"type":19,"tag":65,"props":780,"children":781},{"style":757},[782],{"type":24,"value":760},{"type":19,"tag":65,"props":784,"children":785},{"style":78},[786],{"type":24,"value":736},{"type":19,"tag":65,"props":788,"children":789},{"style":72},[790],{"type":24,"value":96},{"type":19,"tag":65,"props":792,"children":793},{"style":78},[794],{"type":24,"value":778},{"type":19,"tag":65,"props":796,"children":797},{"style":757},[798],{"type":24,"value":799},"Item2",{"type":19,"tag":65,"props":801,"children":802},{"style":78},[803],{"type":24,"value":804},"), acc.",{"type":19,"tag":65,"props":806,"children":807},{"style":757},[808],{"type":24,"value":799},{"type":19,"tag":65,"props":810,"children":811},{"style":72},[812],{"type":24,"value":765},{"type":19,"tag":65,"props":814,"children":815},{"style":167},[816],{"type":24,"value":233},{"type":19,"tag":65,"props":818,"children":819},{"style":78},[820],{"type":24,"value":821},")).",{"type":19,"tag":65,"props":823,"children":824},{"style":757},[825],{"type":24,"value":760},{"type":19,"tag":65,"props":827,"children":828},{"style":78},[829],{"type":24,"value":830},";\n",{"type":19,"tag":601,"props":832,"children":833},{},[834],{"type":24,"value":605},{"title":58,"searchDepth":607,"depth":607,"links":836},[],"content:comments:sequence-averages-in-scala:123673.md","comments/sequence-averages-in-scala/123673.md","comments/sequence-averages-in-scala/123673",{"_path":841,"_dir":638,"_draft":6,"_partial":6,"_locale":7,"title":842,"description":843,"id":844,"name":845,"email":846,"avatar":847,"date":848,"body":849,"_type":613,"_id":1309,"_source":615,"_file":1310,"_stem":1311,"_extension":618},"/comments/sequence-averages-in-scala/123660","123660","For fun, I knocked up a similar implementation in C#",123660,"Rik Hemsley","rik@hemsley.cc","https://www.gravatar.com/avatar/f6daa836adc0801062716fcce7336f92?r=pg&d=retro","2014-12-12T07:25:52",{"type":16,"children":850,"toc":1307},[851,855,1303],{"type":19,"tag":20,"props":852,"children":853},{},[854],{"type":24,"value":843},{"type":19,"tag":53,"props":856,"children":858},{"className":655,"code":857,"language":657,"meta":58,"style":58},"public static class EnumerableExtensions {\n    private struct ValueAndCount {\n        public decimal Value;\n        public ulong Count;\n        public decimal Mean { get { return Value/Count; } }\n    }\n\n    public static decimal Mean(this IEnumerable\u003Cint> sequence) {\n        return sequence\n            .Select(n => new ValueAndCount {Value = n, Count = 1})\n            .Aggregate ((agg, valueAndCount) =>\n            new ValueAndCount {Value = agg.Value + valueAndCount.Value, Count = agg.Count + 1} )\n            .Mean();\n    }\n}\n\n",[859],{"type":19,"tag":61,"props":860,"children":861},{"__ignoreMap":58},[862,891,914,932,950,995,1004,1014,1075,1089,1153,1193,1268,1286,1294],{"type":19,"tag":65,"props":863,"children":864},{"class":67,"line":68},[865,870,875,880,886],{"type":19,"tag":65,"props":866,"children":867},{"style":72},[868],{"type":24,"value":869},"public",{"type":19,"tag":65,"props":871,"children":872},{"style":72},[873],{"type":24,"value":874}," static",{"type":19,"tag":65,"props":876,"children":877},{"style":311},[878],{"type":24,"value":879}," class",{"type":19,"tag":65,"props":881,"children":883},{"style":882},"--shiki-default:#3A94C5;--shiki-dark:#8BE9FD",[884],{"type":24,"value":885}," EnumerableExtensions",{"type":19,"tag":65,"props":887,"children":888},{"style":78},[889],{"type":24,"value":890}," {\n",{"type":19,"tag":65,"props":892,"children":893},{"class":67,"line":607},[894,899,904,910],{"type":19,"tag":65,"props":895,"children":896},{"style":72},[897],{"type":24,"value":898},"    private",{"type":19,"tag":65,"props":900,"children":901},{"style":667},[902],{"type":24,"value":903}," struct",{"type":19,"tag":65,"props":905,"children":907},{"style":906},"--shiki-default:#3A94C5;--shiki-default-font-style:inherit;--shiki-dark:#8BE9FD;--shiki-dark-font-style:italic",[908],{"type":24,"value":909}," ValueAndCount",{"type":19,"tag":65,"props":911,"children":912},{"style":78},[913],{"type":24,"value":890},{"type":19,"tag":65,"props":915,"children":916},{"class":67,"line":621},[917,922,927],{"type":19,"tag":65,"props":918,"children":919},{"style":72},[920],{"type":24,"value":921},"        public",{"type":19,"tag":65,"props":923,"children":924},{"style":667},[925],{"type":24,"value":926}," decimal",{"type":19,"tag":65,"props":928,"children":929},{"style":78},[930],{"type":24,"value":931}," Value;\n",{"type":19,"tag":65,"props":933,"children":935},{"class":67,"line":934},4,[936,940,945],{"type":19,"tag":65,"props":937,"children":938},{"style":72},[939],{"type":24,"value":921},{"type":19,"tag":65,"props":941,"children":942},{"style":667},[943],{"type":24,"value":944}," ulong",{"type":19,"tag":65,"props":946,"children":947},{"style":78},[948],{"type":24,"value":949}," Count;\n",{"type":19,"tag":65,"props":951,"children":953},{"class":67,"line":952},5,[954,958,962,967,972,976,981,986,990],{"type":19,"tag":65,"props":955,"children":956},{"style":72},[957],{"type":24,"value":921},{"type":19,"tag":65,"props":959,"children":960},{"style":667},[961],{"type":24,"value":926},{"type":19,"tag":65,"props":963,"children":964},{"style":78},[965],{"type":24,"value":966}," Mean { ",{"type":19,"tag":65,"props":968,"children":969},{"style":667},[970],{"type":24,"value":971},"get",{"type":19,"tag":65,"props":973,"children":974},{"style":78},[975],{"type":24,"value":516},{"type":19,"tag":65,"props":977,"children":978},{"style":311},[979],{"type":24,"value":980},"return",{"type":19,"tag":65,"props":982,"children":983},{"style":78},[984],{"type":24,"value":985}," Value",{"type":19,"tag":65,"props":987,"children":988},{"style":72},[989],{"type":24,"value":96},{"type":19,"tag":65,"props":991,"children":992},{"style":78},[993],{"type":24,"value":994},"Count; } }\n",{"type":19,"tag":65,"props":996,"children":998},{"class":67,"line":997},6,[999],{"type":19,"tag":65,"props":1000,"children":1001},{"style":78},[1002],{"type":24,"value":1003},"    }\n",{"type":19,"tag":65,"props":1005,"children":1007},{"class":67,"line":1006},7,[1008],{"type":19,"tag":65,"props":1009,"children":1011},{"emptyLinePlaceholder":1010},true,[1012],{"type":24,"value":1013},"\n",{"type":19,"tag":65,"props":1015,"children":1017},{"class":67,"line":1016},8,[1018,1023,1027,1031,1036,1040,1045,1050,1055,1060,1065,1070],{"type":19,"tag":65,"props":1019,"children":1020},{"style":72},[1021],{"type":24,"value":1022},"    public",{"type":19,"tag":65,"props":1024,"children":1025},{"style":72},[1026],{"type":24,"value":874},{"type":19,"tag":65,"props":1028,"children":1029},{"style":667},[1030],{"type":24,"value":926},{"type":19,"tag":65,"props":1032,"children":1033},{"style":317},[1034],{"type":24,"value":1035}," Mean",{"type":19,"tag":65,"props":1037,"children":1038},{"style":78},[1039],{"type":24,"value":325},{"type":19,"tag":65,"props":1041,"children":1042},{"style":72},[1043],{"type":24,"value":1044},"this",{"type":19,"tag":65,"props":1046,"children":1047},{"style":906},[1048],{"type":24,"value":1049}," IEnumerable",{"type":19,"tag":65,"props":1051,"children":1052},{"style":78},[1053],{"type":24,"value":1054},"\u003C",{"type":19,"tag":65,"props":1056,"children":1057},{"style":667},[1058],{"type":24,"value":1059},"int",{"type":19,"tag":65,"props":1061,"children":1062},{"style":78},[1063],{"type":24,"value":1064},"> ",{"type":19,"tag":65,"props":1066,"children":1067},{"style":328},[1068],{"type":24,"value":1069},"sequence",{"type":19,"tag":65,"props":1071,"children":1072},{"style":78},[1073],{"type":24,"value":1074},") {\n",{"type":19,"tag":65,"props":1076,"children":1078},{"class":67,"line":1077},9,[1079,1084],{"type":19,"tag":65,"props":1080,"children":1081},{"style":311},[1082],{"type":24,"value":1083},"        return",{"type":19,"tag":65,"props":1085,"children":1086},{"style":78},[1087],{"type":24,"value":1088}," sequence\n",{"type":19,"tag":65,"props":1090,"children":1092},{"class":67,"line":1091},10,[1093,1098,1103,1107,1112,1117,1122,1126,1131,1135,1140,1144,1148],{"type":19,"tag":65,"props":1094,"children":1095},{"style":78},[1096],{"type":24,"value":1097},"            .",{"type":19,"tag":65,"props":1099,"children":1100},{"style":317},[1101],{"type":24,"value":1102},"Select",{"type":19,"tag":65,"props":1104,"children":1105},{"style":78},[1106],{"type":24,"value":325},{"type":19,"tag":65,"props":1108,"children":1109},{"style":328},[1110],{"type":24,"value":1111},"n",{"type":19,"tag":65,"props":1113,"children":1114},{"style":72},[1115],{"type":24,"value":1116}," =>",{"type":19,"tag":65,"props":1118,"children":1119},{"style":311},[1120],{"type":24,"value":1121}," new",{"type":19,"tag":65,"props":1123,"children":1124},{"style":906},[1125],{"type":24,"value":909},{"type":19,"tag":65,"props":1127,"children":1128},{"style":78},[1129],{"type":24,"value":1130}," {Value ",{"type":19,"tag":65,"props":1132,"children":1133},{"style":72},[1134],{"type":24,"value":86},{"type":19,"tag":65,"props":1136,"children":1137},{"style":78},[1138],{"type":24,"value":1139}," n, Count ",{"type":19,"tag":65,"props":1141,"children":1142},{"style":72},[1143],{"type":24,"value":86},{"type":19,"tag":65,"props":1145,"children":1146},{"style":167},[1147],{"type":24,"value":233},{"type":19,"tag":65,"props":1149,"children":1150},{"style":78},[1151],{"type":24,"value":1152},"})\n",{"type":19,"tag":65,"props":1154,"children":1156},{"class":67,"line":1155},11,[1157,1161,1165,1170,1175,1179,1184,1188],{"type":19,"tag":65,"props":1158,"children":1159},{"style":78},[1160],{"type":24,"value":1097},{"type":19,"tag":65,"props":1162,"children":1163},{"style":317},[1164],{"type":24,"value":688},{"type":19,"tag":65,"props":1166,"children":1167},{"style":78},[1168],{"type":24,"value":1169}," ((",{"type":19,"tag":65,"props":1171,"children":1172},{"style":328},[1173],{"type":24,"value":1174},"agg",{"type":19,"tag":65,"props":1176,"children":1177},{"style":78},[1178],{"type":24,"value":175},{"type":19,"tag":65,"props":1180,"children":1181},{"style":328},[1182],{"type":24,"value":1183},"valueAndCount",{"type":19,"tag":65,"props":1185,"children":1186},{"style":78},[1187],{"type":24,"value":736},{"type":19,"tag":65,"props":1189,"children":1190},{"style":72},[1191],{"type":24,"value":1192},"=>\n",{"type":19,"tag":65,"props":1194,"children":1196},{"class":67,"line":1195},12,[1197,1202,1206,1210,1214,1219,1224,1228,1233,1237,1242,1246,1250,1255,1259,1263],{"type":19,"tag":65,"props":1198,"children":1199},{"style":311},[1200],{"type":24,"value":1201},"            new",{"type":19,"tag":65,"props":1203,"children":1204},{"style":906},[1205],{"type":24,"value":909},{"type":19,"tag":65,"props":1207,"children":1208},{"style":78},[1209],{"type":24,"value":1130},{"type":19,"tag":65,"props":1211,"children":1212},{"style":72},[1213],{"type":24,"value":86},{"type":19,"tag":65,"props":1215,"children":1216},{"style":78},[1217],{"type":24,"value":1218}," agg.",{"type":19,"tag":65,"props":1220,"children":1221},{"style":757},[1222],{"type":24,"value":1223},"Value",{"type":19,"tag":65,"props":1225,"children":1226},{"style":72},[1227],{"type":24,"value":765},{"type":19,"tag":65,"props":1229,"children":1230},{"style":78},[1231],{"type":24,"value":1232}," valueAndCount.",{"type":19,"tag":65,"props":1234,"children":1235},{"style":757},[1236],{"type":24,"value":1223},{"type":19,"tag":65,"props":1238,"children":1239},{"style":78},[1240],{"type":24,"value":1241},", Count ",{"type":19,"tag":65,"props":1243,"children":1244},{"style":72},[1245],{"type":24,"value":86},{"type":19,"tag":65,"props":1247,"children":1248},{"style":78},[1249],{"type":24,"value":1218},{"type":19,"tag":65,"props":1251,"children":1252},{"style":757},[1253],{"type":24,"value":1254},"Count",{"type":19,"tag":65,"props":1256,"children":1257},{"style":72},[1258],{"type":24,"value":765},{"type":19,"tag":65,"props":1260,"children":1261},{"style":167},[1262],{"type":24,"value":233},{"type":19,"tag":65,"props":1264,"children":1265},{"style":78},[1266],{"type":24,"value":1267},"} )\n",{"type":19,"tag":65,"props":1269,"children":1271},{"class":67,"line":1270},13,[1272,1276,1281],{"type":19,"tag":65,"props":1273,"children":1274},{"style":78},[1275],{"type":24,"value":1097},{"type":19,"tag":65,"props":1277,"children":1278},{"style":317},[1279],{"type":24,"value":1280},"Mean",{"type":19,"tag":65,"props":1282,"children":1283},{"style":78},[1284],{"type":24,"value":1285},"();\n",{"type":19,"tag":65,"props":1287,"children":1289},{"class":67,"line":1288},14,[1290],{"type":19,"tag":65,"props":1291,"children":1292},{"style":78},[1293],{"type":24,"value":1003},{"type":19,"tag":65,"props":1295,"children":1297},{"class":67,"line":1296},15,[1298],{"type":19,"tag":65,"props":1299,"children":1300},{"style":78},[1301],{"type":24,"value":1302},"}\n",{"type":19,"tag":601,"props":1304,"children":1305},{},[1306],{"type":24,"value":605},{"title":58,"searchDepth":607,"depth":607,"links":1308},[],"content:comments:sequence-averages-in-scala:123660.md","comments/sequence-averages-in-scala/123660.md","comments/sequence-averages-in-scala/123660",{"_path":1313,"_dir":638,"_draft":6,"_partial":6,"_locale":7,"title":1314,"description":1315,"id":1316,"name":1317,"email":1318,"avatar":1319,"date":1320,"body":1321,"_type":613,"_id":1441,"_source":615,"_file":1442,"_stem":1443,"_extension":618},"/comments/sequence-averages-in-scala/123623","123623","Welcome to Scala. You can use a case statement to deconstruct the tuple and avoid the ugly _1 and _2.",123623,"Harold","hanyikal@hotmail.com","https://www.gravatar.com/avatar/94f6e7c752a150dd1ba4de4c6dbb6e55?r=pg&d=retro","2014-12-12T00:58:19",{"type":16,"children":1322,"toc":1439},[1323,1344,1435],{"type":19,"tag":20,"props":1324,"children":1325},{},[1326,1328,1334,1336,1342],{"type":24,"value":1327},"Welcome to Scala. You can use a case statement to deconstruct the tuple and avoid the ugly ",{"type":19,"tag":61,"props":1329,"children":1331},{"className":1330},[],[1332],{"type":24,"value":1333},"_1",{"type":24,"value":1335}," and ",{"type":19,"tag":61,"props":1337,"children":1339},{"className":1338},[],[1340],{"type":24,"value":1341},"_2",{"type":24,"value":1343},".",{"type":19,"tag":53,"props":1345,"children":1347},{"className":55,"code":1346,"language":57,"meta":58,"style":58},"seq.foldLeft((0.0, 1)) { case ((avg, idx), next) => (avg + (next - avg)/idx, idx + 1) }._1\n",[1348],{"type":19,"tag":61,"props":1349,"children":1350},{"__ignoreMap":58},[1351],{"type":19,"tag":65,"props":1352,"children":1353},{"class":67,"line":68},[1354,1359,1363,1367,1371,1376,1381,1386,1390,1395,1399,1404,1408,1413,1417,1422,1426,1430],{"type":19,"tag":65,"props":1355,"children":1356},{"style":78},[1357],{"type":24,"value":1358},"seq.foldLeft((",{"type":19,"tag":65,"props":1360,"children":1361},{"style":167},[1362],{"type":24,"value":170},{"type":19,"tag":65,"props":1364,"children":1365},{"style":78},[1366],{"type":24,"value":175},{"type":19,"tag":65,"props":1368,"children":1369},{"style":167},[1370],{"type":24,"value":180},{"type":19,"tag":65,"props":1372,"children":1373},{"style":78},[1374],{"type":24,"value":1375},")) { ",{"type":19,"tag":65,"props":1377,"children":1378},{"style":311},[1379],{"type":24,"value":1380},"case",{"type":19,"tag":65,"props":1382,"children":1383},{"style":78},[1384],{"type":24,"value":1385}," ((avg, idx), next) ",{"type":19,"tag":65,"props":1387,"children":1388},{"style":72},[1389],{"type":24,"value":190},{"type":19,"tag":65,"props":1391,"children":1392},{"style":78},[1393],{"type":24,"value":1394}," (avg ",{"type":19,"tag":65,"props":1396,"children":1397},{"style":72},[1398],{"type":24,"value":200},{"type":19,"tag":65,"props":1400,"children":1401},{"style":78},[1402],{"type":24,"value":1403}," (next ",{"type":19,"tag":65,"props":1405,"children":1406},{"style":72},[1407],{"type":24,"value":210},{"type":19,"tag":65,"props":1409,"children":1410},{"style":78},[1411],{"type":24,"value":1412}," avg)",{"type":19,"tag":65,"props":1414,"children":1415},{"style":72},[1416],{"type":24,"value":96},{"type":19,"tag":65,"props":1418,"children":1419},{"style":78},[1420],{"type":24,"value":1421},"idx, idx ",{"type":19,"tag":65,"props":1423,"children":1424},{"style":72},[1425],{"type":24,"value":200},{"type":19,"tag":65,"props":1427,"children":1428},{"style":167},[1429],{"type":24,"value":233},{"type":19,"tag":65,"props":1431,"children":1432},{"style":78},[1433],{"type":24,"value":1434},") }._1\n",{"type":19,"tag":601,"props":1436,"children":1437},{},[1438],{"type":24,"value":605},{"title":58,"searchDepth":607,"depth":607,"links":1440},[],"content:comments:sequence-averages-in-scala:123623.md","comments/sequence-averages-in-scala/123623.md","comments/sequence-averages-in-scala/123623",1779264582591]