Tag Info

New answers tagged

1

This is a simple Python implementation: from random import random def select(container, weights): total_weight = float(sum(weights)) rel_weight = [w / total_weight for w in weights] # Probability for each element probs = [sum(rel_weight[:i + 1]) for i in range(len(rel_weight))] for (i, element) in enumerate(container): if ...


3

Just adding explicit type annotations to Ruby wouldn't do the trick. One key characteristic of Ruby is that any method can be redefined at runtime. So even if you know that some value will always be an instance of class C, that doesn't allow you to do things like inlining. Note that in Ruby, even loops are actually implemented as method calls. So your ...


0

Cython and Numba are examples of what this would look like for Python, and yes, you can get performance comparable to C code. Cython is an extension of the Python language that adds type annotations and makes it easy to call C functions. Numba uses hints provided as Python @decorators to inform it of the data types for which it will generate efficient ...


1

Performance doesn't always come down to interpreted vs. compiled. There are so many other factors that can influence the performance equation: The nature of the software can favor programmer productivity over clock cycles. Other factors are more important, like network and database latency. Compilation and interpretation are a gray area anyway. Is a JIT ...


0

The other big difference between procs, methods and lambdas is how they deal with break and return. Basically, a return in a Proc will jump out of any enclosing method as well, and a return from a lambda will only exit the lambda. A break from a Proc tries to break out of an enclosing iteration (if there's no iteration, you'll get an error), while a break ...


4

Summary: The design without the abstract class will be only be acceptable if it is carefully documented to distinguish its abstract and concrete behaviours. The Liskov Substitution Principle is generally regarded as a "good thing". By the LSP, I mean that if type S is a subtype of type T, then objects of type S should behave as objects of type T are ...


1

Congratulations, you just have discovered how the violation of Liskov Substitution Principle looks like, as the very first commenter politely pointed you at. To answer exactly to your question: according to the aforementioned principle, you don't design a subclass whose method contradicts its superclass. Reasons for this are explained in the literature ...


2

This bothers me because it looks like a logical contradiction: All scissors are right-handed. Incorrect at this point. You've defined scissors as all having handedness, and that this defaults to right-handed if not overridden. You have not said that all scissors are right-handed, that would require overriding being prohibited (as can be done in ...


34

There is nothing wrong with the design shown in the question. While one could also introduce abstract Scissor with two concrete subclasses, and maybe more overall clarity, it's also common to do it like shown (especially when the hierarchy is a result of years of incremental development, with Scissor being around for much longer than the concept of ...


9

Another option is to introduce the handed-ness as a dependency with a default value of right-handed. In pseudocode here as I am not familiar with Ruby: class Scissors { Scissors(isRightHanded = true) { _isRightHanded = isRightHanded } IsRightHanded() { return _isRightHanded } } class LeftHandedScissors : Scissors { ...


23

You are thinking too logically! There is no logical contradiction because class definitions are not logical propositions. Having the Scissor base class return true does not correspond to saying that all scissors are right-handed. It just means that a scissor instance is right-handed unless the method is overridden in a subclass.


15

You don't. It's like saying that all animals are dogs, and then asking how to make cats meow instead of bark. If you were naming your classes properly, your Scissor class would rather be named RightHandedScissor; now does it make sense to inherit LeftHandedScissor from RightHandedScissor? One possible approach is to make Scissor class abstract, and ...


3

"All scissors are right-handed"? Where do you get that idea from? Your code only expresses "scissors are right-handed by default". It's a default value, not a design decision. If there were no way of having a different value, what's the point in programming a boolean accessor function?


0

When reading keep in mind that I am a Ruby on Rails Developer, and answering from that eco-system. My views will be slanted in that direction. Would a PHP programmer want to learn Ruby?, I know I do, but conversely, would a Ruby programmer want to learn PHP? Absolutely not, in most cases. Not just because I don't like PHP (I don't, but I use it), but ...



Top 50 recent answers are included