polymorphism

Converting Haskell Polymorphic Cosine function to F#

时光总嘲笑我的痴心妄想 提交于 2019-12-12 09:44:49
问题 I'm trying to convert some Haskell code to F# but I'm having some trouble since Haskell is lazy by default and F# is not. I'm also still learning my way around F#. Below is a polymorphic cosine function in Haskell with pretty good performance. I want to try and keep the same or better performance parameters in F#. I would like to see a F# List version and a F# Seq version since the Seq version would be more like the lazy Haskell but the List version would probably perform better. Thanks for

Comparing Polymorphic Base Types in C++ without RTTI

≡放荡痞女 提交于 2019-12-12 08:39:32
问题 I have some pointers to a base type of Shape. I want to compare these objects using the == operator. The == operator should obviously return false if the objects are of different derived type. If they are of the same derived type however the members of the derived type should then be compared. I have read that using the C++ RTTI is bad practice and should only be used in rare and essential circumstances. As far as I can see this problem cannot be generally solved without using the RTTI. Each

Passing derived objects in a constructure

帅比萌擦擦* 提交于 2019-12-12 05:38:26
问题 This is a bit of a convoluted question, hopefully I can make it clear. I am finding that this may not be possible, but am trying to see if anybody has a solution. I have four classes, two are core classes and two are those core classes extended: extUser Extends coreUser extSecurity Extends coreSecurity In the constructor for coreUser you have this: public coreUser(string id, ref coreSecurity cs) When trying to extend coreUser you would have this: public extUser(string id ref extSecurity es) :

Java: reading from an input file, then moving data into subclasses

吃可爱长大的小学妹 提交于 2019-12-12 05:15:59
问题 Early stages of a simulation project, when this program runs, its supposed to read in a file such as the one below, then it should process each neuron and synapse through the program. There are two types of synapse, unnamed synapses and named ones. Synapse X above is a named synapse connecting neurons A and B. Unnamed synapses have a single dash in the name field. Neuron and synapse names be non-numeric. Any synapse that is named may be the target of a secondary synapse. The synapse B X above

How to access the properties of an instance of a derived class which is passed as a parameter in the form of the base class

余生颓废 提交于 2019-12-12 03:59:46
问题 In C# I have a base class and a derived class. I have a function which has the base class as an input parameter public void SomeFunction(BaseClass InstanceOfDerivedClass) Is there a way I can access the properties specific to the derived class, even though it has been passed as a base class? Could I use GetType or Cast or something like that? I appreciate that the solutions may not be elegant but at the moment the alternative is to repeat this function many times for the different derived

Polymorphism vs DownCasting

≡放荡痞女 提交于 2019-12-12 03:35:07
问题 Let's say I have a Base class Employee and an derived class Manager , like below: class Employee { public: string Name; }; class Manager : public Employee { public: string Designation; }; While implementing some function like below: Employee* SomeFunction(bool SomeCondition) { Employee *Emp = NULL; if (SomeCondition) { //Code goes here : Both Implementation 1 and 2 work fine! } return Emp; } When SomeCondition is true, I want to return a non-null object of type Manager . In such a scenario,

Redesign the OOP Splitting combined interfaces to individuals

丶灬走出姿态 提交于 2019-12-12 03:00:05
问题 I was trying to understand how to avoid double entry in a clean and "designable" way to the following scenario: public interface ICommandAble { } public interface ILogAble extends ICommandAble { } public interface IMonitorAble extends ICommandAble { } public abstract class ClassAbs { } public class A extends ClassAbs implements IMonitorAble, ILogAble { } Test method: public void test() { A a=new A(); List<ICommandAble>commandList=new ArrayList<ICommandAble>() if (a instanceof ILogAble) {

How do I store different types that implement the same trait in a vector and call common functions on them?

爱⌒轻易说出口 提交于 2019-12-12 02:58:37
问题 I'm learning Rust and I'm having difficulties in implementing polymorphism. I want to use an array to store either Circle or Test . trait Poli { fn area(&self) -> f64; } struct Circle { x: f64, y: f64, radius: f64, } impl Circle { fn new (xx: f64, yy: f64, r: f64) -> Circle{ Circle{ x: xx, y: yy, radius: r } } } impl Poli for Circle { fn area(&self) -> f64 { std::f64::consts::PI * (self.radius * self.radius) } } struct Test { x: f64, y: f64, radius: f64, test: f64, } impl Test { fn new (xx:

treat non-polymorphic objects in a polymorphic way with no performance overhead

旧城冷巷雨未停 提交于 2019-12-12 02:56:27
问题 This question is inspired by this question which was asking about calling the same method on dissimilar types when those types are known at compile time. This got me thinking. Suppose I had dissimilar non-polymorphic types but I wanted use them polymorphically. Furthermore, I want to do that without ever invoking new and delete since these are known performance bottlenecks. How would I do that? Note this is a Q&A style question. I have provided the answer I came up with. This is not to

Case Study: Polymorphism for Image Processing

ⅰ亾dé卋堺 提交于 2019-12-12 02:43:39
问题 I'm studying Digital Image Processing by myself and would be really grateful if somebody could comment whether polymorphism should be applied for this case or if there's a better class design. Basically, a 2D Filter/Kernel can be either: non-separable or separable . An important kernel operation is the convolution and the way to compute it, depends on the filter type. template < typename T > class CKernel2D{ public: //.... virtual CMatrix<T> myConvolution(const CMatrix<T> & input) = 0; //....