How does the overloading of the assignment operator in combination of the length function work?
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
add a comment |
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
add a comment |
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
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
r operator-overloading
asked Nov 25 '18 at 20:36
KarlPKarlP
4,27821938
4,27821938
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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
I was wondering if you meantnames
in your example rather thanname
?
– prosoitos
Nov 25 '18 at 21:25
@prosoitos - I didn't. I usedname
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
add a comment |
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
});
}
});
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%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
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
I was wondering if you meantnames
in your example rather thanname
?
– prosoitos
Nov 25 '18 at 21:25
@prosoitos - I didn't. I usedname
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
add a comment |
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
I was wondering if you meantnames
in your example rather thanname
?
– prosoitos
Nov 25 '18 at 21:25
@prosoitos - I didn't. I usedname
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
add a comment |
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
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
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 meantnames
in your example rather thanname
?
– prosoitos
Nov 25 '18 at 21:25
@prosoitos - I didn't. I usedname
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
add a comment |
I was wondering if you meantnames
in your example rather thanname
?
– prosoitos
Nov 25 '18 at 21:25
@prosoitos - I didn't. I usedname
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
add a comment |
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.
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%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
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