Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

f# set and map equality

Tags:

equality

set

map

f#

How does set1 = set2 for F# immutable sets and maps work? Will they do proper set/map equality?

The documentation says:

Immutable sets based on binary trees, where comparison is the F# structural comparison function, potentially using implementations of the IComparable interface on key values.

They are based on binary trees, so might not the exact form depending on the order elements were inserted? So naively using the F# structural comparison function would not work...

I can't find anything about it in the documentation. Have I just missed it?

like image 520
johv Avatar asked Dec 05 '13 18:12

johv


1 Answers

Yes, it will work properly, apparently.

Looking at the source, Equals is overridden and uses GetEnumerator and checks that these return the same elements in the same order. These should be in-order as seen in this question, since the doc for Set.toSeq says: "An ordered sequence of the elements of set."

This is also mentioned in the book "F# for Scientists" by Jon Harrop and Don Syme:

quote from "F# for Scientists"

like image 193
johv Avatar answered Sep 19 '22 00:09

johv