The approach here is to declare the uncurried function as a class like the one we saw in Fig. 3.1, then to declare a chain of classes to represent the stages of currying.
// This is the uncurried function
class MyFunction {
// Environment Part
public int32 z;
public int32 k;
// Control Part
public DateTime Value(DateTime x, int32 y) {
return x.AddYears(int.Parse(y.ToString()) + z * k);
}
// Currier
public MyFunctionCurried1 Curry(DateTime x) {
return new MyFunctionCurried1(this, x);
}
}
// This is the function with one parameter curried
class MyFunctionCurried1 {
private MyFunction _curriedFrom;
private DateTime x;
public MyFunctionCurried1(MyFunction curriedFrom, DateTime x) {
this._curriedFrom = curriedFrom;
this.x = x;
}
// Control Part
public DateTime Value(int32 y) {
return this._curriedFrom.Value(x, y);
}
// Currier
public MyFunctionCurried2 Curry(int32 y) {
return new MyFunctionCurried2(this, y);
}
}
// This is the function with both parameters curried
class MyFunctionCurried2 {
private MyFunction _curriedFrom;
private int32 y;
public MyFunctionCurried2(MyFunctionCurried1 curriedFrom, int32 y) {
this._curriedFrom = curriedFrom;
this.y = y;
}
// Control Part
public DateTime Value() {
return this._curriedFrom.Value(y);
}
}
Fig. 4.1: A curry chain (C#-like pseudocode)
So far, so good. In the next post I'll introduce function signatures as interfaces, so I can pass functions (and their curried variants) around to higher order functions.
No comments:
Post a Comment