4 people like it.

ad hoc tagged sets

Given an ordered set S and an arbitrary set T, any function T -> S induces a natural ordering on T. We use this idea and the infrastructure provided by Tagged.Set to quickly construct sets over arbitrary types with custom comparison rules.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
#r "FSharp.Powerpack.dll"

open System.Collections.Generic
open Microsoft.FSharp.Collections

module TagSet =

    let create (f : 'T -> 'D) (input : #seq<'T>) =
        let comparer =
            {
                new IComparer<'T> with
                    member __.Compare(x,y) = compare (f x) (f y)
            }

        Tagged.Set<_,_>.Create(comparer, input)

// example 1
let modSet = TagSet.create (fun x -> x % 5) <| seq { 1 .. 1000 }

modSet.ToList()

// example 2
open System
open System.Reflection

let typeSet = TagSet.create (fun (t: Type) -> t.GUID) <| Assembly.GetExecutingAssembly().GetTypes()

typeSet.ToList()
namespace System
namespace System.Collections
namespace System.Collections.Generic
namespace Microsoft
namespace Microsoft.FSharp
namespace Microsoft.FSharp.Collections
val create : f:('T -> 'D) -> input:#seq<'T> -> 'b (requires comparison)

Full name: Script.TagSet.create
val f : ('T -> 'D) (requires comparison)
val input : #seq<'T>
Multiple items
val seq : sequence:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Core.Operators.seq

--------------------
type seq<'T> = IEnumerable<'T>

Full name: Microsoft.FSharp.Collections.seq<_>
val comparer : IComparer<'T>
type IComparer<'T> =
  member Compare : x:'T * y:'T -> int

Full name: System.Collections.Generic.IComparer<_>
val x : 'T
val y : 'T
val compare : e1:'T -> e2:'T -> int (requires comparison)

Full name: Microsoft.FSharp.Core.Operators.compare
Multiple items
module Set

from Microsoft.FSharp.Collections

--------------------
type Set<'T (requires comparison)> =
  interface IComparable
  interface IEnumerable
  interface IEnumerable<'T>
  interface ICollection<'T>
  new : elements:seq<'T> -> Set<'T>
  member Add : value:'T -> Set<'T>
  member Contains : value:'T -> bool
  override Equals : obj -> bool
  member IsProperSubsetOf : otherSet:Set<'T> -> bool
  member IsProperSupersetOf : otherSet:Set<'T> -> bool
  ...

Full name: Microsoft.FSharp.Collections.Set<_>

--------------------
new : elements:seq<'T> -> Set<'T>
val modSet : obj

Full name: Script.modSet
module TagSet

from Script
val x : int
namespace System.Reflection
val typeSet : obj

Full name: Script.typeSet
val t : Type
type Type =
  inherit MemberInfo
  member Assembly : Assembly
  member AssemblyQualifiedName : string
  member Attributes : TypeAttributes
  member BaseType : Type
  member ContainsGenericParameters : bool
  member DeclaringMethod : MethodBase
  member DeclaringType : Type
  member Equals : o:obj -> bool + 1 overload
  member FindInterfaces : filter:TypeFilter * filterCriteria:obj -> Type[]
  member FindMembers : memberType:MemberTypes * bindingAttr:BindingFlags * filter:MemberFilter * filterCriteria:obj -> MemberInfo[]
  ...

Full name: System.Type
property Type.GUID: Guid
type Assembly =
  member CodeBase : string
  member CreateInstance : typeName:string -> obj + 2 overloads
  member EntryPoint : MethodInfo
  member Equals : o:obj -> bool
  member EscapedCodeBase : string
  member Evidence : Evidence
  member FullName : string
  member GetCustomAttributes : inherit:bool -> obj[] + 1 overload
  member GetCustomAttributesData : unit -> IList<CustomAttributeData>
  member GetExportedTypes : unit -> Type[]
  ...

Full name: System.Reflection.Assembly
Assembly.GetExecutingAssembly() : Assembly
Raw view Test code New version

More information

Link:http://fssnip.net/cD
Posted:12 years ago
Author:Eirik Tsarpalis
Tags: tagged sets , comparable types