博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Functional Programming] Rewrite a reducer with functional state ADT
阅读量:4983 次
发布时间:2019-06-12

本文共 2492 字,大约阅读时间需要 8 分钟。

For example we have a feature reducer like this:

// selectCard :: String -> Action Stringexport const selectCard =  createAction(SELECT_CARD)// showFeedback :: String -> Action Stringexport const showFeedback =  createAction(SHOW_FEEDBACK)// reducer :: Action a -> (State AppState ()) | Nullconst reducer = ({ type, payload }) => {  switch (type) {    case SELECT_CARD:      return answer(payload)    case SHOW_FEEDBACK:      return feedback(payload)  }  return null}export default reducer

 

First of all, we can replace 'swtich' statement with normal Object:

const actionReducer = {  SELECT_CARD: answer,  SHOW_FEEDBACK: feedback}  // reducer :: Action a -> (State AppState ()) | Nullconst reducer = ({ type, payload }) => (actionReducer[type] || Function.prototype)(payload)

In case of ´actionReducer[type]´ return undefined, we default a function by `Function.prototype`, ready to take a payload.

 

const reducer = ({ type, payload }) => (actionReducer[type] || Function.prototype)(payload)

This type of code is suitable for using 'Maybe'.

// createReducer :: ActionReducer -> Reducerexport const createReducer = actionReducer =>  ({ type, payload }) =>    prop(type, actionReducer)      .map(applyTo(payload))

Refactor:

// showFeedback :: String -> Action String export const showFeedback =  createAction(SHOW_FEEDBACK)const reducer = createReducer({  SELECT_CARD: answer,  SHOW_FEEDBACK: feedback})// reducer :: Action a -> (State AppState ()) | Null// const reducer = ({ type, payload }) => //   (actionReducer[type] || Function.prototype)(payload)export default reducer

 

For this workflow, the following code should also be chagned to handle Maybe type:

// Fromimport turn from './turn'//reducer :: (AppState, Action a) -> AppStateconst reducer = (prev, action) =>  const result = turn(action)  return isSameType(State, result)    ? result.execWith(prev)    : prev}export default reducer // Toimport turn from './turn'//reducer :: (AppState, Action a) -> AppStateconst reducer = (prev, action) =>  turn(action)    .chain(safe(isSameType(State)))//   ? result.execWith(prev) //   : prevexport default reducer

 

Here ´turn(action)´ return a Maybe type, we still need to check whether inside Maybe, it is `State` type, 

.chain(safe(isSameType(State)))

 

If it is, then we call `execWith` otherwise we return previous state:

const reducer = (prev, action) => turn(action)   .chain(safe(isSameType(State)))   .map(execWith(prev))   .option(prev)

 

转载于:https://www.cnblogs.com/Answer1215/p/11436596.html

你可能感兴趣的文章
串口配置
查看>>
centos的安装,网络的调试
查看>>
dfs枚举
查看>>
线程等待问题
查看>>
(四)rsync未授权访问
查看>>
喜欢就好
查看>>
MVC3基础嵌套总结
查看>>
QML 基本可视元素之Rectangle 七
查看>>
Python--set常用操作函数
查看>>
Java基于Tomcat Https keytool 自签证书
查看>>
机房收费调试问题(一)
查看>>
Perl多进程处理Web日志
查看>>
Oracle中MD5+Base64加密实现
查看>>
linux 编辑文档
查看>>
Java中ArrayList类的用法(转)
查看>>
作业5 指针应用1。
查看>>
关于JAVA项目中的常用的异常处理情况总结
查看>>
字段类型的选择原则
查看>>
StructLayoutLayout 属性无法通过GetCustomAttributes 或者 Attributes获得
查看>>
如何一键收藏微信文章?
查看>>