Compute variance via functional programming So I'm supposed to calculate variance based on this forumula through functional programming:
$$\sigma^2 = \frac{\sum\limits_{i=0}^{n-1} (X -\mu)^2}{n-1}$$
This is the code that works:
public static double variance1(int[] array)
{
     double mean=mean(array);
     double summation=DoubleStream.iterate(0,idx-&gt;idx+1)
                                  .limit(array.length)
                                  .reduce(0,(summation_counter,idx)-&gt;
                                   {
                                     return Math.pow(array[(int)idx]-mean, 2)+summation_counter;
                                   });
                                  
                               
     return summation/(array.length-1);
}

Although the code works, it's ugly with the typecasts and seems forced.
Initially, I thought of doing something like
double summation=IntStream.rangeClosed(0,array.length-1)
                          .reduce(0, (summation_counter,idx)-&gt; ....

but I couldn't use an IntStream as the body of the reduce method returns double.
First off, watch your formating. Java standard is to have spaces around operators, and a space after commas. In the reduce lambda you can leave out the return and the braces, just like you did't for iterate.
It's a bit ugly to use a DoubleStream for the array indices, and number streams provide a sum() method, so using reduce is unnecessary.
An array can easily streamed using Arrays.stream(), which produces an IntStream and  with mapToDouble a DoubleStream can be created:
double summation = Arrays.stream(array)
                     .mapToDouble(x -&gt; (x - mean) * (x - mean))
                     .sum();