icomparable

F# comparison vs C# IComparable

拟墨画扇 提交于 2019-12-05 04:21:15
My problem, in a nutshell, is this: What can I do about storing a tuple (or any type with a constraint of 'comparison') in a C# container that requires an IComparable ? This works: > let x (y : 'a when 'a : comparison) = y ;; val x : y:'a -> 'a when 'a : comparison > x (1,2) ;; val it : int * int = (1, 2) I would have thought this would work: > let x (y : IComparable<int>) = y ;; val x : y:IComparable<int> -> IComparable<int> > x (1,2) ;; x (1,2) ;; ---^^^ stdin(28,4): error FS0001: The type ''a * 'b' is not compatible with the type 'IComparable<int>' And this as well: > let x (y : IComparable

Why is C# Array.BinarySearch so fast?

杀马特。学长 韩版系。学妹 提交于 2019-12-05 01:36:34
I have implemented a very simple binarySearch implementation in C# for finding integers in an integer array: Binary Search static int binarySearch(int[] arr, int i) { int low = 0, high = arr.Length - 1, mid; while (low <= high) { mid = (low + high) / 2; if (i < arr[mid]) high = mid - 1; else if (i > arr[mid]) low = mid + 1; else return mid; } return -1; } When comparing it to C#'s native Array.BinarySearch() I can see that Array.BinarySearch() is more than twice as fast as my function, every single time. MSDN on Array.BinarySearch : Searches an entire one-dimensional sorted array for a

F# Set using custom class

纵饮孤独 提交于 2019-12-05 01:06:59
问题 I'm trying to use Set operations with a class that I have. Every instance of this class has a unique ID. Do I need to implement the System.IComparable interface and if so how would I? type SomeClass(id : int) = member this.ID = id let someSet = Set.of_list [SomeClass(1); SomeClass(2)] let test = someSet.Contains(SomeClass(2)) 回答1: Here's an implementation that should work: type SomeClass(id : int) = member this.ID = id override this.Equals(o) = match o with | :? SomeClass as sc -> this.ID =

Generics and Implementing IComparable

╄→尐↘猪︶ㄣ 提交于 2019-12-04 22:29:42
问题 I am very new to generics and I am trying to write a simple class which will be generic but also allow sorting of some description on a string member variable. At the moment I have a basic class but when I try to implement the interface member CompareTo() I get an error at the top telling me it is not implemented. What is the issue here? using System; namespace GenericsPracticeConsole.Types { class SortableGenericType<T> : IComparable { private T t; private string stringName; public T name {

Implementing IComparable

故事扮演 提交于 2019-12-04 22:09:13
I am implmenting the IComparable to sort like typed objects. My question is why does it cast type person to int32? The array's Sort() seems to cast each type in the array to the type that I am using for comparison. Comparable: public class Person:IComparable { protected int age; public int Age { get; set; } public int CompareTo(object obj) { if(obj is Person) { var person = (Person) obj; return age.CompareTo(person.age); } else { throw new ArgumentException("Object is not of type Person"); } } } } class Program { static void Main(string[] args) { Person p1 = new Person(); Person p2 = new

Internal working of the Sort() and CompareTo() methods

倾然丶 夕夏残阳落幕 提交于 2019-12-04 04:31:57
问题 I've been trying to figure out how the CompareTo() method works internally and I failed. I've searched this site and read some posts, and I think I've seen all there is to see in MSDN about this subject and I just don't seem to get it. An MSDN example: public int CompareTo(object obj) { if (obj == null) { return 1; } Temperature otherTemperature = obj as Temperature; if (otherTemperature != null) { return this.temperatureC.CompareTo(otherTemperature.temperatureC); } else { throw new

Modify List.Contains behavior

╄→гoц情女王★ 提交于 2019-12-04 00:57:30
问题 I have a List<MyObj> with the class MyObj : IComparable . I wrote the method CompareTo in the MyObj class per the IComparable interface, but when I use the List<MyObj>.Contains(myObjInstance) it returns false when it should be true . I'm not sure I'm understanding how I need to proceed to make sure the List uses my custom comparison method when calling then Contains function. Here is my compareTo implementation: #region IComparable Members public int CompareTo(object obj) { MyObj myObj =

F# Set using custom class

十年热恋 提交于 2019-12-03 16:24:04
I'm trying to use Set operations with a class that I have. Every instance of this class has a unique ID. Do I need to implement the System.IComparable interface and if so how would I? type SomeClass(id : int) = member this.ID = id let someSet = Set.of_list [SomeClass(1); SomeClass(2)] let test = someSet.Contains(SomeClass(2)) Here's an implementation that should work: type SomeClass(id : int) = member this.ID = id override this.Equals(o) = match o with | :? SomeClass as sc -> this.ID = sc.ID | _ -> false override this.GetHashCode() = id.GetHashCode() interface System.IComparable with member

Why do I have to overload operators when implementing CompareTo?

こ雲淡風輕ζ 提交于 2019-12-03 08:14:40
问题 Let's say I have a type that implements IComparable. I would have thought it's reasonable to expect that the operators == , != , > , < , >= and <= would "just work" automatically by calling CompareTo but instead I have to override them all if I want to use them. From the language design perspective is there a good reason it was done this way? Are there any cases when you it's genuinely useful for A>B to behave differently to Compare(A,B)>0 ? 回答1: The whole situation is vexing. C# has too many

Why do I have to overload operators when implementing CompareTo?

空扰寡人 提交于 2019-12-02 21:53:01
Let's say I have a type that implements IComparable. I would have thought it's reasonable to expect that the operators == , != , > , < , >= and <= would "just work" automatically by calling CompareTo but instead I have to override them all if I want to use them. From the language design perspective is there a good reason it was done this way? Are there any cases when you it's genuinely useful for A>B to behave differently to Compare(A,B)>0 ? The whole situation is vexing. C# has too many ways to express equality and inequality: the == != > < >= <= operators (which are logically static methods)