Creating a list without Null entries

If you want to generate a list using a If function, the False (or the True) value might create Nulls.

The solution is to use the vanishing function : ##&[]

testmatch[input_, letter_] := 
 If[StringMatchQ[input, StringJoin[letter, "*"]], input, ## &[]]

This is like a very simple grep command.

The idea comes from How to remove the Null symbol in a Table in Mathematica?

7 Likes

I always use `Unevaluated[Sequence[]]` inside If as my “vanishing function”. So techinically I just use Sequence[]. That seems straightforward :). But this is nothing new I see, it can be found by following some more links in the linked article above.

Things can be further sped up by giving If (or other functions, in particular Function) attribute SequenceHold temporarily, but I guess that is a hack.

1 Like