Created
July 31, 2019 22:11
-
-
Save GeDiez/dcc72f3693fd77bea4182fb163d16e52 to your computer and use it in GitHub Desktop.
simulate lift using js
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 initialState = { | |
floor: 0, | |
isOpen: false, | |
direction: 'up', | |
} | |
function Lift (state = initialState) { | |
console.table(state) | |
function request (floor) { | |
return Lift({ | |
...state, | |
direction, | |
floor | |
}) | |
} | |
function setFloor(floor) { | |
return Lift({ | |
...state, | |
floor | |
}) | |
} | |
function open () { | |
return Lift({ | |
...state, | |
isOpen: true | |
}) | |
} | |
function close () { | |
return Lift({ | |
...state, | |
isOpen: false | |
}) | |
} | |
return { | |
open, | |
close, | |
request | |
} | |
} | |
function LiftQueue (lift) { | |
return function (liftTarget, callback) { | |
if (lift.floor !== liftTarget.floor) { | |
setTimeout(() => { | |
console.log("DING!") | |
liftTarget.open().close() | |
callback(LiftQueue(liftTarget)) | |
}, Math.abs(lift.floor - liftTarget.floor) * 1000) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
running:
copy this script to some REPL like in the browser