6

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)
Guy Coder
  • 24,501
  • 8
  • 71
  • 136
tesla1060
  • 2,621
  • 6
  • 31
  • 43

2 Answers2

9

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
Community
  • 1
  • 1
Mark Seemann
  • 225,310
  • 48
  • 427
  • 736
8

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.

yuyoyuppe
  • 1,582
  • 2
  • 20
  • 33