Linked Questions
13 questions linked to/from Alternatives pattern in a function definition
603
votes
19
answers
157k
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 ...
45
votes
4
answers
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 ...
20
votes
1
answer
329
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...
17
votes
2
answers
698
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 ...
14
votes
9
answers
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 ...
12
votes
9
answers
571
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 ...
10
votes
4
answers
2k
views
Function argument to default under certain condition
Inspired by this and this question (and how I handle this in practice), what is the best way to default a function value when a certain condition is met?
For example, if a function is defined as:
<...
9
votes
5
answers
477
views
How to distinguish between lists and values?
I have a (hopefully small) problem with some numerical integration algorithm, more specifically I want to integrate the imaginary part of a complex valued function, e.g. ...
9
votes
3
answers
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 ...
9
votes
3
answers
773
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, ...
5
votes
2
answers
309
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 ...
4
votes
2
answers
412
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 ...
3
votes
1
answer
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 ...