简体   繁体   English

如何在f#中解包列表中的联合值

[英]how to unwrap union value in list in f#

You know that to unwrap a value of a single union type you have to do this: 您知道要解开单个联合类型的值,您必须这样做:

type Foo = Foo of int*string

let processFoo foo =
    let (Foo (t1,t2)) = foo
    printfn "%A %A" t1 t2 

but my question is: if there a way to do that for lists ?: 但我的问题是:如果有办法为列表做到这一点?:

let processFooList (foolist:Foo list )  =
    let ??? = foolist // how to get a int*string list
    ...

thanks. 谢谢。

最好的方法是使用一个与List.map结合的函数

let processFooList (foolist:Foo list )  = foolist |> List.map (function |Foo(t1,t2)->t1,t2)

There's no predefined active pattern for converting the list from Foo to int * string , but you could combine the Named Pattern §7.2 (deconstruct single case union) with the projection into your own single case Active Pattern §7.2.3 . 没有用于将列表从Foo转换为int * string的预定义活动模式,但您可以将命名模式§7.2 (解构单例并集)与投影组合到您自己的单例ActivePattern§7.2.3中

let asTuple (Foo(t1, t2)) = t1, t2      // extract tuple from single Foo
let (|FooList|) =  List.map asTuple     // apply to list

Use as function argument: 用作函数参数:

let processFooList (FooList fooList) =  // now you can extract tuples from Foo list
    ...                                 // fooList is an (int * string) list

Use in let-binding: 用于绑定:

let (FooList fooList) = 
    [ Foo(1, "a"); Foo(2, "b") ]
printfn "%A" fooList                    // prints [(1, "a"); (2, "b")]

Distilling/summarising/restating/reposting the other two answers, your cited line: 提炼/总结/重述/重新发布其他两个答案,您的引用行:

let ??? = foolist // how to get a int*string list

Can become: 可以变成:

let ``???`` = foolist |> List.map (function |Foo(x,y) -> x,y)

If you're writing a transformation, you can do the matching in the params having defined an Active Pattern using either of the following: 如果您正在编写转换,则可以使用以下任一方法在已定义活动模式的参数中进行匹配:

let (|FooList|) = List.map <| fun (Foo(t1, t2)) -> t1,t2
let (|FooList|) = List.map <| function |Foo(t1, t2) -> t1,t2

which can then be consumed as follows: 然后可以按如下方式消费:

let processFooList (fooList:Foo list )  =
    // do something with fooList

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM