- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
以下函数:
let twoInputs x y =
let sum = x + y
let product a = sum * a
product
具有以下类型:
val twoInputs : x:int -> y:int -> (int -> int)
这是完全合理的,我知道为什么会这样。但是为什么这个功能:
let oneInput = twoInputs 1
是类型 val oneInput : (int -> int -> int)
?
不应该是 int -> (int -> int)
吗?
另外,我认为上面的函数应该符合Associative property ,所以 int -> int -> (int -> int)
和 int -> int -> int -> int
应该没有区别。如果是这样,为什么不直接将后者指定为 twoInputs
的函数类型?
最佳答案
括号表示“FsharpFunc<_> 类型的值”,而没有括号表示“真正的 CLR 方法”。在您的示例中,twoInput
被编译为真正的 CLR 方法,但返回类型为 FSharpFunc<_>
的值,因此它的类型。但是你的oneInput
被编译为 FSharpFunc<_>
类型的类字段,因此它的类型。
你实际上可以实现相同的效果(即将 true 方法转换为值),即使没有柯里化(Currying),非常简单:
> let twoInputs x y =
> let sum = x + y
> let product a = sum * a
> product
> let twoInputsAgain = twoInputs
val twoInputs : x:int -> y:int -> (int -> int)
val twoInputsAgain : (int -> int -> int -> int)
发生这种情况是因为 CLR 不支持“分配方法”的概念,因此 F# 必须通过声明 twoInputsAgain
来编译它作为 FSharpFunc<_>
类型的字段然后为其分配一个继承自 FSharpFunc<_>
的类的实例并调用 twoInputs
什么时候Invoke
d.
如果你将它反编译为 C#(我使用 ILSpy),这就是你所看到的:
static $Program()
{
$Program.twoInputsAgain@11 = new Program.twoInputsAgain@11();
}
internal class twoInputsAgain@11 : OptimizedClosures.FSharpFunc<int, int, FSharpFunc<int, int>>
{
public override FSharpFunc<int, int> Invoke(int x, int y)
{
return Program.twoInputs(x, y);
}
}
最后,我想指出的是,这种区别在实践中并不重要,除非你练习一些真正的黑魔法,所以你不应该担心它。
关于f# - 为什么 F# currying "flattens"函数类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30915485/
torch.flatten() 和 torch.nn.Flatten() 有什么区别? 最佳答案 扁平化在 PyTorch 中以三种形式提供 作为张量方法(oop 风格) torch.Tensor.f
我有以下(不完整的)功能: /* Populates char* name with the named location of the ith (flat) element * of an arr
我正在尝试使用 Keras 和基于 Marcin 的 PS3 示例的 Tensorflow 后端构建一个可变长度序列分类模型:https://stackoverflow.com/a/42635571/
我开始使用 keras 和 tensorflow 进行深度学习。在最初阶段,我有一个疑问。当我使用 tf.contrib.layers.flatten (Api 1.8) 用于拼合图像(也可以是多 c
鉴于以下SVG: 是否有任何工具可以通过变换自动更新路径点,并从组元素中删除变换? 我有一个SVG应用程序(草图),它使用转换属性(如果您
我目前有一个 Python 脚本,它使用 flatten 函数来展平 JSON 对象,然后我将使用 json_normalize将扁平的 JSON 转换为 DataFrame。 import pand
我注意到Option.flatten是defined,如下所示: def flatten [B](隐式ev:A To) with Serializable ... implicit def conf
我希望有人经历过相同的过程并可以帮助我看看以下情况是否可能。 我目前构建了一个从 S3 存储桶进行复制的管道。该存储桶包含大量文件夹。 Azure 数据工厂是否有办法在从 S3 存储桶复制数据时忽略文
我希望有人经历过相同的过程并可以帮助我看看以下情况是否可能。 我目前构建了一个从 S3 存储桶进行复制的管道。该存储桶包含大量文件夹。 Azure 数据工厂是否有办法在从 S3 存储桶复制数据时忽略文
这个问题在这里已经有了答案: How to flatten tree via LINQ? (15 个答案) 关闭 6 年前。 我有一个嵌套类: class Item {
我有下表: group_id amount type 1 10 1 1 2 1 1 5 2 1
我有兴趣实现一个通用的 flatten一个容器的容器 为了简化,我会使用自己的容器。主要原因是标准容器接收分配器的附加模板参数,这对我编写操作来说更复杂。因此,考虑容器 Vector , List等让
给定如下列表(我的实际列表比这个长,列表中至少有100个子列表): [[135, "Apple Banana"], [136, "Peach Watermelon"], [137, "Orange S
这个问题在这里已经有了答案: Flatten an irregular (arbitrarily nested) list of lists (51 个答案) 关闭 5 年前。 我有一个不规则的数据
我创建了一个函数,它接受输入变量的向量,并为每组输入返回一个元胞数组。最终输出变量 (out) 似乎由一个包含两个 1x5 单元格的 2x1 单元格组成。我在下面提供了截图: 我只是想弄清楚如何将元胞
我正在尝试编写一个方法,它将接受一个由字符 0、1 和 ? 组成的字符串,并返回由原始字符串表示的一组字符串,将 ? 作为通配符,可以是 0 或 1。例如,如果输入是 "011?0",则输出应该是 [
假设我有一堆从 Pig UDF 生成的数据包,其中包含多个字符串元组。我怎样才能将它们全部从数据包中拉出来并简单地使每个字符串成为它自己的数据“行”。 databags = FOREACH 数据生成
所以我的目标是: fruitColourMapping = [{'apple': 'red'}, {'banana': 'yellow'}] 到 finalMap = {'apple': 'red',
我需要使用 Serialize 和 Deserialize 的自定义实现,但我不知道如何做 #[serde(flatten)] 有没有人知道? 注意:我知道我可以将较低元素的完整实现完全重写到较高元素
我有一个 FLATTEN LAMBDA 函数,可以将数组中的数据展平。这很好用,但我想集成另一个数组参数,这样我就可以使用不连续的范围。 在我的示例中,A1:B6 范围位于 array 中并返回展平数
我是一名优秀的程序员,十分优秀!