There is RxJava-Computation-Expressions extension for RxJava. It provides operators like ifThen and others. However, the advantages of using it are not obvious. Because standard if-else block can be used or ternary operator, which are way readable and usual. I checked different functional languages and didn't find similar construction. All of them are using standard if-else operator.
Moreover, such extension is available not for all Rx languages and available only for RxJava 1.x.
The question is: what is the purpose of using ifThen operator from RxJava? Which advantages could it give comparing to standard if-else or ternary operator?
They allow taking a branch at subscription time and not when the sequence is assembled. It can mean that different subscribers will get different branches.
Further reading.
Related
=> isn't overloadable in C++. I have always wondered whether this is just a pure coincidence or whether there is a specific reason this sequence of two characters cannot be overloaded. The emergence of the comparison operator <=> leads me to believe the former.
I think it's a prime candidate for overloading because it looks like an arrow, which is very common in functional programming syntax and mathematics. Granted -> exists already but having another arrow could be useful to disambiguate between the two. I also don't see how it could break backwards compatibility.
C++ only allows you to overload operators that are already operators in the base language.
The base language defined operators, each with its associated precedence and associativity. In an operator overload, you can supply the meaning of that operator (the code that will be executed) for types for a user-defined type (or sometimes, for a combination of two user-defined types).
You cannot, however, just choose an arbitrary set of symbols, and treat it as an operator (no matter how attractive that might be).
There are languages that do allow this--for example, ML (and many of its descendants) allow you to define an operator with an entirely new name that's not part of the base language at all. When you do this, you define the precedence and associativity you want that operator to have. I think that's great, and provides a useful capability--but (at least as the capability is defined in ML) it also has some weaknesses that probably wouldn't fit nearly so well with how things work in C++. I wouldn't expect to see it as part of C++ any time soon (or, probably, ever).
In C++, “overloading” is a mechanism for providing a custom definition for existing operators so they can be used in custom types, not a mechanism for adding new operators to the language. You can’t change the meaning of a “=>” operator because C++ (as of this writing at least) doesn’t have a “=>” operator.
As a supplement to Jerry’s great answer, I want to point out that this was not an oversight by any stretch, but a very conscious design decision. Bjarne Stroustrup, the original creator of the C++ language, describes his thoughts on this in his fabulous book “The Design and Evolution of C++”, as I quote here:
I [Stroustrup] considered it important to provide overloading as a mechanism for extending the language and not for mutating it; that is, it is possible to define operators to work on user-defined types (classes), but not to change the meaning of operators on built-in types. In addition, I didn’t want to allow programmers to introduce new operators. I feared cryptic notation and having to adopt complicated parsing strategies like those needed for Algol68.”
(bold emphasis mine, italics in the original)
Source: Stroustrup, Bjarne: “The Design and Evolution of C++”, Addison-Wesley, 1994. §3.6.5
PS: Although a bit dated as a reference for modern C++ design, this is an excellent and fascinating source to explore the history and the reasoning that led to the design of the original C++ language. The language further design has long been under the purview of an ISO Standards committee but its continuous evolution has continued to be driven by many of the same principles described in the book and Dr. Stroustrup continues to be an important voice in that evolution process.
In C++, expression templates is a technique that relies on the compiler's knowledge about expressions in C++ code to simplify them and optimize them beyond what would be possible in a procedural program. It's a powerful technique used by e.g. the Eigen and Armadillo matrix libraries to speed up certain compound operations on matrices. An incomplete wiki page on the Eigen web page almost starts explaining it.
I wonder if a similar technique exists in Rust, i.e. is there a way to make the Rust compiler optimize certain expressions at compile time so that the least amount of temporaries is created.
If I read Expression Templates right, then you can see them in action with Rust Iterators: methods such as filter, take, etc etc return an expression template, a struct which represents the computation but doesn't perform it until requested. This gives the optimization you require right away, no temporaries are created.
Using the where clause I imagine one can write specializations to further optimize certain combinations of computations.
I really enjoy using Like operator of VB.NET. It suffices for the majority of my uses, and I am sure this is true for the 'average' developer.
One of the main resason I avoid regexes is that the syntax is complicated and it is hard to see at first glance what is happening. The Like operator uses fool-proof syntax.
Is it just me, or do other people have similar views? On the same note, is there such than as a 'simplified' regex notation.
Like operator: http://msdn.microsoft.com/en-us/library/swf8kaxw(v=vs.71).aspx
I would use whatever your company's standards dictate or, if that's not applicable, whatever your personal standards dictate.
Some people are intimately familiar with Regular Expressions and would prefer the complete syntax. Others are not and may be more comfortable with the Like operator (in my environment, a lot of folks cross from the T-SQL world to .NET so the Like operator is very familiar).
Additionally, as per the documentation, the Like operator can be overloaded such that you can utilize it for comparison on your own types, as opposed to just strings.
I think or is keyword in c++.
It might be that I've been doing too much python code recently but I find or is more readable than || and xor much more readable than ^.
Is it a good idea to use the word alternatives to the symbolic operators?
Why don't I see them used more?
The unsatisfying answer is that you should use symbolic operators because everyone else does.
An arguably more sensible reason is that they stand out more from the rest of the code.
Is it a good idea to use the word alternatives to the symbolic operators?
Completely depends on the target audience for your code – both people and tools. People can be unused to them, and some tools don't recognize them. (Sometimes those tools use <ciso646> to define them as macros.)
I've started to use "and" and "or" more, especially when switching between C++ and Python, and it has been more readable. The bit of extra consistency between languages matters more than I first thought it would, but more importantly, && and || are control structures and not operators (i.e. short-circuiting), thus making them words differentiates from operators.
(Yes, technically they're operators in C++, but they're more similar to if, else, return, while, and so forth than +, -, *, and other operators. The comma and conditional operators are similarly control structures, and it probably isn't a coincidence they are often found confusing, or at least less readable than separate statements and if/else, respectively.)
However, I very rarely use them in new code written for SO, for example, because I've not yet encountered a question where bringing up this side issue was more important than being readable to SO's C++ audience.
Every C++ programmer knows about && and ||.
Not every C++ programmer is aware that and and or are legal alternatives.
For that reason alone, you're better off sticking with what's commonly used.
It is pretty easy to get used to, so I'd say it's not a big deal, and definitely not worth potentially confusing the reader of your code over.
These keywords are alternative tokens which were added to the standard (Standard C) in 1995. See details here
https://en.wikipedia.org/wiki/C_alternative_tokens
Why the keywords were added:
The alternative tokens allow programmers to use C language bitwise and logical operators which could otherwise be hard to type on some international and non-QWERTY keyboards.
How they were added:
They are implemented as a group of macro constants in the C standard library in the iso646.h header.
The iso646.h header defines 11 macros including or.
What about C++:
The above-mentioned identifiers are operator keywords in the ISO C++ programming language and do not require the inclusion of a header file. For consistency, the C++98 standard provides the header <ciso646>. However the latter file has no effect, being empty.
So, there is a historic reason for having the keywords in C/C++ languages, and it is not related to what is better to use. As mentioned above, you should stick to a coding convention.
My first question is or a bit-wise or | or a Boolean shortcut or ||?
I bet there is half a dozen people on my team that would have to go look it up.
So I think it is better to stick with the standard convention,
Because that is what people are used to. The whole point of programming is to not be ambiguous.
These keywords are only there for terminals that can't handle the special characters |, & etc. Whether they constitute a more readable code or not is arguable.
If you know what || means then or is not more readable than ||. And if you know the very fundamentals of C++, i.e. the syntax, then in my humble opinion one is not more readable than the other.
Also, C++ programmers in most cases use the special-character alternatives of the keywords. So it's usually a good idea not to be the exception in a project, unless you're starting a project and you're setting the rules.
|| is how you say "boolean or" in C++. If you actually write or you are going to confuse the heck out of readers of your code, even if you can get the compiler to accept it.
I really do have sympathy for your argument, deft. Honestly. C++ code is really ugly (and IMHO hard to follow) due to its reliance on line-noise-like symbology. But that's the C++ philosophy. If you want nice Englishy reable code, C++ is just not your language. Try Ada.
I'm serious here. In a lot of ways Ada is a better language. This is one of them. But if you are going to stick with C++ instead, you need to ebrace it. Trying to write Ada (or Pascal) in C++ is no better than trying to write C++ or Fortran in Ada.
I have a little bit of experience in C++. I know how to overload the plus sign and what not, but would like to overload the space operator.
For example:
MyObject obj();
result = obj - foo; // This would be treated as a normal '-' operation.
result = obj-foo; // This would invoke code which throws an assert at runtime
This would allow me to enforce certain style guidelines I'm trying to set forth for my team.
There is no space operator in C++. Whitespace is not significant in most cases, there is no difference from a parsing point of view between
a-b
and
a - b
Which I'm happy for.
Note: there are some corner cases (nested templates spring to mind) where it actually matters, but that's more of an artifact of the grammar than indications of the whitespace being active as an "operator", in my opinion.
Bjarne Stroustrup did at one time suggest allowing overloading of whitespaces [PDF]. But seeing as this article was published on April 1st he may not have been 100% serious...
The article is worth a read though.
There is no such item as the whitespace operator. In general in C++ whitespace is irrelevant and doesn't cause functionality differences in the program, hence there's nothing to overload.
What you're looking for is a static analysis tool for C++. Something akin to StyleCop for C++. Unfortunately I don't have a lot of experience in this area and can't recommend a specific program. Likely someone else on this thread will be able to though.
Sorry, not possible.
Instead look at code formatters - there are a few for every popular IDE (for example checkstyle for eclipse), or you can setup pre-commit hook in your version control system server, that checks if code before and after formattting is the same, and if not, it returns error instead of allowing to commit the code.
It's not possible. Space is not an operator. If you want to enforce style, use static code analysis tools and talk to your team members. Try cpplint or something similar.
If you want to enforce style guidelines, obtain a tool or application that can identify violations of style. A tool that can do this is Klocwork. There are probably smaller and simpler tools out there.
Worst case, write your own.
I'd recommend a look at Guy Steele's new HPC language: Fortress. It allows the overloading of the juxtaposition operator. There's also a blog post covering this aspect here. It's not going to help you with formatting, but it's rather funky. (Haskell and ML also use juxtaposition to apply functions).