Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // convert function of a single Pair argument to a function of two arguments
- // parameter f is a function of a Pair
- // returns a function of two arguments
- function toBiFunction(f)
- {
- function temp(a, b)
- {
- p = new Pair(a, b); // turn 2 values into a Pair
- return f(p); // return teh function of that Pair
- }
- return temp;
- }
- function f1(pair) // example function of a Pair
- {
- // check type of argument
- if (pair.kind!=="Pair") throw "Not a pair";
- // exampel function body
- return pair.getFirst() + pair.getSecond();
- }
- function Pair(a, b) { // in effect, define a Pair class (actually function constructor)
- this.kind="Pair";
- this.firstField = a;
- this.secondField = b;
- this.getFirst = getFirst1;
- this.getSecond = getSecond1;
- function getFirst1()
- {
- return this.firstField;
- }
- function getSecond1()
- {
- return this.secondField;
- }
- }
- p1 = new Pair(1, 2); // make a Pair
- console.log(p1.getFirst()); // 1
- funcPair=f1; // make an example function object of a Pair
- // console.log(funcPair(7)); this throws an exception - cannot apply funcPair to singleton
- console.log(funcPair(p1)); // 3 - check funcPair working
- funcBi = toBiFunction(funcPair); // create a biFunction
- console.log(funcBi(7,8)); // 15
Advertisement
Add Comment
Please, Sign In to add comment