Skip to content

Instantly share code, notes, and snippets.

@Nimelrian
Last active January 30, 2019 17:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nimelrian/928987ab2270f996a9158283d06232c0 to your computer and use it in GitHub Desktop.
Save Nimelrian/928987ab2270f996a9158283d06232c0 to your computer and use it in GitHub Desktop.
function getRevenueOfGermanCustomers(userService, orderService) {
return userService.getUsers()
.filter(user => user.country === "DE")
.flatmap(user => orderService.getOrdersByUser(user.id))
.map(order => order.total)
.reduce((sum, total) => sum + total)
}
function getRevenueOfGermanCustomers(userService, orderService) {
// We have to define our implementation details logic before we use it, which leads to unnecessary noise.
const isUserFromGermany = user => user.country === "DE";
const getOrdersForUser = user => orderService.getOrdersByUser(user.id);
const getOrderRevenue = order => order.total;
const sum = (a, b) => a + b;
return userService.getUsers()
.filter(isUserFromGermany)
.flatmap(getOrdersForUser)
.map(getOrderRevenue)
.reduce(sum)
}
function getRevenueOfGermanCustomers(userService, orderService) {
return userService.getUsers()
.filter(isUserFromGermany)
.flatmap(getOrdersForUser)
.map(getOrderRevenue)
.reduce(sum)
// This works: We have moved our implementation details out of the spotlight. However, this is verbose
// and tools like Prettier will format each function into 3 lines instead of keeping them on a single line each.
function isUserFromGermany (user) { return user.country === "DE"; }
function getOrdersForUser (user) { return orderService.getOrdersByUser(user.id); }
function getOrderRevenue (order) { return order.total; }
function sum (a, b) { return a + b; }
}
function getRevenueOfGermanCustomers(userService, orderService) {
return userService.getUsers()
.filter(isUserFromGermany)
.flatmap(user => getOrdersForUser(orderService, user))
.map(getOrderRevenue)
.reduce(sum)
}
// When defining the arrow functions out of scope, we can move them to the bottom.
// This allows us to move implementation details out of the spotlight,
// but forces us to define an additional parameter for getOrdersForUser and requires
// an additional wrapping arrow function in our declarative flow.
const isUserFromGermany = user => user.country === "DE";
const getOrdersForUser = (orderService, user) => orderService.getOrdersByUser(user.id);
const getOrderRevenue = order => order.total;
const sum = (a, b) => a + b;
function getRevenueOfGermanCustomers(userService, orderService) {
return userService.getUsers()
.filter(isUserFromGermany)
.flatmap(getOrdersForUser)
.map(getOrderRevenue)
.reduce(sum)
function isUserFromGermany (user) = user.country === "DE";
function getOrdersForUser (user) = orderService.getOrdersByUser(user.id);
function getOrderRevenue (order) = order.total;
function sum (a, b) = a + b;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment