Define function that behaves almost identically to Mathematica function
$begingroup$
Often I like to define my own functions that are almost exactly the same as Mathematica defined functions, apart from a few tweaks. See this question for example. I want to define them properly so they handle optional arguments correctly. What is a general strategy for accomplishing this? Here's a concrete (esoteric) example. I define myListPlot
that is almost identical to ListPlot
except that is adds a gridline corresponding to the first data point.
data = Table[RandomReal, {x, 1, 10}]
myListPlot[data_, opts_] := ListPlot[data, GridLines -> {None, {data[[1]]}}, opts]
myListPlot[data, {PlotStyle -> Red, Joined -> True}]
Not too bad. However I have to pass the optional arguments as a list. Instead, I would like to pass the optional arguments in the same way one does with ListPlot
. In other words, I would like to modify myListPlot
so that I would pass arguments like
myListPlot[data, PlotStyle -> Red, Joined -> True]
Perhaps I'm going about this completely the wrong way. Nevertheless I hope the reader understands what I'm trying to accomplish and can suggest a solution.
functions optional-arguments arguments
$endgroup$
add a comment |
$begingroup$
Often I like to define my own functions that are almost exactly the same as Mathematica defined functions, apart from a few tweaks. See this question for example. I want to define them properly so they handle optional arguments correctly. What is a general strategy for accomplishing this? Here's a concrete (esoteric) example. I define myListPlot
that is almost identical to ListPlot
except that is adds a gridline corresponding to the first data point.
data = Table[RandomReal, {x, 1, 10}]
myListPlot[data_, opts_] := ListPlot[data, GridLines -> {None, {data[[1]]}}, opts]
myListPlot[data, {PlotStyle -> Red, Joined -> True}]
Not too bad. However I have to pass the optional arguments as a list. Instead, I would like to pass the optional arguments in the same way one does with ListPlot
. In other words, I would like to modify myListPlot
so that I would pass arguments like
myListPlot[data, PlotStyle -> Red, Joined -> True]
Perhaps I'm going about this completely the wrong way. Nevertheless I hope the reader understands what I'm trying to accomplish and can suggest a solution.
functions optional-arguments arguments
$endgroup$
$begingroup$
Try changingmyListPlot[data_, opts_]
tomyListPlot[data_, opts___]
.
$endgroup$
– rafalc
yesterday
$begingroup$
Works like a charm. Why not go ahead and make it an answer.
$endgroup$
– Tom
yesterday
add a comment |
$begingroup$
Often I like to define my own functions that are almost exactly the same as Mathematica defined functions, apart from a few tweaks. See this question for example. I want to define them properly so they handle optional arguments correctly. What is a general strategy for accomplishing this? Here's a concrete (esoteric) example. I define myListPlot
that is almost identical to ListPlot
except that is adds a gridline corresponding to the first data point.
data = Table[RandomReal, {x, 1, 10}]
myListPlot[data_, opts_] := ListPlot[data, GridLines -> {None, {data[[1]]}}, opts]
myListPlot[data, {PlotStyle -> Red, Joined -> True}]
Not too bad. However I have to pass the optional arguments as a list. Instead, I would like to pass the optional arguments in the same way one does with ListPlot
. In other words, I would like to modify myListPlot
so that I would pass arguments like
myListPlot[data, PlotStyle -> Red, Joined -> True]
Perhaps I'm going about this completely the wrong way. Nevertheless I hope the reader understands what I'm trying to accomplish and can suggest a solution.
functions optional-arguments arguments
$endgroup$
Often I like to define my own functions that are almost exactly the same as Mathematica defined functions, apart from a few tweaks. See this question for example. I want to define them properly so they handle optional arguments correctly. What is a general strategy for accomplishing this? Here's a concrete (esoteric) example. I define myListPlot
that is almost identical to ListPlot
except that is adds a gridline corresponding to the first data point.
data = Table[RandomReal, {x, 1, 10}]
myListPlot[data_, opts_] := ListPlot[data, GridLines -> {None, {data[[1]]}}, opts]
myListPlot[data, {PlotStyle -> Red, Joined -> True}]
Not too bad. However I have to pass the optional arguments as a list. Instead, I would like to pass the optional arguments in the same way one does with ListPlot
. In other words, I would like to modify myListPlot
so that I would pass arguments like
myListPlot[data, PlotStyle -> Red, Joined -> True]
Perhaps I'm going about this completely the wrong way. Nevertheless I hope the reader understands what I'm trying to accomplish and can suggest a solution.
functions optional-arguments arguments
functions optional-arguments arguments
asked yesterday
TomTom
1,293919
1,293919
$begingroup$
Try changingmyListPlot[data_, opts_]
tomyListPlot[data_, opts___]
.
$endgroup$
– rafalc
yesterday
$begingroup$
Works like a charm. Why not go ahead and make it an answer.
$endgroup$
– Tom
yesterday
add a comment |
$begingroup$
Try changingmyListPlot[data_, opts_]
tomyListPlot[data_, opts___]
.
$endgroup$
– rafalc
yesterday
$begingroup$
Works like a charm. Why not go ahead and make it an answer.
$endgroup$
– Tom
yesterday
$begingroup$
Try changing
myListPlot[data_, opts_]
to myListPlot[data_, opts___]
.$endgroup$
– rafalc
yesterday
$begingroup$
Try changing
myListPlot[data_, opts_]
to myListPlot[data_, opts___]
.$endgroup$
– rafalc
yesterday
$begingroup$
Works like a charm. Why not go ahead and make it an answer.
$endgroup$
– Tom
yesterday
$begingroup$
Works like a charm. Why not go ahead and make it an answer.
$endgroup$
– Tom
yesterday
add a comment |
2 Answers
2
active
oldest
votes
$begingroup$
If you want to constrain it to only options from ListPlot
, you could use OptionsPattern
in combination with FilterRules
and Options
.
myListPlot[data_, opts : OptionsPattern] :=
ListPlot[data, GridLines -> {None, {data[[1]]}},
FilterRules[{opts}, Options[ListPlot]]]
which results in:
myListPlot[data, PlotStyle -> Red, Joined -> True]
$endgroup$
$begingroup$
OptionsPattern[ListPlot]
is more precise.
$endgroup$
– Edmund
yesterday
add a comment |
$begingroup$
The usual way to define a Wolfram Language function that takes n
arguments and an arbitrary number of options is like this:
f[arg1_, ..., argn_, opts___] := ...
A little bit of pattern matching background (taken from the WL reference):
_
any single expression
x_
any single expression, to be namedx
__
any sequence of one or more expressions
x__
sequence namedx
x__h
sequence of expressions, all of whose heads areh
___
any sequence of zero or more expressions
x___
sequence of zero or more expressions namedx
x___h
sequence of zero or more expressions, all of whose heads areh
$endgroup$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
});
});
}, "mathjax-editing");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "387"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f191884%2fdefine-function-that-behaves-almost-identically-to-mathematica-function%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
If you want to constrain it to only options from ListPlot
, you could use OptionsPattern
in combination with FilterRules
and Options
.
myListPlot[data_, opts : OptionsPattern] :=
ListPlot[data, GridLines -> {None, {data[[1]]}},
FilterRules[{opts}, Options[ListPlot]]]
which results in:
myListPlot[data, PlotStyle -> Red, Joined -> True]
$endgroup$
$begingroup$
OptionsPattern[ListPlot]
is more precise.
$endgroup$
– Edmund
yesterday
add a comment |
$begingroup$
If you want to constrain it to only options from ListPlot
, you could use OptionsPattern
in combination with FilterRules
and Options
.
myListPlot[data_, opts : OptionsPattern] :=
ListPlot[data, GridLines -> {None, {data[[1]]}},
FilterRules[{opts}, Options[ListPlot]]]
which results in:
myListPlot[data, PlotStyle -> Red, Joined -> True]
$endgroup$
$begingroup$
OptionsPattern[ListPlot]
is more precise.
$endgroup$
– Edmund
yesterday
add a comment |
$begingroup$
If you want to constrain it to only options from ListPlot
, you could use OptionsPattern
in combination with FilterRules
and Options
.
myListPlot[data_, opts : OptionsPattern] :=
ListPlot[data, GridLines -> {None, {data[[1]]}},
FilterRules[{opts}, Options[ListPlot]]]
which results in:
myListPlot[data, PlotStyle -> Red, Joined -> True]
$endgroup$
If you want to constrain it to only options from ListPlot
, you could use OptionsPattern
in combination with FilterRules
and Options
.
myListPlot[data_, opts : OptionsPattern] :=
ListPlot[data, GridLines -> {None, {data[[1]]}},
FilterRules[{opts}, Options[ListPlot]]]
which results in:
myListPlot[data, PlotStyle -> Red, Joined -> True]
answered yesterday
chuychuy
9,3131741
9,3131741
$begingroup$
OptionsPattern[ListPlot]
is more precise.
$endgroup$
– Edmund
yesterday
add a comment |
$begingroup$
OptionsPattern[ListPlot]
is more precise.
$endgroup$
– Edmund
yesterday
$begingroup$
OptionsPattern[ListPlot]
is more precise.$endgroup$
– Edmund
yesterday
$begingroup$
OptionsPattern[ListPlot]
is more precise.$endgroup$
– Edmund
yesterday
add a comment |
$begingroup$
The usual way to define a Wolfram Language function that takes n
arguments and an arbitrary number of options is like this:
f[arg1_, ..., argn_, opts___] := ...
A little bit of pattern matching background (taken from the WL reference):
_
any single expression
x_
any single expression, to be namedx
__
any sequence of one or more expressions
x__
sequence namedx
x__h
sequence of expressions, all of whose heads areh
___
any sequence of zero or more expressions
x___
sequence of zero or more expressions namedx
x___h
sequence of zero or more expressions, all of whose heads areh
$endgroup$
add a comment |
$begingroup$
The usual way to define a Wolfram Language function that takes n
arguments and an arbitrary number of options is like this:
f[arg1_, ..., argn_, opts___] := ...
A little bit of pattern matching background (taken from the WL reference):
_
any single expression
x_
any single expression, to be namedx
__
any sequence of one or more expressions
x__
sequence namedx
x__h
sequence of expressions, all of whose heads areh
___
any sequence of zero or more expressions
x___
sequence of zero or more expressions namedx
x___h
sequence of zero or more expressions, all of whose heads areh
$endgroup$
add a comment |
$begingroup$
The usual way to define a Wolfram Language function that takes n
arguments and an arbitrary number of options is like this:
f[arg1_, ..., argn_, opts___] := ...
A little bit of pattern matching background (taken from the WL reference):
_
any single expression
x_
any single expression, to be namedx
__
any sequence of one or more expressions
x__
sequence namedx
x__h
sequence of expressions, all of whose heads areh
___
any sequence of zero or more expressions
x___
sequence of zero or more expressions namedx
x___h
sequence of zero or more expressions, all of whose heads areh
$endgroup$
The usual way to define a Wolfram Language function that takes n
arguments and an arbitrary number of options is like this:
f[arg1_, ..., argn_, opts___] := ...
A little bit of pattern matching background (taken from the WL reference):
_
any single expression
x_
any single expression, to be namedx
__
any sequence of one or more expressions
x__
sequence namedx
x__h
sequence of expressions, all of whose heads areh
___
any sequence of zero or more expressions
x___
sequence of zero or more expressions namedx
x___h
sequence of zero or more expressions, all of whose heads areh
answered yesterday
rafalcrafalc
697212
697212
add a comment |
add a comment |
Thanks for contributing an answer to Mathematica Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f191884%2fdefine-function-that-behaves-almost-identically-to-mathematica-function%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
$begingroup$
Try changing
myListPlot[data_, opts_]
tomyListPlot[data_, opts___]
.$endgroup$
– rafalc
yesterday
$begingroup$
Works like a charm. Why not go ahead and make it an answer.
$endgroup$
– Tom
yesterday