F# Seq.fold 智能感知

发布于 2024-11-03 12:58:47 字数 686 浏览 5 评论 0原文

我正在学习 F#,正在努力解析 Seq.fold 中出现的智能感知:

val fold : ('State -> 'T -> 'State) -> 'State -> seq<'T> -> 'State

在 C# 中,我习惯了 Aggregate 扩展方法,并且可以很好地理解C# 声明:

(extension) TAccumulate Aggregate<TSource, TAccumulate>(this IEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> func);

因此,为了理解/推导本例中的 F# 语法,我一直在尝试在 Seq.foldAggregate 之间建立类比,但也许这是错误的做法。我知道 -> 定义了一个函数(或签名?),但除此之外,我很难阅读出现的内容。

需要明确的是,我不需要如何使用折叠的示例;我不需要一个例子来说明如何使用折叠。我只是在寻找 Seq.fold 智能感知中使用的 F# 语法的细分。谢谢。

I'm learning F# and am struggling to parse the intellisense that appears for Seq.fold:

val fold : ('State -> 'T -> 'State) -> 'State -> seq<'T> -> 'State

In C# I'm accustomed to the Aggregate extension method and can understand perfectly fine the C# declaration:

(extension) TAccumulate Aggregate<TSource, TAccumulate>(this IEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> func);

So in order to understand/deduce the F# syntax in this case, I've been trying to establish an analogy between Seq.fold and Aggregate but perhaps this is the wrong approach. I understand that -> defines a function (or the signature?), but beyond that I'm having a hard time reading what appeared.

To be clear, I don't need an example of how to use folding; I'm simply looking for a breakdown of the the F# syntax used in the Seq.fold intellisense. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一桥轻雨一伞开 2024-11-10 12:58:47

在 F# 和许多其他函数式语言中,'a ->; 'b-> 'c-> 'd 是采用 'a'b'c 类型参数作为输入*的函数类型,并且返回一个'd。 So

  ('State -> 'T -> 'State)
//  1st      2nd   output

在 C# 中意味着类型的函数

State func<State,T>(State firstInput, T secondInput);

  ('State -> 'T -> 'State) -> 'State -> seq<'T> -> 'State
//1st input (a function)      2nd       3rd        output

在 C# 中意味着 * 类型的函数

State func<State,T>(Func<State, T, State> firstInput,
                    State secondInput, 
                    IEnumerable<T> thirdInput);

:暂时忽略柯里化。

In F# and many other functional languages, 'a -> 'b -> 'c -> 'd is the type of function which takes parameters of type 'a, 'b, 'c as input*, and returns a 'd. So

  ('State -> 'T -> 'State)
//  1st      2nd   output

means, in C#, a function of type

State func<State,T>(State firstInput, T secondInput);

and

  ('State -> 'T -> 'State) -> 'State -> seq<'T> -> 'State
//1st input (a function)      2nd       3rd        output

means, in C#, a function of type

State func<State,T>(Func<State, T, State> firstInput,
                    State secondInput, 
                    IEnumerable<T> thirdInput);

*: currying is ignored for the moment.

醉酒的小男人 2024-11-10 12:58:47

Fold 是一个函数,它接受一个函数 ( fun ),该函数采用 'State 类型的元素和 T 类型的元素并返回 'State 类型的值 ( ('State -> 'T -> 'State) ),一个 'State 类型的值和一个 T 类型的元素序列,并返回一个 `State 类型的值

基本上,在 F# 交互中键入以下内容

let sum x y = x+y;;

,您将看到

val sum : int -> int -> int

这意味着 sum 采用 int 和 int 并返回 int 。如果你能理解这一点,你就可以理解任何函数签名。

另请注意 ->是右结合的,当您将函数视为接受一个值并将其转换为另一个值的转换时,这种符号会变得更加明显。

所以上面其实就是int -> (int -> int ) 一个接受 int 并返回一个接受 int 并返回 int 的函数(称为柯里化。)

fold is a function which accepts a function ( fun ) taking a element of type 'State and element of type T and returning value of type 'State ( ('State -> 'T -> 'State) ), a value of type 'State and a sequence of elements of type T and returns a value of type `State

Basically, type the following in the F# interactive

let sum x y = x+y;;

and you will see

val sum : int -> int -> int

That means sum take int and int and return an int. If you can understand this, you can understand any function signature.

Also note that -> is right associative and the notation will become more apparent when you see functions as transformations accepting one value and transforming it in to another.

So the above is actually int -> (int -> int ) a function taking int and returning a function which takes a int and returns int ( referred to as currying. )

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文