Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 在需要Func的位置接受FSharpFunc_.net_F#_Extension Methods_Func - Fatal编程技术网

.net 在需要Func的位置接受FSharpFunc

.net 在需要Func的位置接受FSharpFunc,.net,f#,extension-methods,func,.net,F#,Extension Methods,Func,如中所述,预期为Func的方法将不接受F#函数值 重载方法以使其接受F#函数值的好方法是什么?下面是一个将F#函数值传递给IEnumerable的示例。All: open System.Linq open IEnumerableAllFSharpFunc let seqA = seq { 1..10 } let abc n = n > 0 seqA.All abc |> printfn "%A" using System; using System.Collections.G

如中所述,预期为
Func
的方法将不接受F#函数值


重载方法以使其接受F#函数值的好方法是什么?

下面是一个将F#函数值传递给IEnumerable的示例。All:

open System.Linq
open IEnumerableAllFSharpFunc

let seqA = seq { 1..10 }

let abc n = n > 0

seqA.All abc |> printfn "%A"
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.FSharp.Core;

namespace IEnumerableAllFSharpFunc
{
    public static class Utils
    {
        public static bool All<T>(this IEnumerable<T> seq, FSharpFunc<T, bool> pred)
        {
            var converter = FSharpFunc<T, bool>.ToConverter(pred);

            Func<T, bool> func = (elt) => converter(elt);

            return seq.All(func);
        }
    }
}
IEnumerable.All
上给定此扩展方法:

open System.Linq
open IEnumerableAllFSharpFunc

let seqA = seq { 1..10 }

let abc n = n > 0

seqA.All abc |> printfn "%A"
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.FSharp.Core;

namespace IEnumerableAllFSharpFunc
{
    public static class Utils
    {
        public static bool All<T>(this IEnumerable<T> seq, FSharpFunc<T, bool> pred)
        {
            var converter = FSharpFunc<T, bool>.ToConverter(pred);

            Func<T, bool> func = (elt) => converter(elt);

            return seq.All(func);
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用Microsoft.FSharp.Core;
命名空间IEnumerablellfsharpfunc
{
公共静态类Utils
{
公共静态bool All(此IEnumerable seq,FSharpFunc pred)
{
var转换器=FSharpFunc.ToConverter(pred);
Func Func=(elt)=>转换器(elt);
返回顺序全部(func);
}
}
}

欢迎使用更优雅的方法。:-)

仅仅创建一个Func还不够吗

let doSomethingWithFunc (f : System.Func<_,_>) =
    42

let doSomethingWithFSharpFunc (f : 'a -> 'b) =
    System.Func<_,_>(f) |> doSomethingWithFunc

(fun x -> 42) |> doSomethingWithFSharpFunc
let doSomethingWithFunc(f:System.Func)=
42
让doSomethingWithFSharpFunc(f:'a->'b)=
System.Func(f)|>doSomethingWithFunc
(乐趣x->42)|>doSomethingWithFSharpFunc

我知道这不是你要问的,但与其直接尝试从用C#编写的代码中支持F#(我得到的印象是你正在尝试这样做),不如提供一个小型的适配器模块,使F#的功能组合更容易

有很多这样的例子,例如,它提供了一些函数,使得从F#使用起来更容易

例如,如果您想从F#访问,您可以编写一个小的适配器函数,例如

让所有f(s:'a seq)=s.all(fun x->f x)
您可以这样使用:-

seqA |> all abc
但是,对于
All
,您可以使用内置的F#函数:

seqA |> Seq.forall abc

使用初始问题中的代码,最简单的解决方案是创建预期委托的实例(本例中为
Func
),并将函数值作为参数传递

let seqA = { 1..10 }
let abc = fun n -> n > 0
seqA.All (Func<_,_> abc)
让seka={1..10}
让abc=fun->n>0
序号全部(职能abc)

Patryk在他的评论中注意到了这种语法,但我想我应该对真正发生的事情进行解释。

这里是另一种方法:

open System
open System.Collections.Generic
open System.Linq

type IEnumerable<'T> with
    member this.All(pred: 'T -> bool) = this.All(Func<_,_> pred)

let even n = n % 2 = 0

let seqA = seq { 0..2..10 }

seqA.All(even) |> printfn "%A"
开放系统
open System.Collections.Generic
开放系统
类型IEnumerable bool)=此.All(Func pred)
设偶数n=n%2=0
设seka=seq{0..2..10}
seka.All(偶数)|>printfn“%A”

@PatrykĆwiek:这很有效,但实际上不是演员阵容。它相当于
seka.All(newfunc(abc))
。F中的强制转换形式为
x'T
,其中
:>
:?>