Created
January 13, 2020 08:33
-
-
Save thoferon/842532d61e626259a346498153e5ff61 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
import readline from "readline-sync" | |
// Side effects | |
class EffectfulImplementation { | |
getUserInput () { | |
return readline.question("> ") | |
} | |
listTodoItems () { | |
// Dummy implementation. | |
return [ | |
{ description: "Laundry", done: false }, | |
{ description: "Groceries", done: true } | |
] | |
} | |
printTodoItems (items) { | |
for (const item of items) { | |
console.log(item) | |
} | |
} | |
} | |
// Logic (pure code) | |
function parseCommand(input) { | |
// Dummy implementation. | |
return { | |
action: "list", | |
} | |
} | |
function cli(impl) { | |
const userInput = impl.getUserInput() | |
const command = parseCommand(userInput) | |
switch (command.action) { | |
case "list": | |
const items = impl.listTodoItems() | |
impl.printTodoItems(items) | |
// ... | |
} | |
} | |
// Glue code | |
cli(new EffectfulImplementation()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment