13 questions linked to/from Alternatives pattern in a function definition
156k views

### Where can I find examples of good Mathematica programming practice?

I consider myself a pretty good Mathematica programmer, but I'm always looking out for ways to either improve my way of doing things in Mathematica, or to see if there's something nifty that I haven't ...
561 views

### List manipulation - adding last element of sublist to each sublist

If I have the following list: {{}, {1, 2}, {}, {2, 4, 3}, {5, 4, 3, 2}, {}} How can I add the last element of each sublist to each sublist? The results should ...
188 views

### Optional argument as a correction of a main argument

Let's consider a function f[a_,b___] I want that if I input only one argument I get f[a]=a but if I input 2 arguments I ...
395 views

### Generating all permutations of labels in an expression

I have some very long and complex expressions which involve a set of $n$ variables, and I want to be able to permute the labels of the variables. I will give a simple example, instead of my awful ...
760 views

### Generalization to AllTrue, AnyTrue and NoneTrue

I am wondering if there is a natural Mathematica way to generalize those functions. To be specific, All three functions AllTrue, ...
5k views

### Can a function be made to accept a variable amount of inputs?

I have a function that takes two inputs and processes them for a single output. What I need is one that can take a varying number of inputs. and process them to a single output. Is this possible in ...
2k views

### How can a power of 0 or 1 be replaced?

Consider this given example for ReplaceAll 1 + x^2 + x^4 /. x^p_ -> f[p] This returns ...
304 views

### Function pattern to support listed and non-listed arguments [duplicate]

I often want to write functions that take as an argument either a) a rule or b) a list of rules. As an example, when using Replace with just one rule, it does not ...
2k views

### Function with zero or one arguments

Is there a neat way to define a function with a single optional argument that defaults to an empty sequence? For example, suppose I want to define a wrapper for ...
323 views

### How to get Cases to wrap results in an arbitrary head?

A number of functions provide for results to be wrapped in an arbitrary head. This is very important in cases where the results should not be evaluated. Take Level...
686 views

### f[arg1, arg2,...,argN] vs. f[{arg1, arg2,...,argN}]

I am trying to reproduce the API of a function (written in R) that accepts an arbitrary number of arguments and handles it the same way as it would handle a single argument that is a list of different ...