Skip to content

Instantly share code, notes, and snippets.

@thoferon
Created January 13, 2020 08:33
Show Gist options
  • Save thoferon/842532d61e626259a346498153e5ff61 to your computer and use it in GitHub Desktop.
Save thoferon/842532d61e626259a346498153e5ff61 to your computer and use it in GitHub Desktop.
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