Tag Info

Hot answers tagged

19

There is one semi-conditional side effect I can think of that is okay: while(iter.MoveNext()) That said, I think this falls mostly into the "never is a really big qualifier" category. I can think of a few rare cases where I've seen it be acceptable, but in general this is vile and to be avoided. I also cannot think of a scenario where that particular ...


12

We already do; that's what multi-core processors are for. Part of highly-optimizing for speed is specialization. When you build processors that specialize in one thing, you can optimize for that one thing, and ignore optimizing for anything else. Optimization can have conflicting goals. That's the reason we have many different kinds of data structures; ...


10

Because general computation is hard to parallelize into the SIMD model that is the driving force behind GPU design. Graphics has the bonus of having each vertex of the rendered mesh being able to be looked at in isolation and execute the same operations on it to figure out it's position on the screen and with each resulting pixel on screen is the same. Those ...


5

In my world, a read from memory may be considered a side effect (e.g. memory mapped IO). Now, consider the following: while( ( *memory_mapped_device_status_register & READY_FLAG) == 0) { // Wait } And compare to: status = *memory_mapped_device_status_register; while( ( status & READY_FLAG) == 0) { // Wait ...


4

In typical OO parlance, methods belong to objects. If methods were themselves objects, then these objects would of course have methods. But those are objects, too, so they have methods. Which are objects which have methods which are objects which have methods which are objects which have methods … There are ways to deal with this sort of recursion, but the ...


4

Your piece of code can easily be rewritten into: if(not condition1) { MethodA() return } //perform some logic if(condition2) { //perform some logic if(condition3) { //Perform logic return } } MethodA(param) Then, a part of the code can be extracted into a method: int Hello() { if(condition1) { ...


2

In Python, methods are regular fields, but callable. Also they have a self parameter that is explicitly declared, but implicitly passed; though you can invoke any method on the class object, passing an instance explicitly. class A: def __init__(self, val): self._val = val def fun(self): print(self._val) def invoke(f): f() def ...


2

Yes, in javascript. That's because in javascript methods are functions and functions are full-fledged objects. Example: // Here's a "class" with method foo. // Note that javascript don't really have classes, // we use constructors instead: function FooTeNator () { this.text = "foo"; } FooTeNator.prototype.foo = function () {return this.text} Notice ...


2

I found this. The Haskell equivalent is sequence, and Clojure has a something similar in juxt, which takes a list of functions and returns a function that applies them all to a single value. Given the very different names, it does not seem like there is a term for the concept that is widely accepted between languages.


2

It's just called a map. Functions being first-class means they usually aren't any different from any other list element, including in terminology. There's a better way of expressing it, however. You generally don't use Function or Object directly like that, as you're throwing away a lot of type information. Here's a better implementation: def dmap[A, ...


2

As always with such questions, this is a matter of degree. If there were unequivocal proof that any side effect within an if expression always led to worse code, then it wouldn't be legal to create those expressions. Language designers may be ideosyncratic, fallible humans, but they're not that stupid. That said, what are examples of justified side effects ...


2

Though really smelly code, it has the advantage of being simpler (and maybe faster if you don't have a good optimizing compiler) than the equivalent if (a > 33 | b < 54) {b++; ...} else b++; but of course it is possible to optimize it to the following (but watch out! this one has different behavior in case of overflow!): b++; if (a > 33 | b < ...


1

Run the simulator through every step to N+1, and then record the state. When you want to jump to N+1, just restore the state you recorded.


1

Other answers are good, but the question reminds me of a past phenomenon in computer graphics (remember that?) It was called the Wheel of Reincarnation. It goes like this: Invent a computer. Program the computer to do something useful, like draw pictures. For performance, invent a special processor to off-load the picture drawing. To improve the special ...


1

It comes down to the contracts that your methods obey. Your example code doesn't show any exception specifications, so technically nobody should assume anything about what happens for maxLength < 0 - it doesn't even matter whether you throw an exception at all. If you do want that behaviour to be part of your semantics, it should be documented. Then ...


1

It depends a little bit of what you are doing with these properties, but one pattern that would get around the explicit casting would be double-dispatch or 'visitor'. It's a common pattern that is used in operating on trees but it think it might suit your need. In c++ the only disadvantage is that there is a point where you might need to refer to all your ...


1

Functions in JavaScript are first-class citizens (objects with methods). There are no methods per se, but every function can be invoked as a method on any object. Functions can also be assigned to fields and invoked in c++-like object-oriented syntax. var func = function(arg) { this.field = arg; } func.apply(null, ["value0"]); // 'func' invoked as a free ...


1

C++11 lambdas: struct Foo{ int bar(){ return 42; } }; Foo f; auto methodobject = [&]{ return f.bar(); }; int i = methodobject(); //calls f.bar Lambdas are implemented as regular classes with the captured objects (in this case Foo &f) as members and an auto operator (){/*whatever is in the lambda declaration*/}. With a bit more ...



Only top voted, non community-wiki answers of a minimum length are eligible