Shorthand for operations?

Mathematica has a large number of built-in symbols, and I am unsure when there is a shorthand for the operation I am trying to perform.

Question 1:

Is there a shorthand for the function Join[#,#] &? Here I only need the operation to apply to lists.

Question 2:

Is there a shorthand for a function which swaps out one operation with another:

f[bool_, a_, b_] := If[bool, a + b, a – b]

=================

1

 

I highly recommend waiting 24 hours before Accepting an answer, to give people all around the world time to respond. You never know what answers you might get. 🙂
– Mr.Wizard♦
Feb 2 ’15 at 7:19

  

 

Point taken. Unaccepted for now.
– pre-kidney
Feb 2 ’15 at 7:19

  

 

I guess no one else feels like answering.
– Mr.Wizard♦
Feb 5 ’15 at 8:16

=================

1 Answer
1

=================

I can’t think of a way to improve Join[#, #] &. Merely as a game you could use:

{##, ##} & @@ {1, 2, 3}

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

However this is inadvisable because it will unpack packed arrays and it loses the original Head.

For the second case if the arguments are long, rather than just a and b, it can be shorter to use:

If[bool, Plus, Subtract][a, b]

Somewhat related: List manipulation to build a functional expression.

You could also use two separate definitions:

f[True, a_, b_] := a + b
f[False, a_, b_] := a – b

Neither is an improvement in your example but each has its place I believe.

  

 

Thanks, the If[bool, Plus, Subtract] was what I was looking for. I am embarrassed to admit that I had tried If[bool, +, -] before giving up.
– pre-kidney
Feb 2 ’15 at 7:19

  

 

@pre-kidney You’re welcome. We all have mental blocks from time to time; I think I have more than my share of them.
– Mr.Wizard♦
Feb 2 ’15 at 7:20