/** * AND of two or more filters. * * @param <U> type of the users * @param filters a number of item filters * @return an item filter which does a logical AND to two or more filters */ @SuppressWarnings("unchecked") public static <U> Function<U, IntPredicate> and(Function<U, IntPredicate>... filters) { return user -> { IntPredicate andPredicate = iidx -> true; for (Function<U, IntPredicate> filter : filters) { andPredicate = andPredicate.and(filter.apply(user)); } return andPredicate; }; } }
package com.company; import rx.Observable; import java.util.function.IntPredicate; import java.util.stream.IntStream; public class Main { public static void main(String[] args) { final IntPredicate[] p={(x)->true}; IntStream primesStream=IntStream.iterate(2,n->n+1).filter(i -> p[0].test(i)).peek(i->p[0]=p[0].and(v->v%i!=0) ); Observable primes = Observable.from(()->primesStream.iterator()); primes.take(10).forEach((x) -> System.out.println(x.toString())); } }
public static StreamEx<Integer> sieve(StreamEx<Integer> input, IntPredicate isPrime) { return input.headTail((head, tail) -> isPrime.test(head) ? sieve(tail, isPrime.and(n -> n % head != 0)).prepend(head) : sieve(tail, isPrime)); } sieve(StreamEx.iterate(2, x -> x+1), i -> true).limit(1000).forEach(System.out::println);
@Override public IntPredicate getMatcher(NodeVectorTree tree) { switch (operator) { case Token.UNION: return nodetest1.getMatcher(tree).or(nodetest2.getMatcher(tree)); case Token.INTERSECT: return nodetest1.getMatcher(tree).and(nodetest2.getMatcher(tree)); case Token.EXCEPT: return new IntExceptPredicate(nodetest1.getMatcher(tree), nodetest2.getMatcher(tree)); default: throw new IllegalArgumentException("Unknown operator in Combined Node Test"); } }
@Override public IntPredicate getMatcher(NodeVectorTree tree) { switch (operator) { case Token.UNION: return nodetest1.getMatcher(tree).or(nodetest2.getMatcher(tree)); case Token.INTERSECT: return nodetest1.getMatcher(tree).and(nodetest2.getMatcher(tree)); case Token.EXCEPT: return new IntExceptPredicate(nodetest1.getMatcher(tree), nodetest2.getMatcher(tree)); default: throw new IllegalArgumentException("Unknown operator in Combined Node Test"); } }