Example usage for java.util.stream LongStream iterate

List of usage examples for java.util.stream LongStream iterate

Introduction

In this page you can find the example usage for java.util.stream LongStream iterate.

Prototype

public static LongStream iterate(final long seed, final LongUnaryOperator f) 

Source Link

Document

Returns an infinite sequential ordered LongStream produced by iterative application of a function f to an initial element seed , producing a Stream consisting of seed , f(seed) , f(f(seed)) , etc.

Usage

From source file:Main.java

public static void main(String[] args) {
    LongStream b = LongStream.iterate(1, (n) -> {
        return n + 1;
    });// www. j  a  v  a2s .  com

    b.limit(10).forEach(System.out::println);
}

From source file:Main.java

/**
 * Applies the Collatz conjecture on <code>value</code>, and count the steps to reach 1.
 * <p>/*from  w ww . j av a  2s  .c  o  m*/
 * Examples:
 * <ul>
 * <li>1: 1 &#8594; 4 &#8594; 2 &#8594; 1. Therefore, the return value is 3.</li>
 * <li>2: 2 &#8594; 1. Therefore, the return value is 1.</li>
 * </ul>
 * <p>
 * This implementation works by using a {@link Stream#anyMatch(java.util.function.Predicate)}
 * short-circuiting terminal operation, so although the right answer is returned at the end, it
 * does alter the definition of a stream of Collatz sequence numbers.
 *
 * @param value the value to start from.
 * @return the number of steps to end at 1.
 * @throws IllegalArgumentException if <code>value</code> is 0 or less.
 */
public static long stepsFor(long value) {
    if (value <= 0) {
        throw new IllegalArgumentException("Input must be greater than 0.");
    }
    final long[] result = new long[1];
    LongStream.iterate(value, v -> ++result[0] > 0 && v % 2 == 0 ? v / 2 : 3 * v + 1)
            .anyMatch(v -> v == 1 && result[0] > v);
    return --result[0];
}

From source file:beast.math.NumberFormatter.java

public void setSignificantFigures(int sf) {
    this.sf = sf;

    upperCutoff = ArithmeticUtils.pow(10, sf - 1);
    cutoffTable = LongStream.iterate(1, l -> 10 * l).limit(sf).asDoubleStream().toArray();

    decimalFormat.setMinimumIntegerDigits(1);
    decimalFormat.setMaximumFractionDigits(sf - 1);
    decimalFormat.setMinimumFractionDigits(sf - 1);

    scientificFormat = getScientificFormat();

    fieldWidth = sf;//w w  w. ja  va  2  s .  c  om
}