How does the overloading of the assignment operator in combination of the length function work?












3















How does the mutating implementation of length() actually work?



Example: Give a vector v, how does this set the length to 12?



length(v) <- 12


Can I create my own function that can overload an operator in the same way?



Example: Set every other element to 7



everyOther(v) <- 7









share|improve this question



























    3















    How does the mutating implementation of length() actually work?



    Example: Give a vector v, how does this set the length to 12?



    length(v) <- 12


    Can I create my own function that can overload an operator in the same way?



    Example: Set every other element to 7



    everyOther(v) <- 7









    share|improve this question

























      3












      3








      3


      2






      How does the mutating implementation of length() actually work?



      Example: Give a vector v, how does this set the length to 12?



      length(v) <- 12


      Can I create my own function that can overload an operator in the same way?



      Example: Set every other element to 7



      everyOther(v) <- 7









      share|improve this question














      How does the mutating implementation of length() actually work?



      Example: Give a vector v, how does this set the length to 12?



      length(v) <- 12


      Can I create my own function that can overload an operator in the same way?



      Example: Set every other element to 7



      everyOther(v) <- 7






      r operator-overloading






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 25 '18 at 20:36









      KarlPKarlP

      4,27821938




      4,27821938
























          1 Answer
          1






          active

          oldest

          votes


















          4














          Those assignment functions are just that, functions. They can be written in the following form (note the backticks - they must be used), where fname distinguishes the function name.



          `fname<-` <- function(x, value) { ... }


          So your everyOther assignment function can be written as



          `everyOther<-` <- function(x, value) {
          x[c(FALSE, TRUE)] <- value
          x
          }


          And we can use it just as we would length(x) <- value



          v <- 1:20
          everyOther(v) <- 7
          v
          # [1] 1 7 3 7 5 7 7 7 9 7 11 7 13 7 15 7 17 7 19 7





          share|improve this answer


























          • I was wondering if you meant names in your example rather than name?

            – prosoitos
            Nov 25 '18 at 21:25











          • @prosoitos - I didn't. I used name to mean the name of the function.

            – Rich Scriven
            Nov 25 '18 at 21:25








          • 1





            Got you. I thought you were using an existing accessor function as an example and I didn't realize that "name" was used to mean "name of function". Great explanation by the way :)

            – prosoitos
            Nov 25 '18 at 21:28













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          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: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          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%2fstackoverflow.com%2fquestions%2f53471699%2fhow-does-the-overloading-of-the-assignment-operator-in-combination-of-the-length%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          4














          Those assignment functions are just that, functions. They can be written in the following form (note the backticks - they must be used), where fname distinguishes the function name.



          `fname<-` <- function(x, value) { ... }


          So your everyOther assignment function can be written as



          `everyOther<-` <- function(x, value) {
          x[c(FALSE, TRUE)] <- value
          x
          }


          And we can use it just as we would length(x) <- value



          v <- 1:20
          everyOther(v) <- 7
          v
          # [1] 1 7 3 7 5 7 7 7 9 7 11 7 13 7 15 7 17 7 19 7





          share|improve this answer


























          • I was wondering if you meant names in your example rather than name?

            – prosoitos
            Nov 25 '18 at 21:25











          • @prosoitos - I didn't. I used name to mean the name of the function.

            – Rich Scriven
            Nov 25 '18 at 21:25








          • 1





            Got you. I thought you were using an existing accessor function as an example and I didn't realize that "name" was used to mean "name of function". Great explanation by the way :)

            – prosoitos
            Nov 25 '18 at 21:28


















          4














          Those assignment functions are just that, functions. They can be written in the following form (note the backticks - they must be used), where fname distinguishes the function name.



          `fname<-` <- function(x, value) { ... }


          So your everyOther assignment function can be written as



          `everyOther<-` <- function(x, value) {
          x[c(FALSE, TRUE)] <- value
          x
          }


          And we can use it just as we would length(x) <- value



          v <- 1:20
          everyOther(v) <- 7
          v
          # [1] 1 7 3 7 5 7 7 7 9 7 11 7 13 7 15 7 17 7 19 7





          share|improve this answer


























          • I was wondering if you meant names in your example rather than name?

            – prosoitos
            Nov 25 '18 at 21:25











          • @prosoitos - I didn't. I used name to mean the name of the function.

            – Rich Scriven
            Nov 25 '18 at 21:25








          • 1





            Got you. I thought you were using an existing accessor function as an example and I didn't realize that "name" was used to mean "name of function". Great explanation by the way :)

            – prosoitos
            Nov 25 '18 at 21:28
















          4












          4








          4







          Those assignment functions are just that, functions. They can be written in the following form (note the backticks - they must be used), where fname distinguishes the function name.



          `fname<-` <- function(x, value) { ... }


          So your everyOther assignment function can be written as



          `everyOther<-` <- function(x, value) {
          x[c(FALSE, TRUE)] <- value
          x
          }


          And we can use it just as we would length(x) <- value



          v <- 1:20
          everyOther(v) <- 7
          v
          # [1] 1 7 3 7 5 7 7 7 9 7 11 7 13 7 15 7 17 7 19 7





          share|improve this answer















          Those assignment functions are just that, functions. They can be written in the following form (note the backticks - they must be used), where fname distinguishes the function name.



          `fname<-` <- function(x, value) { ... }


          So your everyOther assignment function can be written as



          `everyOther<-` <- function(x, value) {
          x[c(FALSE, TRUE)] <- value
          x
          }


          And we can use it just as we would length(x) <- value



          v <- 1:20
          everyOther(v) <- 7
          v
          # [1] 1 7 3 7 5 7 7 7 9 7 11 7 13 7 15 7 17 7 19 7






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 25 '18 at 21:26

























          answered Nov 25 '18 at 20:53









          Rich ScrivenRich Scriven

          77.3k8103173




          77.3k8103173













          • I was wondering if you meant names in your example rather than name?

            – prosoitos
            Nov 25 '18 at 21:25











          • @prosoitos - I didn't. I used name to mean the name of the function.

            – Rich Scriven
            Nov 25 '18 at 21:25








          • 1





            Got you. I thought you were using an existing accessor function as an example and I didn't realize that "name" was used to mean "name of function". Great explanation by the way :)

            – prosoitos
            Nov 25 '18 at 21:28





















          • I was wondering if you meant names in your example rather than name?

            – prosoitos
            Nov 25 '18 at 21:25











          • @prosoitos - I didn't. I used name to mean the name of the function.

            – Rich Scriven
            Nov 25 '18 at 21:25








          • 1





            Got you. I thought you were using an existing accessor function as an example and I didn't realize that "name" was used to mean "name of function". Great explanation by the way :)

            – prosoitos
            Nov 25 '18 at 21:28



















          I was wondering if you meant names in your example rather than name?

          – prosoitos
          Nov 25 '18 at 21:25





          I was wondering if you meant names in your example rather than name?

          – prosoitos
          Nov 25 '18 at 21:25













          @prosoitos - I didn't. I used name to mean the name of the function.

          – Rich Scriven
          Nov 25 '18 at 21:25







          @prosoitos - I didn't. I used name to mean the name of the function.

          – Rich Scriven
          Nov 25 '18 at 21:25






          1




          1





          Got you. I thought you were using an existing accessor function as an example and I didn't realize that "name" was used to mean "name of function". Great explanation by the way :)

          – prosoitos
          Nov 25 '18 at 21:28







          Got you. I thought you were using an existing accessor function as an example and I didn't realize that "name" was used to mean "name of function". Great explanation by the way :)

          – prosoitos
          Nov 25 '18 at 21:28






















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


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


          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%2fstackoverflow.com%2fquestions%2f53471699%2fhow-does-the-overloading-of-the-assignment-operator-in-combination-of-the-length%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

          Costa Masnaga

          Fotorealismo

          Sidney Franklin