Created
January 7, 2019 08:32
-
-
Save zhentian-wan/3c514d5dde57558eaf9ab06d44225fa3 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
const { curry, compose, State, mapProps } = require("crocks"); | |
const { modify } = State; | |
const state = { | |
left: 8, | |
moves: 0 | |
}; | |
const inc = x => x + 1; | |
const dec = x => x - 1; | |
const clamp = (min, max) => x => Math.min(Math.max(min, x), max); | |
const clampAfter = curry((min, max, fn) => | |
compose( | |
clamp(min, max), | |
fn | |
) | |
); | |
const over = (key, fn) => modify(mapProps({ [key]: fn })); | |
const limitMoves = clampAfter(0, 8); | |
const decLeft = () => over("left", limitMoves(dec)); | |
const res = decLeft() | |
.chain(decLeft) | |
.chain(decLeft) | |
.chain(decLeft) | |
.chain(decLeft) | |
.chain(decLeft) | |
.chain(decLeft) | |
.chain(decLeft) | |
.chain(decLeft) | |
.execWith(state); | |
console.log(res); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment