Filter() drop elements of a tuple with singleton doesn't work

I want to remove some models in tuple “dropm”, from a list of models “mList”.
This fails when the tuple “dropm” is a singleton. It only works if “dropm” is a Vector{string}.
I want it to work if “dropm” is a tuple.

mList =[“m1”, “m2”, “m3”, “m4”]

dropm = (“m2”, “m1”)
mListNew = filter(m → m ∉ dropm, mList)

dropm = (“m2”)
mListNew = filter(m → m ∉ dropm, mList) error

dropm = [“m2”, “m1”]
mListNew = filter(m → m ∉ dropm, mList)

dropm = [“m2”]
mListNew = filter(m → m ∉ dropm, mList)

mList =["m1", "m2", "m3", "m4"]

dropm = ("m2", "m1")
mListNew = filter(m -> m ∉ dropm, mList)

dropm = ("m2")
mListNew = filter(m -> m ∉ dropm, mList) #ERROR

dropm = ["m2", "m1"]
mListNew = filter(m -> m ∉ dropm, mList)

dropm = ["m2"]
mListNew = filter(m -> m ∉ dropm, mList)

I don’t have a solution, only some better formation :slight_smile:

1 Like

dropm = (“m2”) is not creating a tuple, the brackets don’t do anything here, so dropm is just the string "m2". You are probably looking for dropm = (“m2”,). The comma is necessary here, since otherwise this can’t be distinguished from brackets only used for precedence like in (1 + 2) * 3, for example.

1 Like

Thank you @simeonschaub!
Then there is a slight generality annoyance.
To make a vector of strings, the syntax is identical whether it is a singleton or not:

dropm = ["m2", "m1"]  #= VS =#  dropm = ["m2"]

But to make a tuple, the syntax is different when the tuple is a singleton:

dropm = ("m2", "m1")  #= VS =#  dropm =("m2",)

Not ideal. Already caused my code to break.

I understand the frustration, but there’s not really anything that can be done here. Like I explained above, in a case like (1 + 2) * 3 it would otherwise be ambiguous, whether you mean a tuple containing only 3 or simply the integer 3 times 3, since brackets can also be used just to establish mathematical precedence, e.g. which function should be evaluated first.

For math using parentheses totally makes sense. It’s the natural thing (PEMDAS).
But for tuples w/ strings? meh. Thanks for your help though.

I think that would be much more confusing than the current behavior. The way syntax is parsed shouldn’t really depend on any type information. Otherwise it would be really hard to explain to people, why ("a") is completely different to (2). You can also just as well use infix operators with strings, so in expressions like ("a" * "b")^3, the brackets still have an important purpose.

1 Like

Thanks, I’m new to Julia & never knew about those string operations.

1 Like