F# Seq.fold 智能感知
我正在学习 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.fold
和 Aggregate
之间建立类比,但也许这是错误的做法。我知道 ->
定义了一个函数(或签名?),但除此之外,我很难阅读出现的内容。
需要明确的是,我不需要如何使用折叠的示例;我不需要一个例子来说明如何使用折叠。我只是在寻找 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 F# 和许多其他函数式语言中,
'a ->; 'b-> 'c-> 'd
是采用'a
、'b
、'c
类型参数作为输入*的函数类型,并且返回一个'd
。 So在 C# 中意味着类型的函数
,
在 C# 中意味着 * 类型的函数
:暂时忽略柯里化。
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
. Someans, in C#, a function of type
and
means, in C#, a function of type
*: currying is ignored for the moment.
Fold 是一个函数,它接受一个函数 ( fun ),该函数采用 'State 类型的元素和 T 类型的元素并返回 'State 类型的值 (
('State -> 'T -> 'State) ),一个 'State 类型的值和一个 T 类型的元素序列,并返回一个 `State 类型的值
基本上,在 F# 交互中键入以下内容
,您将看到
这意味着 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 `StateBasically, type the following in the F# interactive
and you will see
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. )