We build our first state transactions as two discrete transactions, each working on a specific portion of the state. Each of these transitions are governed by an acceptable range and we want to be able to apply these limit within our model.

With our discrete transactions we see that is easy to reason about and keeps our rules local to the data being worked on, without having to be considered in other transitions or, even worse, in the eventual view layer. Patterns like this make it easier to easily transport a given data model to any view layer.

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; // Make sure 'x' in range of [min, max]
// if x < min, return min
// if x > max, return max
const limitRange = (min, max) => x => Math.min(Math.max(min, x), max); // Run the 'fn', get the value, then pass into limitRange
const limitAfter = curry((min, max, fn) =>
compose(
limitRange(min, max),
fn
)
); // For each key we passed in, apply fn to it
const over = (key, fn) => modify(mapProps({ [key]: fn })); const limitMoves = limitAfter(0, 8); const decLeft = () => over("left", limitMoves(dec)); const res = decLeft()
.chain(decLeft)
.execWith(state);
console.log(res); //{left: 6, moves: 0}

最新文章

  1. iOS多播放器封装
  2. 【多线程 5】线程池的类型以及submit()和execute()的区别
  3. Windows 10 开始菜单修改程序
  4. Java多线程与并发库高级应用-Callable与Future的应用
  5. OpenGLES入门笔记二
  6. 清北暑假模拟day1 爱
  7. miniUI 可编辑datagrid获取值的问题
  8. 离线树状数组 hihocoder 1391 Countries
  9. XML和JSON两种数据交换格式的比较
  10. 使用BurpSuite进行双文件上传拿Webshell
  11. Python-字符串的常用操作
  12. HDOJ1287_破译密码
  13. 业务-----修改Service常用逻辑
  14. Java理论学时第六节。课后作业。
  15. odoo开发笔记 -- wkhtmltox打印不显示中文 --ubuntu字体安装
  16. 【SpringBoot系列2】SpringBoot整合Redis
  17. [转]谈谈前端渲染 VS 后端渲染
  18. MySQL &quot;replace into&quot; 的坑以及insert相关操作
  19. linux 动态库加载路径修改
  20. 检查Linux服务器性能的关键十条命令

热门文章

  1. js j将数字每三位用逗号隔开的方法
  2. Photoshop&#160;&#160;cs6 快捷键命令大全
  3. 2.OpenStack-安装消息队列服务
  4. Laravel实现用户名或密码登录
  5. Python的网络编程[2] -&gt; TFTP 协议[1] -&gt; TFTP 的 Python 实现
  6. 小白入门篇:flex布局
  7. hdu6035(树形DP)
  8. 分层图【p4822】[BJWC2012]冻结
  9. 【bzoj4318】【OSU!】期望dp——维护多个期望值递推
  10. 在sqlserver中如何从字符串中提取数字,英文,中文,过滤重复字符