When we start to accumulate functions that all work on a given datatype, we end up creating a bunch of boilerplate code in almost every function to handle the changes we need to make to our records’ values. This can lead to not only undesirable boilerplate present in all of our functions, but also can cause us to have to create variables just to manage our stateful changes.

We’ll take a look at a couple patterns that can act as early warning signs that will eventually cause us to not have a good time. Once we know what the smell is, we’ll look at how moving our computations into State can clean up all of our state management code by making it the responsibility of State. This allows our functions to only describe how state should change over time versus us having to change it ourselves.

Imaging we have a user object, if we want to udpate firstName prop, we have to update fullName as well.

const user =  {
firstName: 'John',
lastName: 'Green',
fullName: 'John Green'
}

Code like this:

const _buildFulName = user => {
const {firstName, lastName} = user;
const fullName = joinName(firstName, lastName)
return _updateFullName(fullName, user)
}
const _updateFirstName = curry(
firstName => compose(
_buildFulName,
assign({firstName})
)
)
const _updateFullName = curry(
fullName => assign({fullName})
)

We want to use a more flexiable way to do it:

const getState = (key) => get(prop(key))
const getFirstName = () => getState('firstName').map(option(''));
const getLastName = () => getState('lastName').map(option(''));
const joinName = firstName => lastName => `${firstName}, ${lastName}` const buildFullName = () => liftA2(
joinName,
getFirstName(),
getLastName()
).chain(updateFullName)
const updateFirstName = firstName => modify(
assign({firstName})
).chain(buildFullName);
const updateFullName = fullName => modify(
assign({fullName})
)

最新文章

  1. [转] LBYL与EAFP两种防御性编程风格
  2. C# WebApi 请求方式Post,返回Response
  3. IntelliJ设置鼠标悬浮提示和修改快捷键
  4. git 在提交之前撤销add操作
  5. 【转载】高性能I/O设计模式Reactor和Proactor
  6. Linux网络编程echo多线程服务器
  7. JDK,JRE,JVM区别与联系(转)
  8. struts2移除标签button的id传统的价值观念问题
  9. UIView的alpha、hidden、opaque 深入
  10. php 极简框架ES发布(代码总和不到 400 行)
  11. 【DDD】领域驱动设计实践 —— 限界上下文识别
  12. MMA8451重力加速度计通过写内部校准寄存器进行校准
  13. EBS R12安装升级(FRESH)(二)
  14. 和我一起学习爬虫之HTTP/HTTPS抓包工具--Fiddler
  15. HBase Thrift过滤语法
  16. Cocos Creator代码编辑环境配置
  17. scala语法
  18. php linux 环境搭建
  19. Luogu 1613 跑路(最短路径,倍增)
  20. redis的数据持久化存储

热门文章

  1. git 命令小结
  2. 设置loadrunner中每个mdrv.exe进程中包含的vuser个数
  3. 六十三 、异步IO
  4. spawn-fcgi出错处理
  5. Codeforces 713A. Sonya and Queries
  6. 图论&双连通分量&强联通分量&2-SAT
  7. 第8天-setInterval/setTimeout
  8. [P4063][JXOI2017]数列(DP)
  9. android:scrollbarStyle属性及滚动条和分割线覆盖问题
  10. Java并发(四):happens-before