@ SuppressWarnings ( "unchecked" ) private static < H > H _fold ( Iterable source , Operation [ ] ops , int opIdx , H ident , Fn2 reducer ) { Object ret = ident ; // This is a label - the first one I have used in Java in years, or maybe ever. // I'm assuming this is fast, but will have to test to confirm it. sourceLoop : for ( Object o : source ) { for ( int j = opIdx ; j < ops . length ; j ++ ) { Operation op = ops [ j ] ; if ( ( op . filter != null ) && ! op . filter . apply ( o ) ) { // stop processing this source item and go to the next one. continue sourceLoop ; } if ( op . map != null ) { o = op . map . apply ( o ) ; // This is how map can handle takeWhile, take, and other termination marker // roles. Remember, the fewer functions we have to check for, the faster this // will execute. if ( o == TERMINATE ) { return ( H ) ret ; } } else if ( op . flatMap != null ) { ret = _fold ( op . flatMap . apply ( o ) , ops , j + 1 , ( H ) ret , reducer ) ; // stop processing this source item and go to the next one. continue sourceLoop ; } // if ( (op.terminate != null) && op.terminate.apply(o) ) { // return (G) ret; // } } // Here, the item made it through all the operations. Combine it with the result. ret = reducer . apply ( ret , o ) ; } return ( H ) ret ; }
than lazily evaluated and cached linked - list Sequence model .