// imperative var makes = []; for (var i = 0; i < cars.length; i++) { makes.push(cars[i].make); } // declarative var makes = cars.map(function(car) { return car.make; });
// Original line add(multiply(flock_b, add(flock_a, flock_c)), multiply(flock_a, flock_b)); // (add(flock_a, flock_c) == flock_a) add(multiply(flock_b, flock_a), multiply(flock_a, flock_b)); #1 passo: aplicar a lei da identidade (x + 0) = x
add(multiply(flock_b, flock_a), multiply(flock_a, flock_b)); multiply(flock_b, add(flock_a, flock_a)); #2 passo: aplicar a lei da distributividade a × (b + c) = a × b + a × c
// Original line add(multiply(flock_b, add(flock_a, flock_c)), multiply(flock_a, flock_b)); // Apply the identity property to remove the extra add // (add(flock_a, flock_c) == flock_a) add(multiply(flock_b, flock_a), multiply(flock_a, flock_b)); // Apply distributive property to achieve our result multiply(flock_b, add(flock_a, flock_a));
// impure var minimum = 21; var checkAge = function(age) { return age >= minimum; }; // pure var checkAge = function(age) { var minimum = 21; return age >= minimum; };
Side Effects A side effect is a change of system state or observable interaction with the outside world that occurs during the calculation of a result.
Alguns side effects... ● mudança no sistema de arquivos ● inserção de registro no banco de dados ● fazer uma chamada HTTP ● mutations ● imprimir alguma informação na tela / logging ● obtenção de input do usuário ● querying o DOM (querySelector)
"The problem with object-oriented languages is they’ve got all this implicit environment that they carry around with them. You wanted a banana but what you got was a gorilla holding the banana... and the entire jungle" ~> Erlang creator, Joe Armstrong
var toUpperCase = function(x) { return x.toUpperCase(); }; var exclaim = function(x) { return x + '!'; }; var shout = compose(exclaim, toUpperCase); shout("send in the clowns"); //=> "SEND IN THE CLOWNS!"
// without compose var shout = function(x) { return exclaim(toUpperCase(x)); }; // with compose var shout = compose(exclaim, toUpperCase); shout("send in the clowns"); //=> "SEND IN THE CLOWNS!"
var loudLastUpper = compose(exclaim, toUpperCase, head, reverse); // ou var last = compose(head, reverse); var loudLastUpper = compose(exclaim, toUpperCase, last); // ou var last = compose(head, reverse); var angry = compose(exclaim, toUpperCase); var loudLastUpper = compose(angry, last); // n possibilidades...
//não é pointfree pq mencionou o dado: word var snakeCase = function(word) { return word.toLowerCase().replace(/\s+/ig, '_'); }; //pointfree var snakeCase = compose(replace(/\s+/ig, '_'), toLowerCase);
var latin = compose(map, angry, reverse); latin(['frog', 'eyes']); // error // right - each function expects 1 argument. var latin = compose(map(angry), reverse); latin(['frog', 'eyes']); // ['EYES!', 'FROG!'])
var dasherize = compose(join('-'), map(toLower), split(' '), replace(/\s{2,}/ig, ' ')); dasherize('The world is a vampire'); // 'the-world-is-a-vampire' Ah, preciso mapear o "toLower".
Especificações do nosso app: 1. Construir uma URL a partir da palavra-chave; 2. Fazer uma chamada HTTP à API do Flickr; 3. Transformar o resultado JSON em HTML images; 4. Renderiza-los na tela; https://api.flickr.com/services/feeds/photos_public.gne?tags =cats&format=json&jsoncallback=?
var moment = require('moment'); // getAge :: Date -> User -> Either(String, Number) var getAge = curry(function(now, user) { var birthdate = moment(user.birthdate, 'YYYY-MM-DD'); if (!birthdate.isValid()) return Left.of('Birth date could not be parsed'); return Right.of(now.diff(birthdate, 'years')); }); getAge(moment(), { birthdate: '2005-12-12', }); // Right(9) getAge(moment(), { birthdate: '20010704', }); // Left('Birth date could not be parsed')
// fortune :: Number -> String var fortune = compose(concat('If you survive, you will be '), add(1)); // zoltar :: User -> Either(String, _) var zoltar = compose(map(console.log), map(fortune), getAge(moment())); zoltar({ birthdate: '2005-12-12', }); // 'If you survive, you will be 10' // Right(undefined) zoltar({ birthdate: 'balloons!', }); // Left('Birth date could not be parsed')
// either :: (a -> c) -> (b -> c) -> Either a b -> c var either = curry(function(f, g, e) { switch (e.constructor) { case Left: return f(e.__value); case Right: return g(e.__value); } }); // zoltar :: User -> _ var zoltar = compose(console.log, either(id, fortune), getAge(moment())); zoltar({ birthdate: '2005-12-12', }); // "If you survive, you will be 10" // undefined zoltar({ birthdate: 'balloons!', }); // "Birth date could not be parsed" // undefined