Created
July 21, 2015 00:23
-
-
Save TiagoWinehouse/e8a2ca39dfcad9d5bf22 to your computer and use it in GitHub Desktop.
JavaScript Funcional
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Lista Apenas um array de objetos; | |
var users = [ | |
{name: "John Doe", age: 42} | |
, {name: "Mary Doe", age: 37} | |
, {name: "Eddie Doe", age: 24} | |
]; | |
//Ordenação Utilize a função Array#sort; | |
var ordered = users.sort(function(a, b){ | |
return (a.name < b.name ? -1 : 1); | |
}); | |
//Renderização Crie uma função que lida com essa abstração. | |
function UserView(user) { | |
this.user = user; | |
} | |
UserView.prototype.render = function() { | |
return "<tr><td>" + this.user.name + "</td>" + | |
"<td>" + this.user.age + "</td></tr>" | |
; | |
}; | |
//Agregação Transforme a lista em um resultado agregado. | |
var html = ""; | |
var view; | |
for (var i = 0; i < ordered.length; i++) { | |
view = new UserView(ordered[i]); | |
html += view.render(); | |
} | |
//Primeira Solução | |
function UserView(user) { | |
this.user = user; | |
} | |
UserView.prototype.render = function() { | |
return "<tr><td>" + this.user.name + "</td>" + | |
"<td>" + this.user.age + "</td></tr>"; | |
}; | |
var ordered = users.sort(function(a, b){ | |
return (a.name < b.name ? -1 : 1); | |
}); | |
var html = ""; | |
var view; | |
for (var i = 0; i < ordered.length; i++) { | |
view = new UserView(ordered[i]); | |
html += view.render(); | |
} | |
//Segunda Solução | |
function UserView(user) { | |
this.user = user; | |
} | |
UserView.prototype.render = function() { | |
return "<tr><td>" + this.user.name + "</td>" + | |
"<td>" + this.user.age + "</td></tr>"; | |
}; | |
var html = users | |
.sort(function(a, b){ return a.name > b.name ? 1 : -1; }) | |
.map(function(user){ return new UserView(user); }) | |
.map(function(view){ return view.render(); }) | |
.join("\n") | |
; | |
//Extraia padrões | |
function asc(attribute) { | |
return function(a, b) { | |
return a[attribute] > b[attribute] ? 1 : -1; | |
}; | |
} | |
function desc(attribute) { | |
return function(a, b) { | |
return a[attribute] > b[attribute] ? -1 : 1; | |
}; | |
} | |
users.sort(asc("name")); | |
function wrapWith(constructor) { | |
return function(object) { | |
return new constructor(object); | |
}; | |
} | |
users.map(wrapWith(UserView)); | |
function call(funcName) { | |
return function(object) { | |
return object[funcName].call(object); | |
}; | |
} | |
views.map(call("render")); | |
users | |
.sort(asc("name")) | |
.map(wrapWith(UserView)) | |
.map(call("render")) | |
.join("\n"); | |
//Function Composition pode ajudar! (mas não sempre); compose(a, b, c); a(b(c())); | |
var callRender = call("render"); | |
var wrapView = wrapWith(UserView); | |
var render = compose(callRender, wrapView); | |
users | |
.sort(asc("name")) | |
.map(render) | |
.join("\n") | |
; | |
compose(callRender, wrapView)(user); | |
//é equivalente a | |
callRender(wrapView(user)); | |
//“ | |
//Às vezes a implementação | |
//mais elegante é uma função. | |
//Não é um metodo. | |
//Não é uma classe. | |
//Não é um framework. | |
//É apenas uma função. | |
//— John Carmack |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment