Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4000838
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:49:52+00:00 2026-05-20T07:49:52+00:00

this is what i have so far: type u = {str : string} //some

  • 0

this is what i have so far:

type u = {str : string} //some type that has some property str (for simplicity, only one)
type du=
   | A of u
   | B of u // some discriminated union that carries u with it

then, somewhere i have a sequence of du that i am doing distinctBy and the property to do distinct is str.
best i could come up with is this:

Seq.distinctBy (fun d -> match d with (A u|B u) -> u.str)

the code works, but i don’t like having to match on a and b of the discriminated union and would like to replace the match with something.

question is, what? 🙂

EDIT:

in my case, a and b of the discriminated union will always carry same type u with them, one solution is to get rid of du and add it’s string form to type u and simplify this whole mess, but i would like to keep it this way for now because i was going to do matches and such on the a and b…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T07:49:52+00:00Added an answer on May 20, 2026 at 7:49 am

    How about doing the match one time as a property of du?

    type u = {str : string}
    
    type du =
        | A of u
        | B of u with
        member this.U =
            match this with
            (A u | B u) -> u
    
    [A {str="hello"}; B {str="world"}; A {str="world"}]
    |> Seq.distinctBy (fun x -> x.U.str)
    
    //val it : seq<du> = seq [A {str = "hello";}; B {str = "world";}]
    

    However, I have a couple ideas which may model the relationship between u and du better while satisfying your “EDIT” concerns. One way is simply using tuples:

    type u = {str : string}
    
    type du =
        | A
        | B
    
    //focus on type u
    [A, {str="hello"}; B, {str="world"}; A, {str="world"}]
    |> Seq.distinctBy (fun (_,x) -> x.str)
    //val it : seq<du * u> = seq [(A, {str = "hello";}); (B, {str = "world";})]
    
    //focus on type du
    let x = A, {str="hello"}
    match x with
    | A,_ -> "A"
    | B,_ -> "B"
    //val it : string = "A"
    

    Another way is to switch it around and add du to u:

    type du =
        | A
        | B
    
    type u = { case : du; str : string}
    
    //focus on type u
    [{case=A; str="hello"}; {case=B; str="world"}; {case=A; str="world"}]
    |> Seq.distinctBy (fun x -> x.str)
    //val it : seq<u> = seq [{case = A;
    //                        str = "hello";}; {case = B;
    //                                          str = "world";}]
    
    //focus on type du
    let x = {case=A; str="hello"}
    match x with
    | {case=A} -> "A"
    | {case=B} -> "B"
    //val it : string = "A"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Has anyone done this / have some example code?
I have this function that converts an integer to a std::string: std::string intToStr(const int
So I have a couple of functions that work with a string type I
I have a script that appends some rows to a table. One of the
So far i have this code: function changeGeoLoc($a,$b,$c){ echo <ul style='list-style-type: none; display:inline;'>; while(list(,$geoloc)
This is what I have so far: myArray.map!{ rand(max) } Obviously, however, sometimes the
Running ipconfig /all shows a Teredo Tunneling Pseudo-Interface. What is that? Does this have
I have this code in jQuery, that I want to reimplement with the prototype
I have this gigantic ugly string: J0000000: Transaction A0001401 started on 8/22/2008 9:49:29 AM
I have this string 'john smith~123 Street~Apt 4~New York~NY~12345' Using JavaScript, what is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.