F#匹配问题

F#匹配问题,f#,F#,这就是我到目前为止所做的: 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 然后,在某个地方,我有一个du序列,我正在做distinctBy,要做distinct的属性是str。 我能想到的最好的办

这就是我到目前为止所做的:

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
然后,在某个地方,我有一个du序列,我正在做distinctBy,要做distinct的属性是str。 我能想到的最好的办法是:

Seq.distinctBy (fun d -> match d with (A u|B u) -> u.str)
代码是有效的,但我不喜欢必须匹配受歧视的联合的a和b,我想用一些东西来替换匹配

问题是,什么?:)

编辑:


在我的例子中,有区别的并集的a和b总是带有相同的u型,一个解决方案是去掉du,将它的字符串形式添加到u型,简化整个混乱,但我现在想保持这种方式,因为我要在a和b上进行匹配等等。

你真的不能像你描述的那样简化它,但你可以简化它。谢谢@Tomas Petricek

输出

[| A {str = "A";}; B {str = "B";} |]

把比赛作为杜的财产进行一次怎么样

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";}]
type u={str:string}
du型=
|美国
|B的u与
各位议员:=
与此匹配
(A u | B u)->u
[A{str=“hello”};B{str=“world”};A{str=“world”}]
|>顺序区分(乐趣x->x.U.str)
//val-it:seq=seq[A{str=“hello”};B{str=“world”}]
然而,我有一些想法,可以更好地模拟u和du之间的关系,同时满足您的“编辑”关注。一种方法是简单地使用元组:

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"
type u={str:string}
du型=
|A
|B
//关注u型
[A,{str=“hello”};B,{str=“world”};A,{str=“world”}]
|>顺序区分(乐趣(ux)->x.str)
//val-it:seq=seq[(A,{str=“hello”;});(B,{str=“world”;}]
//关注du型
设x=A,{str=“hello”}
将x与
|A,->“A”
|B,->“B”
//val it:string=“A”
另一种方法是切换并将du添加到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"
du型=
|A
|B
类型u={case:du;str:string}
//关注u型
[{case=A;str=“hello”};{case=B;str=“world”};{case=A;str=“world”}]
|>顺序区分(乐趣x->x.str)
//val it:seq=seq[{case=A;
//str=“hello”;};{case=B;
//str=“世界”;}]
//关注du型
设x={case=A;str=“hello”}
将x与
|{case=A}->“A”
|{case=B}->“B”
//val it:string=“A”
我对F#有点陌生,但希望这会有所帮助。在我看来,活动模式可能会让你的生活更轻松,因为它可以减少你需要在模式匹配中键入的内容。您可以在其位置使用活动模式AorB,而不是使用A | B

type u = { str : string }

type du = 
    | A of u
    | B of u

let (|AorB|) (v:du) =
    match v with
        | A a -> a
        | B b -> b

[A { str = "A" }; B { str = "B"}; A { str = "A"}]
    |> Seq.distinctBy (fun d -> 
                    match d with 
                        | AorB s -> s)
    |> Seq.iter (fun i -> match i with AorB c -> printfn "%s" c.str)
加上斯蒂芬的话,最后的表达式就可以写出来了

[A { str = "A" }; B { str = "B"}; A { str = "A"}]
|> Seq.distinctBy (|AorB|)
|> Seq.iter (fun i -> match i with AorB c -> printfn "%s" c.str)

我相信受歧视的工会案例必须是大写的-不知道为什么会是这样。@Jimmy,你是对的,我会编辑它,谢谢你可以使用
fun(A u | B u)
而不是
function A u | B u
:-)@Tomas-Oh F#你什么时候能停止让我吃惊?嘿@LLama-它变得更好了,当使用这样的活动模式时,您可以执行
。|>Seq.distinctBy(| AorB |)…
先生,你这一次真的让我大吃一惊,哇!谢谢你的小费!呵呵——是的,谁知道编程语言会充满如此真实的乐趣!我本科时学过数学,现在是一名软件工程师:但直到我发现F#,我才在CS中体验到和我在数学中一样的惊奇(正是因为作为一种语言,F#减少了数学思维和表达之间的阻抗失配)!我想就我的问题而言,你的第一个答案最合适,我仍然需要考虑一下我的模型,但是拥有一处房产的想法非常好。
[A { str = "A" }; B { str = "B"}; A { str = "A"}]
|> Seq.distinctBy (|AorB|)
|> Seq.iter (fun i -> match i with AorB c -> printfn "%s" c.str)