Define function that behaves almost identically to Mathematica function












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
    yesterday












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
















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
    yesterday












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














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 yesterday









TomTom

1,293919




1,293919












  • $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$
    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$
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










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
    yesterday



















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
      yesterday
















    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
      yesterday














    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 yesterday









    chuychuy

    9,3131741




    9,3131741












    • $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




    $begingroup$
    OptionsPattern[ListPlot] is more precise.
    $endgroup$
    – Edmund
    yesterday











    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 yesterday









        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

            How to label and detect the document text images

            Vallis Paradisi

            Tabula Rosettana