Is it possible to create in Java 8 a unlimitedly growing in lazy way collection, defined by recursion?

前端 未结 3 705
心在旅途
心在旅途 2021-01-05 03:14

I can create a recursive closure:

static IntUnaryOperator fibo;
fibo = 
    (i) -> 
    i<2 ? 1 : fibo.applyAsInt(i-1)+ fibo.applyAsInt(i-2);
         


        
3条回答
  •  半阙折子戏
    2021-01-05 03:33

    The solution will be created as a class FunctionalSequence for representation of a lazy, infinite sequence of objects, defined by a lambda function with integer argument. The function can be iterative or not. For the iterative case the FunctionalSequence class will have a method initialize for setting the start values.

    The declaration of an object of such class will look so:

        FunctionalSequence fiboSequence =  new FunctionalSequence<>();
        fiboSequence.
            initialize(Stream.of(BigInteger.ONE,BigInteger.ONE)).
            setSequenceFunction(
                (i) ->
                fiboSequence.get(i-2).add(fiboSequence.get(i-1))
            );
    

    Notice, as in the recursive lambda example in the question, we cannot declare the object and define it recursively in one operator. One operator for declaration, another for definition.

    The FunctionalSequence class definition:

    import java.util.Iterator;
    import java.util.LinkedList;
    import java.util.stream.Stream;
    
    public class FunctionalSequence implements Iterable{
        LinkedList> realList = new LinkedList<>();
        StackOverflowingFunction calculate = null;
        public FunctionalSequence initialize(Stream start){
            start.forEachOrdered((T value) ->
            {
                    realList.add(new CountedFlighweight<>());
                    realList.getLast().set(value);
            });
            return this;
        }
        public FunctionalSequence  setSequenceFunction(StackOverflowingFunction calculate){
            this.calculate = calculate;
            return this;
        }
    
        @Override
        public Iterator iterator() {
            return new SequenceIterator();
        }
        public T get(int currentIndex) throws StackOverflowError{
            if(currentIndex < 0) return null;
            while (currentIndex >= realList.size()){
                realList.add(new CountedFlighweight());
            }
            try {
                return (T) realList.get(currentIndex).get(calculate, currentIndex);
            } catch (Exception e) {
                return null;
            }
        }
        public class SequenceIterator implements Iterator{
            int currentIndex;
            @Override
            public boolean hasNext() {
                return true;
            }
    
            @Override
            public T next() {
                T result = null;
                if (currentIndex == realList.size()){
                    realList.add(new CountedFlighweight());
                }
                // here the StackOverflowError catching is a pure formality, by next() we would never cause StackOverflow
                try {
                    result = realList.get(currentIndex).get(calculate, currentIndex);
                } catch (StackOverflowError e) {
                }
                currentIndex++;
                return result;
            }
    
        }
        /**
         * if known is false, the value of reference is irrelevant
         * if known is true, and reference is not null, reference contains the data
         * if known is true, and reference is null, that means, that the appropriate data are corrupted in any way
         * calculation on corrupted data should result in corrupted data.
         * @author Pet
         *
         * @param 
         */
        public class CountedFlighweight{
            private boolean known = false;
            private U reference;
            /**
             * used for initial values setting 
             */
            private void set(U value){
                reference = value;
                known = true;
            }
            /**
             * used for data retrieval or function counting and data saving if necessary
             * @param calculate
             * @param index
             * @return
             * @throws Exception
             */
            public U get(StackOverflowingFunction calculate, int index) throws StackOverflowError{
                if (! known){
                    if(calculate == null) {
                        reference = null;
                    } else {
                        try {
                            reference = calculate.apply(index);
                        } catch (Exception e) {
                            reference = null;
                        }
                    }
                }
                known = true;
                return reference;
            }
        }
    
        @FunctionalInterface
        public interface StackOverflowingFunction  {
            public U apply(K index) throws StackOverflowError;
    
        }
    }
    

    As the recursive function could easily meet the StackOverflowError, we should organize the recursion so that in that case the whole recursive sequence will roll back without any changes really met and throw the exception.

    The use of the FunctionalSequence could look so:

        // by iterator:
        int index=0;
        Iterator iterator = fiboSequence.iterator(); 
        while(index++<10){
            System.out.println(iterator.next());
        }
    

    Or so:

    static private void tryFibo(FunctionalSequence fiboSequence, int i){
        long startTime = System.nanoTime();
        long endTime;
        try {
            fiboSequence.get(i);
            endTime = System.nanoTime();
            System.out.println("repeated timing for f("+i+")=" + (endTime-startTime)/1000000.+" ns");
        } catch (StackOverflowError e) {
            endTime = System.nanoTime();
            //e.printStackTrace();
            System.out.println("failed counting f("+i+"), time=" + (endTime-startTime)/1000000.+" ns");
        }       
    }
    

    The last function can be used in the following way:

        tryFibo(fiboSequence, 1100);
        tryFibo(fiboSequence, 100);
        tryFibo(fiboSequence, 100);
        tryFibo(fiboSequence, 200);
        tryFibo(fiboSequence, 1100);
        tryFibo(fiboSequence, 2100);
        tryFibo(fiboSequence, 2100);
        tryFibo(fiboSequence, 1100);
        tryFibo(fiboSequence, 100);
        tryFibo(fiboSequence, 100);
        tryFibo(fiboSequence, 200);
        tryFibo(fiboSequence, 1100);
    

    Here are the results (the stack was limited to 256K for the needs of testing):

    1
    1
    2
    3
    5
    8
    13
    21
    34
    55
    failed counting f(1100), time=3.555689 ns
    repeated timing for f(100)=0.213156 ns
    repeated timing for f(100)=0.002444 ns
    repeated timing for f(200)=0.266933 ns
    repeated timing for f(1100)=5.457956 ns
    repeated timing for f(2100)=3.016445 ns
    repeated timing for f(2100)=0.001467 ns
    repeated timing for f(1100)=0.005378 ns
    repeated timing for f(100)=0.002934 ns
    repeated timing for f(100)=0.002445 ns
    repeated timing for f(200)=0.002445 ns
    repeated timing for f(1100)=0.003911 ns
    

    Look, the repeatable call of the f(i) for the same index takes practically no time - no iterations were made. We cannot reach f(1100) at once because of the StackOverflowError. But after we have reached once f(200), f(1100) becomes reachable. We made it!

提交回复
热议问题