4

I have a general function that takes a lot of parameters

f : a -> b -> c -> d -> e -> f

I want to provide specialized functions that only take the last two parameters, but provide some fixed values for the first three.

g : d -> e -> f
h : d -> e -> f

Their implementation is something like the following

g = f someA someB someC
h = f someA' someB' someC'

This is all great of course, but when it comes to invoking those functions from C# it's a problem because their types don't get "prettified". Instead I get a bunch of nested FSharpFuncs. I can avoid this problem by defining my functions like

g d e = f someA someB someC d e
h d e = f someA' someB' someC' d e 

But this seems like a really simple, mechanical transformation so I'm wondering if there's an automated way to get the same result. Perhaps some attribute I can attach to them?

ildjarn
  • 62,044
  • 9
  • 127
  • 211
Luka Horvat
  • 4,283
  • 3
  • 30
  • 48
  • [This article](http://www.navision-blog.de/blog/2012/01/30/currying-and-uncurrying-in-c-and-f/) seems to contain the answer. Also, [related](http://stackoverflow.com/questions/411572/proper-currying-in-c-sharp). – Be Brave Be Like Ukraine May 21 '15 at 11:15

1 Answers1

1

Technically speaking, the first and second options of how to write your g and h are not exactly the same. In the first case, f is applied to three arguments and the resulting new function is stored as an object in the value g.

Whereas in the second case, the function f is called with all 5 arguments every time with the values of someA, someB and someC being passed at the time of calling g.

For most cases, this distinction is not really relevant, but it becomes important when you want to cache some parts of your computation.

Long story short: The transformation has a slight semantic difference and therefore cannot really be done automatically. Just add the arguments to the new g and h.

Daniel Fabian
  • 3,828
  • 2
  • 19
  • 28
  • I realize that there are valid reasons why it isn't done automatically, but I don't see why we couldn't have an attribute that we could add to the function to explicitly state we want this transformation done. – Luka Horvat May 21 '15 at 11:51
  • Because F# compiler developers don't have infinite capacity? – Fyodor Soikin May 21 '15 at 11:56
  • Well, you might be able to make an attribute, but what's the point? just adding the arguments is shorter and simple enough. – Daniel Fabian May 21 '15 at 11:57
  • @DanielFabian Because adding an attribute is a solution. Adding parameters is a workaround. – Luka Horvat May 21 '15 at 19:06