Last active
May 6, 2024 08:17
-
-
Save dra1n/d23dca462588e73f2937d63b9dfa0109 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const execute = (command, ...args) => command(...args); | |
function PlaceOrderCommand(order, id) { | |
return orders => { | |
const result = [...orders, { id }]; | |
console.log(`You have successfully ordered ${order} (${id})`); | |
return { success: true, result }; | |
}; | |
} | |
function CancelOrderCommand(id) { | |
return orders => { | |
const result = orders.filter(order => order.id !== id); | |
console.log(`You have canceled your order ${id}`); | |
return { success: true, result }; | |
}; | |
} | |
function TrackOrderCommand(id) { | |
return orders => { | |
console.log(`Your order ${id} will arrive in 20 minutes.`); | |
return { success: true, result: orders }; | |
}; | |
} | |
const initialOrders = []; | |
const commands = [ | |
PlaceOrderCommand("Pad Thai", "1234"), | |
TrackOrderCommand("1234"), | |
CancelOrderCommand("1234") | |
]; | |
const { result, success } = commands.reduce( | |
({ result: orders }, command) => execute(command, orders), | |
{ | |
result: initialOrders | |
} | |
); | |
console.log({ result, success }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment