F# pipe first parameter

一世执手 提交于 2019-12-12 10:47:33

问题


is that possible to pipe the first parameter into a multiple parameter function? for example

date = "20160301"

Is that possible to pipe date into

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

回答1:


As @yuyoyuppe explains in his/her answer, you can't pipe directly into ParseExact because it's a method, and thereby not curried.

It's often a good option to define a curried Adapter function, but you can also pipe into an anonymous function if you only need it locally:

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

which gives you a DateTime value:

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



回答2:


From 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.

Since you cannot curry a function which accepts tuple directly, you'll have to wrap the ParseExact to do that:

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

Perhaps unrelated, but output arguments can be bound like that:

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

Note that TryParse accepts the tuple with the second argument as byref.



来源:https://stackoverflow.com/questions/35716622/f-pipe-first-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!