Consider a data list data
, and a rule list rule
. I want to select from data
all elements that are in rule
. For example, with
data = {a, a + 1, b, b + 2, c, c + 3, a + d, a + c, b + c};
rule = {a, d};
I want to select from data
all elements that contains a
or d
. My solution is to invert the result from FreeQ
, go through the rule
list, and the pick out the results, like this:
HasQL[expr_, lst_] :=
AnyTrue[Table[Not[FreeQ[expr, lst[[i]]]], {i, 1, Length[lst]}],
TrueQ]
and then Select[data, HasQL[#, rule] &]
returns the expected result:
{a, 1 + a, a + d, a + c}
I tried cleaning up the code above:
HasQL[d_, r_] := AnyTrue[(!FreeQ[d, #]) & /@ r, TrueQ]
Select[data, HasQL[#, rule] &]
Is there a more elegant/clean way to achieve the same result?
Select[data, ! FreeQ[#, Alternatives @@ rule] &]
? $\endgroup$