11

I would prefer to write {_, foo} = LongFunctionName[arg1, arg2] instead of foo = LongFunctionName[arg1, arg2][[2]] or foo = Last @ LongFunctionName[arg1, arg2]. Using an actual name for the throwaway variable makes code unnecessarily verbose and can be misleading/confusing. Is it a good idea to use $ for throwaways?

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
ZeitPolizei
  • 533
  • 3
  • 9
  • 3
    I don't believe there is a convention. In fact I don't believe this is a common method at all, in Mathematica. This is closely related to (30138) which is probably the one @Kuba was thinking of. – Mr.Wizard Mar 09 '17 at 11:36
  • 2
    +1, I would like to see a "throwaway variable" in Mathematica. MATLAB also has it, it uses the tilde. – C. E. Mar 09 '17 at 12:11
  • @ZeitPolizei I would like to know if you would object to my closing this question as a duplicate of the one linked above. I feel that this is probably best addressed in a single place, and if there are aspects of your question that are not addressed there it may be better to extend that question rather than creating a separate one. If this action is taken your question will remain as an entry point for searches. – Mr.Wizard Mar 09 '17 at 23:28

2 Answers2

5

Actually you can write {_, foo} = . . . and it works, as noted in How to ignore list elements when extracting with pattern matching.

If one is going to make use of this it may be desirable to turn Off Set::nosym.

Off[Set::nosym]

{a, _, b} = {1, 2, 3};

{a, b}
{1, 3}

Perhaps this is as close to a convention as we can find?

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
4

This one should not need to suppress messages:

Block[{a}, {a, b, a} = {1, 2, 3}];
a
b
(* a *) 
(* 2 *) 
LLlAMnYP
  • 11,486
  • 26
  • 65
  • 1
    While Block is faster I'd probably go with Module because of reasons: http://mathematica.stackexchange.com/a/25934/5478 – Kuba Mar 10 '17 at 08:41