[Functional Programming ADT] Adapt Redux Actions/Reducers for Use with the State ADT

By using the State ADT to define how our application state transitions over time, we clear up the need for a some of the boilerplate that we typically need for a solid Redux integration. We can keep our action names and creators in the same file as the reducer they are used in, as we no longer need to have separate files to hand the same action across different portions of our state.

We will build our a single reducer that will handle the integration with Redux and organize our State ADT based reducers in their own files and bring them into our main reducer to handle running our state transitions and the cases where we do not have an transition for a given action.

 

复制代码
// Action a :: {type: string, payload: a}
// createAction :: String -> a -> Action a
const createAction = type => payload => ({type, payload});
const SELECT_CARD = 'SELECT_CARD';
const selectCard = createAction(SELECT_CARD);


// reducer :: (State, a) -> (State AppState ()) | Null
const reducer = (prevState, {type, payload}) => {
    let result;
    switch(type) {
        case SELECT_CARD:
            result = feedback(payload);
            break;
        default: 
            result = null;   
    }

    return isSameType(State, result) ? result.execWith(prevState): prevState;
}

const sillyVerb = createAction('SILLY_VERB');

console.log(
    reducer(
        state,
        selectCard('green-square')
    )
)
复制代码

If we pass the right action, it will return the result, if a bad action, it keep the prev state untouched.

 

---

复制代码
// Action a :: {type: string, payload: a}
// createAction :: String -> a -> Action a
const createAction = type => payload => ({type, payload});
const SELECT_CARD = 'SELECT_CARD';
const selectCard = createAction(SELECT_CARD);


// reducer :: (State, a) -> (State AppState ()) | Null
const reducer = (prevState, {type, payload}) => {
    let result;
    switch(type) {
        case SELECT_CARD:
            result = feedback(payload);
            break;
        default: 
            result = null;   
    }

    return isSameType(State, result) ? result.execWith(prevState): prevState;
}

const sillyVerb = createAction('SILLY_VERB');

console.log(
    reducer(
        state,
        selectCard('green-square')
    )
)
复制代码

 

posted @   Zhentiw  阅读(185)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2018-02-01 [MST] Create Dynamic Types and use Type Composition to Extract Common Functionality
2017-02-01 [TypeScript] Use the never type to avoid code with dead ends using TypeScript
2016-02-01 [Regex Expression] Use Shorthand to Find Common Sets of Characters
2016-02-01 [Regex Expression] Find Sets of Characters
2016-02-01 [Regular Expressions] Find Repeated Patterns
2016-02-01 [Regular Expressions] Find Plain Text Patterns
点击右上角即可分享
微信分享提示