Define function that behaves almost identically to Mathematica functionErrorListPlot crops error barsHow to...

Where can I educate myself on D&D universe lore, specifically on vampires and supernatural monsters?

Manager has noticed coworker's excessive breaks. Should I warn him?

Identical projects by students at two different colleges: still plagiarism?

Are encryption algorithms with fixed-point free permutations inherently flawed?

Can "ee" appear in Latin?

Why is Shelob considered evil?

Why does finding small effects in large studies indicate publication bias?

Badly designed reimbursement form. What does that say about the company?

Can I legally make a website about boycotting a certain company?

What does “to the numbers” mean in landing clearance?

Rudeness by being polite

Will linear voltage regulator step up current?

Buying a "Used" Router

Does changing "sa" password require a SQL restart (in mixed mode)?

Short story where Earth is given a racist governor who likes species of a certain color

Is it ethical to apply for a job on someone's behalf?

Which was the first story to feature space elevators?

How can I differentiate duration vs starting time

Why would you use 2 alternate layout buttons instead of 1, when only one can be selected at once

Why is Bernie Sanders maximum accepted donation on actblue $5600?

Can you wish for more wishes from an Efreeti bound to service via an Efreeti Bottle?

Sing Baby Shark

Is Apex Sometimes Case Sensitive?

Integral check. Is partial fractions the only way?



Define function that behaves almost identically to Mathematica function


ErrorListPlot crops error barsHow to test if optional argument has been given?create function that behaves like continuedFractionHow can I define a function taking two distinguishable sets of options?How to define a Mathematica Function with varying number of input and output argumentsHow to combine multiple functions into a single function, indexed bya new argumentDefine function that counts recursive fibonacciShadowing of optional function arguments?Define a function with multiple lines mathematicaAn argument which returns the function that is applied to this argumentImposing conditions on argument for a function definition













8












$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}]


The ListPlot generated by the above code



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.










share|improve this question









$endgroup$












  • $begingroup$
    Try changing myListPlot[data_, opts_] to myListPlot[data_, opts___].
    $endgroup$
    – rafalc
    21 hours ago












  • $begingroup$
    Works like a charm. Why not go ahead and make it an answer.
    $endgroup$
    – Tom
    21 hours ago
















8












$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}]


The ListPlot generated by the above code



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.










share|improve this question









$endgroup$












  • $begingroup$
    Try changing myListPlot[data_, opts_] to myListPlot[data_, opts___].
    $endgroup$
    – rafalc
    21 hours ago












  • $begingroup$
    Works like a charm. Why not go ahead and make it an answer.
    $endgroup$
    – Tom
    21 hours ago














8












8








8


3



$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}]


The ListPlot generated by the above code



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.










share|improve this question









$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}]


The ListPlot generated by the above code



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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 22 hours ago









TomTom

1,293919




1,293919












  • $begingroup$
    Try changing myListPlot[data_, opts_] to myListPlot[data_, opts___].
    $endgroup$
    – rafalc
    21 hours ago












  • $begingroup$
    Works like a charm. Why not go ahead and make it an answer.
    $endgroup$
    – Tom
    21 hours ago


















  • $begingroup$
    Try changing myListPlot[data_, opts_] to myListPlot[data_, opts___].
    $endgroup$
    – rafalc
    21 hours ago












  • $begingroup$
    Works like a charm. Why not go ahead and make it an answer.
    $endgroup$
    – Tom
    21 hours ago
















$begingroup$
Try changing myListPlot[data_, opts_] to myListPlot[data_, opts___].
$endgroup$
– rafalc
21 hours ago






$begingroup$
Try changing myListPlot[data_, opts_] to myListPlot[data_, opts___].
$endgroup$
– rafalc
21 hours ago














$begingroup$
Works like a charm. Why not go ahead and make it an answer.
$endgroup$
– Tom
21 hours ago




$begingroup$
Works like a charm. Why not go ahead and make it an answer.
$endgroup$
– Tom
21 hours ago










2 Answers
2






active

oldest

votes


















14












$begingroup$

If you want to constrain it to only options from ListPlot, you could use OptionsPattern in combination with FilterRulesand Options.



myListPlot[data_, opts : OptionsPattern[]] := 
ListPlot[data, GridLines -> {None, {data[[1]]}},
FilterRules[{opts}, Options[ListPlot]]]


which results in:



myListPlot[data, PlotStyle -> Red, Joined -> True]


Mathematica graphics






share|improve this answer









$endgroup$













  • $begingroup$
    OptionsPattern[ListPlot] is more precise.
    $endgroup$
    – Edmund
    13 hours ago



















9












$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 named x


  • __ any sequence of one or more expressions


  • x__ sequence named x


  • x__h sequence of expressions, all of whose heads are h


  • ___ any sequence of zero or more expressions


  • x___ sequence of zero or more expressions named x


  • x___h sequence of zero or more expressions, all of whose heads are h






share|improve this answer









$endgroup$













    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    14












    $begingroup$

    If you want to constrain it to only options from ListPlot, you could use OptionsPattern in combination with FilterRulesand Options.



    myListPlot[data_, opts : OptionsPattern[]] := 
    ListPlot[data, GridLines -> {None, {data[[1]]}},
    FilterRules[{opts}, Options[ListPlot]]]


    which results in:



    myListPlot[data, PlotStyle -> Red, Joined -> True]


    Mathematica graphics






    share|improve this answer









    $endgroup$













    • $begingroup$
      OptionsPattern[ListPlot] is more precise.
      $endgroup$
      – Edmund
      13 hours ago
















    14












    $begingroup$

    If you want to constrain it to only options from ListPlot, you could use OptionsPattern in combination with FilterRulesand Options.



    myListPlot[data_, opts : OptionsPattern[]] := 
    ListPlot[data, GridLines -> {None, {data[[1]]}},
    FilterRules[{opts}, Options[ListPlot]]]


    which results in:



    myListPlot[data, PlotStyle -> Red, Joined -> True]


    Mathematica graphics






    share|improve this answer









    $endgroup$













    • $begingroup$
      OptionsPattern[ListPlot] is more precise.
      $endgroup$
      – Edmund
      13 hours ago














    14












    14








    14





    $begingroup$

    If you want to constrain it to only options from ListPlot, you could use OptionsPattern in combination with FilterRulesand Options.



    myListPlot[data_, opts : OptionsPattern[]] := 
    ListPlot[data, GridLines -> {None, {data[[1]]}},
    FilterRules[{opts}, Options[ListPlot]]]


    which results in:



    myListPlot[data, PlotStyle -> Red, Joined -> True]


    Mathematica graphics






    share|improve this answer









    $endgroup$



    If you want to constrain it to only options from ListPlot, you could use OptionsPattern in combination with FilterRulesand Options.



    myListPlot[data_, opts : OptionsPattern[]] := 
    ListPlot[data, GridLines -> {None, {data[[1]]}},
    FilterRules[{opts}, Options[ListPlot]]]


    which results in:



    myListPlot[data, PlotStyle -> Red, Joined -> True]


    Mathematica graphics







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered 21 hours ago









    chuychuy

    9,3131741




    9,3131741












    • $begingroup$
      OptionsPattern[ListPlot] is more precise.
      $endgroup$
      – Edmund
      13 hours ago


















    • $begingroup$
      OptionsPattern[ListPlot] is more precise.
      $endgroup$
      – Edmund
      13 hours ago
















    $begingroup$
    OptionsPattern[ListPlot] is more precise.
    $endgroup$
    – Edmund
    13 hours ago




    $begingroup$
    OptionsPattern[ListPlot] is more precise.
    $endgroup$
    – Edmund
    13 hours ago











    9












    $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 named x


    • __ any sequence of one or more expressions


    • x__ sequence named x


    • x__h sequence of expressions, all of whose heads are h


    • ___ any sequence of zero or more expressions


    • x___ sequence of zero or more expressions named x


    • x___h sequence of zero or more expressions, all of whose heads are h






    share|improve this answer









    $endgroup$


















      9












      $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 named x


      • __ any sequence of one or more expressions


      • x__ sequence named x


      • x__h sequence of expressions, all of whose heads are h


      • ___ any sequence of zero or more expressions


      • x___ sequence of zero or more expressions named x


      • x___h sequence of zero or more expressions, all of whose heads are h






      share|improve this answer









      $endgroup$
















        9












        9








        9





        $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 named x


        • __ any sequence of one or more expressions


        • x__ sequence named x


        • x__h sequence of expressions, all of whose heads are h


        • ___ any sequence of zero or more expressions


        • x___ sequence of zero or more expressions named x


        • x___h sequence of zero or more expressions, all of whose heads are h






        share|improve this answer









        $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 named x


        • __ any sequence of one or more expressions


        • x__ sequence named x


        • x__h sequence of expressions, all of whose heads are h


        • ___ any sequence of zero or more expressions


        • x___ sequence of zero or more expressions named x


        • x___h sequence of zero or more expressions, all of whose heads are h







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 21 hours ago









        rafalcrafalc

        697212




        697212






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Armoriale delle famiglie italiane (Car) Indice Armi | Bibliografia | Menu di navigazioneBlasone...

            Why does this relation fail symmetry and transitivity properties?Properties of Relations. Reflexive,...

            why typing a variable (or expression) prints the value to stdout?Calling a function of a module by using its...