我怎样才能在 F# 中加入这个?

How can I make this Join in F#?

我在 C# 中有一个 lambda 连接,如下所示:

int[] arrX = { 1, 2, 3 };
int[] arrY = { 3, 4, 5 };

var res = arrX.Join(arrY, x => x, y => y, (x, y) => x);

执行后 res 包含 3,这对两个数组都是通用的。

我想在 F# 中创建完全相同的 lambda 连接,然后尝试:

let arrX = [| 1; 2; 3 |]
let arrY = [| 3; 4; 5 |]

let res = arrX.Join(fun arrY, fun x -> x, fun y -> y, fun (x, y) -> x)

但是编译器说:

lambda 表达式中出现意外符号“,”。应为“->”或其他标记。

错误是第一个参数arrY后面的逗号。

你能告诉我如何让它工作(作为 lambda 表达式)吗?

这对我来说适用于 F#-interactive(并且是从您的 C# 代码直接翻译而来):

open System
open System.Linq

let arrX = [| 1; 2; 3 |]
let arrY = [| 3; 4; 5 |]

let res = arrX.Join(arrY, Func<_,_>(id), Func<_,_>(id), (fun x _ -> x))

执行后 res 将如下所示:

> res;;
val it : Collections.Generic.IEnumerable<int> = seq [3]

备注

喜欢可以写

let res = arrX.Join(arrY, (fun x -> x), (fun x -> x), fun x _ -> x)

正如@RCH 所提议的那样

请注意,使用 F# 核心库至少有两种方法可以做到这一点。

let arrX = [| 1; 2; 3 |]
let arrY = [| 3; 4; 5 |]

//method 1 (does not preserve order)
let res1 = Set.intersect (set arrX) (set arrY)

//method 2
let res2 = 
    query { 
        for x in arrX do
        join y in arrY on (x = y)
        select x
    }

请允许我大胆建议如下:

open System
open System.Linq

let arrX = [| 1; 2; 3 |]
let arrY = [| 3; 4; 5 |]

let res = Set.intersect (Set.ofArray arrX) (Set.ofArray arrY) |> Set.toArray

或者如果总计 "obfuscation style":

let res' = arrX |> Set.ofArray |> Set.intersect <| (Set.ofArray <| arrY) |> Set.toArray

我猜不建议使用 res'-版本...

:-)