Many times we need to access and transform state, either in part or in full, to be used when calculating other state transitions. We will look at how we can leverage the get function on the State ADT to read and modify portions of our application state, putting the result in the Resultant portion. We will create two transactions: one to select a card from a list of cards and another to access the hint portion of our state.

The code below is just trying to get 'cards' and 'hint' props from an object with two fns.

const {prop, State, option,chain, find, propEq, isNumber, compose, safe} = require('crocks');
const {get, modify} = State; const state = {
cards: [
{id: 'green-square', color: 'green', shape: 'square'},
{id: 'orange-square', color: 'orange', shape: 'square'},
{id: 'blue-square', color: 'blue', shape: 'triangle'}
],
hint: {
color: 'green',
shape: 'square'
}
} // getState :: String -> State Object (Maybe a)
const getState = key => get(prop(key)) const findById = id => find(propEq('id', id)); // getCard :: String -> State Object Card
const getCard = id => get(prop('cards'))
.map(chain(findById(id)))
.map(option({id: 'urk', color: '', shape: ''})) const getCard2 = (id) => compose(option({}), chain(findById(id)), prop('cards')) const getHint = () => getState('hint').map(option({
color: 'yay',
shape: 'wow'
})); const res = getCard('green-square')
.evalWith(state) // { id: 'green-square', color: 'green', shape: 'square' } const res1 = get(getCard2('green-square')) //{ id: 'green-square', color: 'green', shape: 'square' }
.evalWith(state) const res2 = getHint()
.evalWith(state); // { color: 'green', shape: 'square' }
console.log(res)
console.log(res1)
console.log(res2)

The important thing to understand is the difference between  'getCard' & 'getCard2' functions. They have the same functionalities.

const getCard = id => get(prop('cards'))
.map(chain(findById(id)))
.map(option({id: 'urk', color: '', shape: ''}))

Once we use 'get', it creates a State instance, therefore we have to use instance method 'map' to access its value.

const getCard2 = (id) => compose(option({}), chain(findById(id)), prop('cards'))    

By the time we define 'getCard2', we haven't lift it into State with 'get' function. We can do normal fp. The reason using 'chian' in both functions is because prop('cards') return a Maybe, findById return a Maybe, would be a nested Maybe type, so using 'chain' to flatten it.

最新文章

  1. Delphi 调用Dll的两种方式
  2. spark1.5 scala.collection.mutable.WrappedArray$ofRef cannot be cast to ...解决办法
  3. XE3随笔3:访问
  4. 堡垒机 paramiko 自动登陆代码
  5. Javascript里,想把一个整数转换成字符串,字符串长度为2
  6. Jqgrid的用法总结与分页功能的拓展
  7. HDU 4998 Rotate (计算几何)
  8. Python File flush方法应用
  9. spring之注解详解
  10. FFMPEG 实现 YUV,RGB各种图像原始数据之间的转换(swscale)
  11. 【机器学习基础】熵、KL散度、交叉熵
  12. vueJS报错记录列表以及解决方案
  13. php的phar是什么?
  14. 利用CNN神经网络实现手写数字mnist分类
  15. 使用yum安装 gdb/g++等软件包
  16. button 去掉原生边框
  17. C# 反射和Type类
  18. PAT 1014 福尔摩斯的约会
  19. iperf测试工具
  20. kafka无法消费数据

热门文章

  1. 8.2 前端检索的敏感词过滤的Python实现(针对元搜索)
  2. Welcome to Workrave
  3. Linux下kill命令
  4. java equals 与 hashCode
  5. 百度之星初赛(A)——T2
  6. [LeetCode]Word Search 回溯
  7. 1.Openstack-环境安装
  8. python--jinja2
  9. Linux使用命令记录
  10. C#发送POST,GET,DELETE请求API,并接受返回值