# Is it possible to combine the following pattern?

I want to simplify the following pattern

Cases[Permutations[Range@3], {___, 2, 3, ___} | {___, 3, 2, ___}]


into something like (but it does not compile).

Cases[Permutations[Range@3], {___, (2, 3) | (3, 2), ___}]


Please let me know if it can be simplified.

• Cases[Permutations[Range@3], {___, 2 | 3, 3 | 2, ___}] Commented Jul 19, 2022 at 16:12

This updated example has an easy fix with OrderlessPatternSequence.

Permutations[Range@3]
Cases[%, {___, OrderlessPatternSequence[2, 3], ___}]


{{1, 2, 3}, {1, 3, 2}, {2, 1, 3}, {2, 3, 1}, {3, 1, 2}, {3, 2, 1}}

{{1, 2, 3}, {1, 3, 2}, {2, 3, 1}, {3, 2, 1}}

Side note: That being said. I was working on this before the edit and I noticed interesting, seemingly wrong, behavior.

Basically, naming a pattern effects matching. For example

MatchQ[{1, 4, 3, 2}, {l___, seq : OrderlessPatternSequence[2, 3], r___}]


returns False where as

MatchQ[{1, 4, 3, 2}, {l___, OrderlessPatternSequence[2, 3], r___}]


returns True. It's also interesting and should be noted that

MatchQ[{1, 4, 2, 3}, seq : {l___, OrderlessPatternSequence[3, 2], r___}];
MatchQ[{1, 4, 2, 3}, {l___, OrderlessPatternSequence[3, 2], r___}];
%% === % === True


True

There's also this confirmed bug to a similar situation. This might be better actually asked a separate question, but I just wanted to see if there was something obvious I'm missing.

• You should report this misbehavior to "OrderlessPatternSequence" to Wolfram Commented Feb 9, 2021 at 11:39
• @DanielHuber Thanks, will do. Commented Feb 9, 2021 at 21:27