Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# pipe first parameter

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)
like image 970
tesla1060 Avatar asked Mar 01 '16 07:03

tesla1060


Video Answer


2 Answers

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
like image 61
Mark Seemann Avatar answered Sep 24 '22 18:09

Mark Seemann


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.

like image 42
yuyoyuppe Avatar answered Sep 24 '22 18:09

yuyoyuppe