Created
November 23, 2017 07:31
-
-
Save gemmadlou/85a2084a39b22f1a9400cedd388dc57d to your computer and use it in GitHub Desktop.
Funny
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
| console.clear() | |
| let state = { | |
| todos: [] | |
| } | |
| let box = data => ({ | |
| map: fun => box(fun(data)), | |
| fold: fun => fun(data), | |
| val: data | |
| }) | |
| let getposts = (time) => new Promise((resolve, reject) => { | |
| setTimeout(() => { | |
| resolve(box(['abc'])) | |
| }, time || 3000) | |
| }) | |
| let addTodos = (state, data) => { | |
| let clone = Object.assign({}, state); | |
| clone.todos = clone.todos.concat(data.val); | |
| return ['todoAdded', clone]; | |
| } | |
| let todoView = (state) => { | |
| return state.todos | |
| .map(todo => '<li>' + todo + '</li>').join('') | |
| } | |
| let apply = ([event, newstate]) => { | |
| let actions = { | |
| todoAdded: () => { | |
| let view = todoView(newstate) | |
| document.querySelector('.todos').innerHTML = view | |
| } | |
| } | |
| state = newstate; | |
| actions[event]() | |
| } | |
| getposts() | |
| .then(data => apply(addTodos(state, data))) | |
| getposts(5000) | |
| .then(data => apply(addTodos(state, data))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It’s meant more as a joke or light-hearted coding fun than serious utility. It’s a good example of how developers sometimes write quirky scripts just to make others smile or poke fun at common patterns in code.