In Julia, is it always the case that :a < :b evaluates to true?
More generally, can I rely on the relational operators (ie <, >, ==, <=, >=) behaving for symbols in exactly the same way they do for strings?
Yes. The source code here shows that Julia uses C function strcmp to do the comparison:
cmp(a::String, b::String) = lexcmp(a.data, b.data)
cmp(a::Symbol, b::Symbol) = Int(sign(ccall(:strcmp, Int32, (Cstring, Cstring), a, b)))
Related
I wanted to see the difference between infix operator and compound assignment operator in r . So I used this example. This example may not be a good choice.
library("magrittr", lib.loc="~/R/R-3.5.1/library")
x <- rnorm(10)
x
bb=x %<>% abs%>% sort
bb1=x %>% abs %>% sort
But seems like i am getting the same answer in both cases.
Is there any unique uses of compound assignment operator ??
Thank you
I'm reading the advanced R introduction by Hadley Wickham, where he states that [ (and +, -, {, etc) are functions, so that [ can be used in this manner
> x <- list(1:3, 4:9, 10:12)
> sapply(x, "[", 2)
[1] 2 5 11
Which is perfectly fine and understandable. But if [ is the function required to subset, does ] have another use rather than a syntactical one?
I found that:
> `]`
Error: object ']' not found
so I assume there is no other use for it?
This is the fundamental difference between syntax and semantics. Semantics require that — in R — things like subsetting and if etc are functions. That’s why R defines functions `[`, `if` etc.
And then there’s syntax. And R’s syntax dictates that the syntax for if is either if (condition) expression or if (condition) expression else expression. Likewise, the syntax for subsetting in R is obj[args…]. That is, ] is simply a syntactic element and it has no semantic equivalent, no corresponding function (same as else).
To make this perhaps even clearer:
[ and ] are syntactic elements in R that delimit a subsetting expression.
By contrast, `[` (note the backticks!) is a function that implements the subsetting operation.
Somehow though, I was expecting ] to be a syntactical element, by default: indexing from the end. So I define it myself in my code:
"]" <- function(x,y) if (y <= length(x)) x[length(x)+1-y] else NA
With the given example, then:
sapply(x, "]", 1)
[1] 3 9 12
sapply(x, "]", 2)
[1] 2 8 11
Main Question
I did
> `+` = `-`
> 5 + 2
[1] 3
How can I "rollback" without restarting the console? Doing
> `+` = sum
of course, restores one function of + but not all. For example
> c(3,4) + c(1,2)
[1] 10
How could I restore other functions of +?
Extra related questions
Is there a name for this "kind of assignment" or the kind of functions that "+" and "-" represent?
What terms can be used to differentiate the function "+" from the function "%+%" that one could create doing
`%+%` = function(x,y){print(paste(x,"+",y,"=",x+y))}
rm() removes an object from your workspace.
rm(`+`)
will remove your custom definition that masks the built-in function.
There is nothing special about the assignment you did. As nrussell points out, infix operators (aka binary operators) are generally possible to define by wrapping them in percent signs. The basic math ones (+, -, *, /, ^, even = and <- and logical operators, ==, |, ||, &, &&, <, etc.) are special in that the parser knows they're binary operators even without being wrapped in %. You can see ?Arithmetic (alias ?"+") and ?base::Ops for more details.
You can override this by fully qualifying the function in reassignment:
`+` = `-`
5 + 2
#[1] 3
`+` <- base::`+`
5 + 2
#[1] 7
It's probably better to just rm the new function though, as Gregor suggests, otherwise you will just have extra object floating around your environment needlessly.
Functions such as +, -, *, etc., and even %+% are called infix operators. The difference is that the former are built into the R language (they are primitives), and therefore do not need to be wrapped in % % to avoid generating a parsing error.
I know the basics like == and !=, or even the difference (vaguely) between & and &&. But stuff like %in% and %% and some stuff used in the context of sprintf(), like sprintf("%.2f", x) stuff I have no idea about.
Worst of all, they're hard to search for on the Internet because they're special characters and I don't know what they're called...
There are several different things going on here with the percent symbol:
Binary Operators
As several have already pointed out, things of the form %%, %in%, %*% are binary operators (respectively modulo, match, and matrix multiply), just like a +, -, etc. They are functions that operate on two arguments that R recognizes as being special due to their name structure (starts and ends with a %). This allows you to use them in form:
Argument1 %fun_name% Argument2
instead of the more traditional:
fun_name(Argument1, Argument2)
Keep in mind that the following are equivalent:
10 %% 2 == `%%`(10, 2)
"hello" %in% c("hello", "world") == `%in%`("hello", c("hello", "world"))
10 + 2 == `+`(10, 2)
R just recognizes the standard operators as well as the %x% operators as special and allows you to use them as traditional binary operators if you don't quote them. If you quote them (in the examples above with backticks), you can use them as standard two argument functions.
Custom Binary Operators
The big difference between the standard binary operators and %x% operators is that you can define custom binary operators and R will recognize them as special and treat them as binary operators:
`%samp%` <- function(e1, e2) sample(e1, e2)
1:10 %samp% 2
# [1] 1 9
Here we defined a binary operator version of the sample function
"%" (Percent) as a token in special function
The meaning of "%" in function like sprintf or format is completely different and has nothing to do with binary operators. The key thing to note is that in those functions the % character is part of a quoted string, and not a standard symbol on the command line (i.e. "%" and % are very different). In the context of sprintf, inside a string, "%" is a special character used to recognize that the subsequent characters have a special meaning and should not be interpreted as regular text. For example, in:
sprintf("I'm a number: %.2f", runif(3))
# [1] "I'm a number: 0.96" "I'm a number: 0.74" "I'm a number: 0.99"
"%.2f" means a floating point number (f) to be displayed with two decimals (.2). Notice how the "I'm a number: " piece is interpreted literally. The use of "%" allows sprintf users to mix literal text with special instructions on how to represent the other sprintf arguments.
The R Language Definition, section 3.1.4 refers to them as "special binary operators". One of the ways they're special is that users can define new binary operators using the %x% syntax (where x is any valid name).
The Writing your own functions section of An Introduction to R, refers to them as Binary Operators (which is somewhat confusing because + is also a binary operator):
10.2 Defining new binary operators
Had we given the bslash() function a different name, namely one of the
form
%anything%
it could have been used as a binary operator in expressions
rather than in function form. Suppose, for example, we choose ! for
the internal character. The function definition would then start as
> "%!%" <- function(X, y) { ... }
(Note the use of quote marks.) The function could then be used as X %!% y. (The backslash symbol itself
is not a convenient choice as it presents special problems in this
context.)
The matrix multiplication operator, %*%, and the outer product matrix
operator %o% are other examples of binary operators defined in this
way.
They don’t have a special name as far as I know. They are described in R operator syntax and precedence.
The %anything% operators are just normal functions, which can be defined by yourself. You do need to put the name of the operator in backticks (`…`), though: this is how R treats special names.
`%test%` = function (a, b) a * b
2 %test% 4
# 8
The sprintf format strings are entirely unrelated, they are not operators at all. Instead, they are just the conventional C-style format strings.
The help file, and the general entry, is indeed a good starting point: ?'%in%'
For example, you can see how the operator '%in%' is defined:
"%in%" <- function(x, table) match(x, table, nomatch = 0) > 0
You can even create your own operators:
'%ni%' <- Negate('%in%')
I have a vector v and I want to find all those elements, that have values between 4 and 7.
v = c(1:9)
# indices of elements with values larger than 4
which(v > 4)
# indices of elements with values smaller than 7
which(v < 7)
v>4 and v<7 give boolean vectors, which I'd like to combine. I tried the following, which did not work for me,...
# combination?
matching = which(v>4 && v<7) # does not work
How can I applay a boolean operation on two boolean vectors, that gives me a resulting vector?
Use & and not &&. R is different from other languages in that the & is not a bitwise and, but a logical operator.
&& only evaluates the first element of each vector:
‘&’ and ‘&&’ indicate logical AND and ‘|’ and ‘||’ indicate
logical OR. The shorter form performs elementwise comparisons in
much the same way as arithmetic operators. The longer form
evaluates left to right examining only the first element of each
vector. Evaluation proceeds only until the result is determined.
The longer form is appropriate for programming control-flow and
typically preferred in ‘if’ clauses.
See ?"&&" for more details.