-
-
Save Nimelrian/928987ab2270f996a9158283d06232c0 to your computer and use it in GitHub Desktop.
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
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) | |
} |
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
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) | |
} |
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
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; } | |
} |
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
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; |
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
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