F#管道第一个参数

F# pipe first parameter

是否可以将第一个参数通过管道传递给多参数函数? 例如

date = "20160301"

是否可以将 date 通过管道传输到

DateTime.ParseExact(    , "yyyyMMDD", CultureInfo.InvariantCulture)

来自this

Methods usually use the tuple form of passing arguments. This achieves a clearer result from the perspective of other .NET languages because the tuple form matches the way arguments are passed in .NET methods.

因为你不能柯里化一个直接接受元组的函数,你必须包装 ParseExact 来做到这一点:

let parseExact date = DateTime.ParseExact(date, "yyyyMMDD", CultureInfo.InvariantCulture)

可能不相关,但输出参数可以这样绑定:

let success, value = Double.TryParse("2.5")

请注意 TryParse 接受第二个参数为 byref 的元组。

正如@yuyoyuppe 在 中解释的那样,您不能直接通过管道输入 ParseExact,因为它是一种方法,因此不会柯里化。

定义柯里化的 Adapter 函数通常是一个不错的选择,但如果您只在本地需要它,您也可以通过管道输入匿名函数:

let res =
    date |> (fun d -> DateTime.ParseExact(d, "yyyyMMdd", CultureInfo.InvariantCulture))

这给你一个 DateTime 值:

> res;;
val it : DateTime = 01.03.2016 00:00:00