我怎样才能加入F#?

时间:2022-02-26 03:13:13

I have a lambda join in C# which looks like this:

我在C#中有一个lambda连接,看起来像这样:

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

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

After execution res contains 3 which is common for both arrays.

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

I want to make the exact same lambda join in F#, and try:

我想在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)

But the compiler says:

但是编译器说:

Unexpected symbol ',' in lambda expression. Expected '->' or other token.

lambda表达式中的意外符号','。预期' - >'或其他令牌。

The error is the comma after the first parameter arrY.

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

Can you tell me how I can get it to work (as a lambda expression)?

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

3 个解决方案

#1


3  

this will work for me in F#-interactive (and is the direct translation from your C# code):

这对我来说在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))

after executing res will look like this:

执行res后会看起来像这样:

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

remarks

if you like you can write

如果你愿意,你可以写

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

as @RCH proposed too

正如@RCH提出的那样

#2


1  

Note that there are at least two ways to do this using the F# core lib.

请注意,使用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
    }

#3


0  

May I boldly suggest the following:

我可以大胆地提出以下建议:

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

or if going for some total "obfuscation style":

或者如果要采取一些完整的“混淆风格”:

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

I guess the res'-version is not recommended...

我想不建议使用res'版本......

:-)

:-)

#1


3  

this will work for me in F#-interactive (and is the direct translation from your C# code):

这对我来说在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))

after executing res will look like this:

执行res后会看起来像这样:

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

remarks

if you like you can write

如果你愿意,你可以写

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

as @RCH proposed too

正如@RCH提出的那样

#2


1  

Note that there are at least two ways to do this using the F# core lib.

请注意,使用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
    }

#3


0  

May I boldly suggest the following:

我可以大胆地提出以下建议:

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

or if going for some total "obfuscation style":

或者如果要采取一些完整的“混淆风格”:

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

I guess the res'-version is not recommended...

我想不建议使用res'版本......

:-)

:-)