operator-keyword

Haskell operator vs function precedence

岁酱吖の 提交于 2019-11-27 12:35:54
I am trying to verify something for myself about operator and function precedence in Haskell. For instance, the following code list = map foo $ xs can be rewritten as list = (map foo) $ (xs) and will eventually be list = map foo xs My question used to be, why the first formulation would not be rewritten as list = (map foo $) xs since function precedence is always higher than operator precedence, but I think that I have found the answer: operators are simply not allowed to be arguments of functions (except of course, if you surround them with parentheses). Is this right? If so, I find it odd,

Concatenating strings doesn't work as expected [closed]

时光毁灭记忆、已成空白 提交于 2019-11-27 11:10:17
I know it is a common issue, but looking for references and other material I don't find a clear answer to this question. Consider the following code: #include <string> // ... // in a method std::string a = "Hello "; std::string b = "World"; std::string c = a + b; The compiler tells me it cannot find an overloaded operator for char[dim] . Does it mean that in the string there is not a + operator? But in several examples there is a situation like this one. If this is not the correct way to concat more strings, what is the best way? Your code, as written, works. You’re probably trying to achieve

How do I create and use a class arrow operator?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-27 10:53:47
问题 So, after researching everywhere for it, I cannot seem to find how to create a class arrow operator, i.e., class Someclass { operator-> () /* ? */ { } }; I just need to know how to work with it and use it appropriately. - what are its inputs? - what does it return? - how do I properly declare/prototype it? 回答1: The operator -> is used to overload member access. A small example: #include <iostream> struct A { void foo() {std::cout << "Hi" << std::endl;} }; struct B { A a; A* operator->() {

PHP use string as operator

泄露秘密 提交于 2019-11-27 09:41:34
Say I have a string, $char. $char == "*". I also have two variables, $a and $b, which equal "4" and "5" respectively. How do I get the result of $a $char $b, ie 4 * 5 ? Thanks :) You can use eval() as suggested by @konforce, however the safest route would be something like: $left = (int)$a; $right = (int)$b; $result = 0; switch($char){ case "*": $result = $left * $right; break; case "+"; $result = $left + $right; break; // etc } safest method is a switch construct: function my_operator($a, $b, $char) { switch($char) { case '=': return $a = $b; case '*': return $a * $b; case '+': return $a + $b

I keep getting a “The operator == is undefined for the argument type(s) boolean, int” and have no idea how to fix it

三世轮回 提交于 2019-11-27 08:33:16
问题 I keep getting a "The operator == is undefined for the argument type(s) boolean, int" in this bit of code at line 3: public void loadState(int i) { if (statesSaved[i] == 0) { return; } List list = TMIUtils.getMinecraft().h.at.e; for (int j = 0; j < 44; j++) { sx slot = (sx)list.get(j + 1); slot.c(null); ul itemstack = TMIUtils.copyStack(states[i][j]); if ((itemstack == null) || (itemstack.c < 0) || (itemstack.c >= sv.f.length) || (sv.f[itemstack.c] == null)) continue; slot.c(itemstack); } } I

What is '#' operator in C?

妖精的绣舞 提交于 2019-11-27 07:21:42
问题 Is there a '#' operator in C ? If yes then in the code enum {ALPS, ANDES, HIMALYAS}; what would the following return ? #ALPS 回答1: The C language does not have an # operator, but the pre-processor (the program that handles #include and #define ) does. The pre-processor simple makes #ALPS into the string "ALPS" . However, this "stringify" operator can only be used in the #define pre-processor directive. For example: #define MAKE_STRING_OF_IDENTIFIER(x) #x char alps[] = MAKE_STRING_OF_IDENTIFIER

Namespaces and operator resolution

半城伤御伤魂 提交于 2019-11-27 07:04:47
I am using a library that defines output stream operators (operator<<) in the global namespace. In my own namespace, I was always declaring such operators in the global namespace, and never had problems with them. But now for various reasons I need to declare those operators in my own namespace, and suddenly, the compiler can't seem to find the operators declared in the library anymore. Here's a simple example that illustrates my problem: #include <iostream> namespace A { struct MyClass {}; } std::ostream & operator<<( std::ostream & os, const A::MyClass & ) { os << "namespace A"; return os; }

User Defined Conversions in C++

。_饼干妹妹 提交于 2019-11-27 06:52:46
问题 Recently, I was browsing through my copy of the C++ Pocket Reference from O'Reilly Media, and I was surprised when I came across a brief section and example regarding user-defined conversion for user-defined types: #include <iostream> class account { private: double balance; public: account (double b) { balance = b; } operator double (void) { return balance; } }; int main (void) { account acc(100.0); double balance = acc; std::cout << balance << std::endl; return 0; } I've been programming in

How do I access the name of the variable assigned to the result of a function within the function?

匆匆过客 提交于 2019-11-27 05:53:14
问题 For example, suppose I would like to be able to define a function that returned the name of the assignment variable concatenated with the first argument: a <- add_str("b") a # "ab" The function in the example above would look something like this: add_str <- function(x) { arg0 <- as.list(match.call())[[1]] return(paste0(arg0, x)) } but where the arg0 line of the function is replaced by a line that will get the name of the variable being assigned ("a") rather than the name of the function. I've

php string number concatenation messed up

本秂侑毒 提交于 2019-11-27 04:49:36
问题 I got some php code here: <?php echo 'hello ' . 1 + 2 . '34'; ?> which outputs 234, but when I add a number 11 before "hello": <?php echo '11hello ' . 1 + 2 . '34'; ?> It outputs 1334 rather than 245(which I expected it to), why is that? 回答1: That's strange... But <?php echo '11hello ' . (1 + 2) . '34'; ?> OR <?php echo '11hello ', 1 + 2, '34'; ?> fixing issue. UPDv1: Finally managed to get proper answer: 'hello' = 0 (contains no leading digits, so PHP assumes it is zero). So 'hello' . 1 + 2