Skip to content

Instantly share code, notes, and snippets.

@zhentian-wan
Created January 7, 2019 08:32
Show Gist options
  • Save zhentian-wan/3c514d5dde57558eaf9ab06d44225fa3 to your computer and use it in GitHub Desktop.
Save zhentian-wan/3c514d5dde57558eaf9ab06d44225fa3 to your computer and use it in GitHub Desktop.
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