Skip to main content

functions - Opposite of Alternatives


I was looking at this question (here) and I tried to find any thing gives the opposite of Alternatives. In other words, some pattern matching function that gives true when expression matches ALL patterns. Is there any function for that?


For example:



x has to be Integer AND Real


The other thing is that from Mr.Wizard solution, suppose I want to make a rule in which x has to be Integer and Real and also x>10, how can I define that in external rule or pattern in one shot and use pattern test ? or pattern : ( for example to do something like this f[x_?match] or f[x:(pattern)])


I hope I made the question clear.


Thank you



Answer



To directly get the opposite of Alternatives, you could negate each pattern with Except and then negate the Alternatives:


also[patts__] := Except[Alternatives @@ Except /@ {patts}]

Cases[Range[1, 15, 1/2], _Integer ~also~ _?(# > 10 &)]


(* {11, 12, 13, 14, 15} *)

Generally some other approach will be simpler, though, as discussed in the comments.


Comments