hg: lambda/lambda/jdk: Initial standard lambda functions and utils

David Holmes David.Holmes at oracle.com
Sun Aug 7 19:22:46 PDT 2011


Steven Simpson said the following on 08/07/11 18:56:
> This seems a little odd:
> 
>   456     /**
>   457      * Returns a predicate that evaluates to {@code true} if all or none of the
>   458      * component predicates evaluate to {@code true}. The components are
>   459      * evaluated in order, and evaluation will end if a predicate result
>   460      * fails to match the first predicate's result.
>   461      *
>   462      * @param<T>  the type of values evaluated by the predicates.
>   463      * @param first initial component predicate to be evaluated.
>   464      * @param second additional component predicate to be evaluated.
>   465      * @return  a predicate that evaluates to {@code true} if all or none of the
>   466      * component predicates evaluate to {@code true}
>   467      */
>   468     public static<T, P extends Predicate<? super T>>  Predicate<T>  xor(

Indeed that describes an exclusive-NOR function not an exclusive-OR.

>   469             Predicate<T>  first, P second) {
>   470         if((null != first)&&  (first == second)) {
>   471             return #{T t ->  false};
>   472         }
>   473
>   474         Objects.requireNonNull(first);
>   475         Objects.requireNonNull(second);
>   476
>   477         return #{T t ->  first.eval(t) ^ second.eval(t)};
>   478     }
> 
> I note that the documentation appears to have come from the N-ary case, 
> but has not yet been adapted for binary.  In either case, it is a 
> strange definition of XOR to me, and not consistent with the 
> implementation of the binary case above.
> 
> Logical XOR yields true if the operands are different - in contrast to 
> the docs.  I would expect N-ary XOR to yield true if the number of true 
> operands is odd, with no possibility of short-circuiting the result - in 
> contrast to the N-ary implementation.  Or is the code following some 
> other convention?

I would expect an N-ary XOR to yield true only if any one operand is true.

David Holmes


> Cheers!
> 


More information about the lambda-dev mailing list