Recently, I recognize two different way to programming soft, Functional-Programming(FP) And Object-oriented programming(OOP).
In the pass, FP is only in study book.
Because, the software be creatives by FP way, those calculation speed are slow than OOP.
However, the hardware keeping improve. Today, those two way isn't different in the calculation speed.
Here is example for FP and OOP
OOP:
function outputTime(num){
return (num<=12)? "good morning!": "good night!";
}
function outputUserInfo(gender,name){
return (gender.indexOf("x")>-1)? " Mr. "+ name: " Miss. "+ name;
}
function sayHello(num,gender,name){
return outputTime(num)+outputUserInfo(gender,name);
}
console.log(sayHello(12,'x','Leo'));
FP:
function outputTime(sayHello){
return function( name ){
var hiAndName_A = sayHello(name);
var hiAndA = sayHello(hiAndName_A);
return new Date().toString()+hiAndA;
}
}
var outMsg = outputTime(function(name){
return 'hi! '+name;
});
console.log(outMsg('Leo'));
[reference]
FP-JS
http://slid.es/gsklee/functional-programming-in-5-minutes
OOP-JS
https://blog.othree.net/log/2013/05/19/this/
Descript
http://www.jerry-tsai.com/2008/05/fp_22.html
In the pass, FP is only in study book.
Because, the software be creatives by FP way, those calculation speed are slow than OOP.
However, the hardware keeping improve. Today, those two way isn't different in the calculation speed.
Here is example for FP and OOP
OOP:
function outputTime(num){
return (num<=12)? "good morning!": "good night!";
}
function outputUserInfo(gender,name){
return (gender.indexOf("x")>-1)? " Mr. "+ name: " Miss. "+ name;
}
function sayHello(num,gender,name){
return outputTime(num)+outputUserInfo(gender,name);
}
console.log(sayHello(12,'x','Leo'));
FP:
function outputTime(sayHello){
return function( name ){
var hiAndName_A = sayHello(name);
var hiAndA = sayHello(hiAndName_A);
return new Date().toString()+hiAndA;
}
}
var outMsg = outputTime(function(name){
return 'hi! '+name;
});
console.log(outMsg('Leo'));
[reference]
FP-JS
http://slid.es/gsklee/functional-programming-in-5-minutes
OOP-JS
https://blog.othree.net/log/2013/05/19/this/
Descript
http://www.jerry-tsai.com/2008/05/fp_22.html
留言
張貼留言