Is there a more idiomatic JavaScript solution for this situation:
const addTuple = (map, tuple) => { map[tuple[0]] = tuple[1]; return map; };
I am looking to refactor this example (refer to finance3.js) in a more functional style:
angular.module('finance3', [])
.factory('currencyConverter', ['$http', function($http) {
let rates = {}; // Ideally should be a future (or some other monad) mapped from $http.success, but it's challenging to do with JavaScript
const processRate = (rate) => {
return [rate.id.substring(3, 6), window.parseFloat(rate.Rate)];
};
const addTuple = (map, tuple) => { map[tuple[0]] = tuple[1]; return map; };
$http.jsonp(url).success(function(data) {
rates = data.query.results.rate.map(processRate).reduce(addTuple, {});
});
return rates;
}]);
Therefore, .reduce(addTuple, {})
can be seen as equivalent to toMap
in this context.
P.S. On a side note, KnockoutJs offers a more reactive (albeit still non-pure functional) approach to updating the model.