这个模块是一些关于函数的实现
提要
describe('functions', function() {
var sayItCalled = false;
var sayIt = function(greeting, name, punctuation) {
sayItCalled = true;
return greeting + ', ' + name + (punctuation || '!');
};
beforeEach(function () {
sayItCalled = false;
});
}
将参数转化为数组模式
it('you should be able to use an array as arguments when calling a function', function() {
var result = functionsAnswers.argsAsArray(sayIt, [ 'Hello', 'Ellie', '!' ]);
expect(result).to.eql('Hello, Ellie!');
expect(sayItCalled).to.be.ok;
});
我的解法
argsAsArray: function(fn, arr) {
return fn.apply(this,arr);
},
作者
argsAsArray: function(fn, arr) {
return fn.apply(null, arr);
},
似乎this,的确是不太需要。
动态绑定Object
it('you should be able to change the context in which a function is called', function() {
var speak = function() {
return sayIt(this.greeting, this.name, '!!!');
};
var obj = {
greeting: 'Hello',
name: 'Rebecca'
};
var result = functionsAnswers.speak(speak, obj);
expect(result).to.eql('Hello, Rebecca!!!');
expect(sayItCalled).to.be.ok;
});
mine
speak: function(fn, obj) {
return fn.apply(obj);
},
返回函数
it('you should be able to return a function from a function', function() {
expect(functionsAnswers.functionFunction('Hello')('world')).to.eql('Hello, world');
expect(functionsAnswers.functionFunction('Hai')('can i haz funxtion?')).to.eql('Hai, can i haz funxtion?');
});
mine
functionFunction: function(str) {
return function (str1) {
return str+", "+str1;
}
},
闭包
返回函数数组
it('you should be able to use closures', function () {
var arr = [ Math.random(), Math.random(), Math.random(), Math.random() ];
var square = function (x) { return x * x; };
var funcs = functionsAnswers.makeClosures(arr, square);
expect(funcs).to.have.length(arr.length);
for (var i = 0; i < arr.length; i++) {
expect(funcs[i]()).to.eql(square(arr[i]));
}
});
mine
makeClosures: function(arr, fn) {
var res = [];
for (var i = 0; i < arr.length; i++) {
res.push(function (num) {
return function () {
return fn(arr[num]);
};
}(i));
}
return res;
}
作者
makeClosures: function(arr, fn) {
var ret = [];
var makeFn = function(val) {
return function() { return fn(val); };
};
for (var i = 0, len = arr.length; i < len; i++) {
ret.push(makeFn(arr[i]));
}
return ret;
},
偏函数
it('you should be able to create a "partial" function', function() {
var partial = functionsAnswers.partial(sayIt, 'Hello', 'Ellie');
expect(partial('!!!')).to.eql('Hello, Ellie!!!');
expect(sayItCalled).to.be.ok;
});
mine
partial: function(fn, str1, str2) {
return function (str3) {
return fn(str1,str2,str3);
}
},
使用arguments
it('you should be able to use arguments', function () {
var a = Math.random();
var b = Math.random();
var c = Math.random();
var d = Math.random();
expect(functionsAnswers.useArguments(a)).to.eql(a);
expect(functionsAnswers.useArguments(a, b)).to.eql(a + b);
expect(functionsAnswers.useArguments(a, b, c)).to.eql(a + b + c);
expect(functionsAnswers.useArguments(a, b, c, d)).to.eql(a + b + c + d);
});
mine
useArguments: function() {
var res = 0;
var n = arguments.length;
for(var i=0;i<n;i++){
res+=arguments[i];
}
return res;
},
函数输入任意参数
it('you should be able to apply functions with arbitrary numbers of arguments', function () {
(function () {
var a = Math.random();
var b = Math.random();
var c = Math.random();
var wasITake2ArgumentsCalled = false;
var iTake2Arguments = function (firstArgument, secondArgument) {
expect(arguments.length).to.eql(2);
expect(firstArgument).to.eql(a);
expect(secondArgument).to.eql(b);
wasITake2ArgumentsCalled = true;
};
var wasITake3ArgumentsCalled = false;
var iTake3Arguments = function (firstArgument, secondArgument, thirdArgument) {
expect(arguments.length).to.eql(3);
expect(firstArgument).to.eql(a);
expect(secondArgument).to.eql(b);
expect(thirdArgument).to.eql(c);
wasITake3ArgumentsCalled = true;
};
functionsAnswers.callIt(iTake2Arguments, a, b);
functionsAnswers.callIt(iTake3Arguments, a, b, c);
expect(wasITake2ArgumentsCalled).to.be.ok;
expect(wasITake3ArgumentsCalled).to.be.ok;
}());
});
mine
callIt: function(fn) {
var new_arg = [];
for(var i=1;i<arguments.length;i++){
new_arg.push(arguments[i]);
}
fn.apply(this,new_arg);
},
callIt: function(fn) {
var args = Array.prototype.slice.call(arguments, 1, arguments.length);
fn.apply(null, args);
},
利用arguments的偏函数
it('you should be able to create a "partial" function for variable number of applied arguments', function () {
var partialMe = function (x, y, z) {
return x / y * z;
};
var a = Math.random();
var b = Math.random();
var c = Math.random();
expect(functionsAnswers.partialUsingArguments(partialMe)(a, b, c)).to.eql(partialMe(a, b, c));
expect(functionsAnswers.partialUsingArguments(partialMe, a)(b, c)).to.eql(partialMe(a, b, c));
expect(functionsAnswers.partialUsingArguments(partialMe, a, b)(c)).to.eql(partialMe(a, b, c));
expect(functionsAnswers.partialUsingArguments(partialMe, a, b, c)()).to.eql(partialMe(a, b, c));
});
mine,还是参考了一下思路
partialUsingArguments: function(fn) {
var argv = Array.prototype.slice.call(arguments,1,arguments.length);
return function () {
var more_argv = argv.concat(Array.prototype.slice.call(arguments));
return fn.apply(null,more_argv);
}
},
柯里化
it('you should be able to curry existing functions', function () {
var curryMe = function (x, y, z) {
return x / y * z;
};
var a = Math.random();
var b = Math.random();
var c = Math.random();
var result;
result = functionsAnswers.curryIt(curryMe);
expect(typeof result).to.eql('function');
expect(result.length).to.eql(1);
result = functionsAnswers.curryIt(curryMe)(a);
expect(typeof result).to.eql('function');
expect(result.length).to.eql(1);
result = functionsAnswers.curryIt(curryMe)(a)(b);
expect(typeof result).to.eql('function');
expect(result.length).to.eql(1);
result = functionsAnswers.curryIt(curryMe)(a)(b)(c);
expect(typeof result).to.eql('number');
expect(result).to.eql(curryMe(a, b, c));
});
curryIt: function(fn) {
return function (a) {
return function (b) {
return function (c) {
return fn.call(null,a,b,c);
}
}
}
}
好吧,我只是通过了测试用例
curryIt: function(fn) {
function applyArguments(_fn, args) {
return _fn.apply(null, args);
}
function getArgumentAccumulator(accumulatedArguments, expectedArgumentsCount) {
return function (currentArgument) {
accumulatedArguments.push(currentArgument);
var allArgumentsProvided = accumulatedArguments.length === expectedArgumentsCount;
if (allArgumentsProvided) {
return applyArguments(fn, accumulatedArguments);
}
return getArgumentAccumulator(accumulatedArguments, expectedArgumentsCount);
};
}
return getArgumentAccumulator([], fn.length);
}