wolf
wolf

Reputation: 419

Accessing tuple item from discriminated union

I have type called type x and it has following members in the form of discriminated union

type Info= Empty | Value of int | Pair of (int * int)

I have a function which takes two arguments and filter the list according to comparisons made by n. I cannot figure out the last part. How can I compare each value of Pair with my n value in that function?

let filterInfo (n:int) (xs:Info list) = xs |>  List.filter (fun (x) -> x <> Empty && x > Value n && // ) 

Upvotes: 1

Views: 95

Answers (2)

Julia
Julia

Reputation: 2085

I ended up adding a read-only property that pattern-matches on this:

type Thing =
    | Sphere of Sphere * Surface
    | Plane of Plane * Surface

    member this.surface =
        match this with
        | Sphere(_, surface) -> surface
        | Plane(_, surface) -> surface

Upvotes: 0

Chad Gilbert
Chad Gilbert

Reputation: 36375

You can create a function that compares a single Info object by using pattern matching. Something like this should suffice:

let compareInfo (n:int) (info:Info) =
        match info with
        | Empty -> false
        | Value x -> n > x
        | Pair (a, b) -> ...

You can call it by curring n from your filter call:

let filterInfo (n:int) (xs:Info list) = xs |>  List.filter (compareInfo n) 

Upvotes: 2

Related Questions