MathIterators { public static ImmutableIterator<Double> square(Iterator<Double> t) { List<ImmutableIterator<Double>> s = tee(t, 2); return multiply(s.get(0), s.get(1)); } private  MathIterators(); static Comparator<Double> weakDoubleComparator(double epsilon); static UnaryOperator<Double> increment(Double delta); static UnaryOperator<Double> times(Double factor); static InfiniteIterator<Double> random(); static InfiniteIterator<Double> arithmeticSeries(double start, double delta); static InfiniteIterator<Double> geometricSeries(double start, double factor); static ImmutableIterator<Double> exp(); static ImmutableIterator<Double> sin(); static ImmutableIterator<Double> cos(); static ImmutableIterator<Double> add(Iterator<Double> s, double inc); @SuppressWarnings("unchecked") static Iterator<Double> inc(Iterator<Double> s, double inc); static ImmutableIterator<Double> add(Iterator<Double> s, Iterator<Double> t); static ImmutableIterator<Double> multiply(Iterator<Double> s, double factor); static ImmutableIterator<Double> multiply(Iterator<Double> s, Iterator<Double> t); static ImmutableIterator<Double> square(Iterator<Double> t); static InfiniteIterator<Double> inverse(Iterator<Double> t); static ImmutableIterator<Double> divide(Iterator<Double> s, Iterator<Double> t); static UnaryOperator<Double> polynom(Iterator<Double> t, int dim); static UnaryOperator<Double> polynom(Iterator<Double> t); static Iterator<Double> compose(Iterator<Double> t, Iterator<Double> s, int dim); static Iterator<Double> compose(Iterator<Double> t, Iterator<Double> s);  }
@Test public void testSquare() { Iterator<Double> s = repeat(1.0); Iterator<Double> r = square(s); BinaryOperator<Double> acc = (a, b) -> a + b; double sum = reduce(limit(r, N), acc); assertEquals(N * (N + 1) / 2, sum, delta); r = square(cos()); s = square(sin()); Iterator<Double> result = map(r, s, acc); Double one = result.next(); assertEquals(1.0, one, delta); sum = reduce(limit(result, 10), acc); assertEquals(0.0, sum, delta); }