F# match question -


this have far:

type u = {str : string} //some type has property str (for simplicity, one) type du=    | of u    | b of u // discriminated union carries u 

then, somewhere have sequence of du doing distinctby , property distinct str. best come this:

seq.distinctby (fun d -> match d (a u|b u) -> u.str) 

the code works, don't having match on , b of discriminated union , replace match something.

question is, what? :)

edit:

in case, , b of discriminated union carry same type u them, 1 solution rid of du , add it's string form type u , simplify whole mess, keep way because going matches , such on , b...

how doing match 1 time property of du?

type u = {str : string}  type du =     | of u     | b of u     member this.u =         match         (a u | b u) -> u  [a {str="hello"}; b {str="world"}; {str="world"}] |> seq.distinctby (fun x -> x.u.str)  //val : seq<du> = seq [a {str = "hello";}; b {str = "world";}] 

however, have couple ideas may model relationship between u , du better while satisfying "edit" concerns. 1 way using tuples:

type u = {str : string}  type du =     |     | b  //focus on type u [a, {str="hello"}; b, {str="world"}; a, {str="world"}] |> seq.distinctby (fun (_,x) -> x.str) //val : seq<du * u> = seq [(a, {str = "hello";}); (b, {str = "world";})]  //focus on type du let x = a, {str="hello"} match x | a,_ -> "a" | b,_ -> "b" //val : string = "a" 

another way switch around , add du u:

type du =     |     | 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 : seq<u> = seq [{case = a; //                        str = "hello";}; {case = b; //                                          str = "world";}]  //focus on type du let x = {case=a; str="hello"} match x | {case=a} -> "a" | {case=b} -> "b" //val : string = "a" 

Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -