Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;

import java.util.Collections;
import java.util.List;

import java.util.function.Function;

public class Main {
    /**
     * As long as the predicate is happy, grind and append results to result list
     *
     * @param seed - initial input to predicate (and to grinder)
     * @param grinder - grinder(seed) gets appended to result
     * @param predicate - predicate(seed) tells whether to keep grinding
     * @return result list
     */
    public static <A> List<A> unfold(A seed, Function<A, A> grinder, Function<A, Boolean> predicate) {
        List<A> result = Collections.EMPTY_LIST;
        A counter = seed;
        while (predicate.apply(counter)) {
            result = append(counter, result);
            counter = grinder.apply(counter);
        }
        return Collections.unmodifiableList(result);
    }

    public static <A> List<A> append(A a, List<A> as) {
        List<A> result = new ArrayList<A>(as);
        result.add(a);
        return Collections.unmodifiableList(result);
    }
}