How many tuple types are there in C#?
In a recent StackOverflow question the poster asked about the difference between tupled and curried form of a function in F#. In F#, you can use pattern matching to easily define a function that takes a tuple as an argument. For example, the poster's function was a simple calculation that multiplies the number of units sold n by the price p:
1:
|
|
The function takes a single argument of type Tuple<float, int>
(or, using the nicer F# notation
float * int
) and immediately decomposes it into two variables, price
and count
. The other
alternative is to write a function in the curried form:
1:
|
|
Here, we get a function of type float -> int -> float
. Usually, you can read this just as a
function that takes float
and int
and returns float
. However, you can also use partial
function application and call the function with just a single argument - if the price of
an apple is $1.20, we can write salesCurried 1.20
to get a new function that takes just
int
and gives us the price of specified number of apples. The poster's question was:
So when I want to implement a function that would have taken n > 1 arguments, should I for example always use a curried function in F# (...)? Or should I take the simple route and use regular function with an n-tuple and curry later on if necessary?
You can see my answer on StackOverflow. The point of this short introduction was that the question inspired me to think about how the world looks from the C# perspective...
Full name: Tuples-in-csharp.salesTuple
val float : value:'T -> float (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.float
--------------------
type float = System.Double
Full name: Microsoft.FSharp.Core.float
--------------------
type float<'Measure> = float
Full name: Microsoft.FSharp.Core.float<_>
Full name: Tuples-in-csharp.salesCurried
Published: Tuesday, 17 September 2013, 3:11 PM
Tags:
c#, f#, functional programming
Read the complete article