Reputation: 428
I'm trying to implement a OrderedSet in F# (a set where you can get items by order of insertion). This is my first naive attempt at it (I still have to deal with possible duplicates in the IEnumerable passed to the constructor. The comparator is also still missing).
I don't think any of the above matters, though. I've been trying to solve the problem and, as far as I could gather from my research, it seems the issue is about indentation or lack of parentheses somewhere.
If I comment out my attempt at implement ISet, there is no issue, which leads me to believe there is something wrong with that part specifically. Here's the code:
open System
open System.Collections.Generic;
type public OrderedSet<'T> public (collection : IEnumerable<'T> option) as this =
let mutable _set = SortedSet<int * 'T>()
do
if Option.isSome(collection) then
let mapper (idx: int) (elem: 'T) = (idx, elem)
do _set <- SortedSet<int * 'T>(Seq.mapi mapper collection.Value)
interface ISet<'T> with
member this.Count
with get() = this._set.Count
Upvotes: 0
Views: 51
Reputation: 17038
I only had to make two changes:
if
under the do
.this.
in front of _set
, because it's let
-bound, not a class member.You also have to finish implementing ISet
, of course. Here's the resulting code:
open System
open System.Collections.Generic;
type public OrderedSet<'T> public (collection : IEnumerable<'T> option) as this =
let mutable _set = SortedSet<int * 'T>()
do
if Option.isSome(collection) then
let mapper (idx: int) (elem: 'T) = (idx, elem)
do _set <- SortedSet<int * 'T>(Seq.mapi mapper collection.Value)
interface ISet<'T> with
member this.Count
with get() = _set.Count
Upvotes: 1