[Functional Programming ADT] Combine Multiple State ADT Based Redux Reducers

Redux provides a convenient helper for combining many reducers called combineReducer, but it focuses in on specific attributes on our state, making it incompatible with using the State ADT. We would like a way to avoid keeping all of our reducers in a single file and want the ability to still combine them in a manner that works with the State ADT.

So we will put together our own helper that we also call combineReducers, but explore how we can use the First Monoid and mreduceMap to get us all the power that the Redux helper provides, but setup for our unique needs. As a bonus we will get a sneak peak of the power of using the flipcombinator to create easy to read compositions without pesky argument juggling

// combineReducers :: [ Reducer ] -> Reducer
/*
export const combineReducers = reducers => action =>
  mreduceMap(First, applyTo(action), reducers);
*/
// We take reducers first and action second, but we use action first, reducers second.
// It is good case to use flip
/*
export const combineReducers = flip(action =>
  mreduceMap(First, applyTo(action))
);*/

// We can use compose to remove action param, applyTo will get action
// Then the return result will be passed into mreduceMap(First)
export const combineReducers = flip(
  compose(
    mreduceMap(First),
    applyTo
  )
);

原文地址:https://www.cnblogs.com/Answer1215/p/10355624.html

时间: 2024-10-10 01:05:46

[Functional Programming ADT] Combine Multiple State ADT Based Redux Reducers的相关文章

[Functional Programming ADT] Initialize Redux Application State Using The State ADT

Not only will we need to give our initial state to a Redux store, we will also need to be able to reset our state at any time by dispatching an action. We can get the best of both worlds by having a function that will return an object with all of our

Beginning Scala study note(4) Functional Programming in Scala

1. Functional programming treats computation as the evaluation of mathematical and avoids state and mutable data. Scala encourages an expression-oriented programming(EOP) 1) In expression-oriented programming every statement is an expression. A state

Apply Functional Programming Principles

Apply Functional Programming Principles Edward Garson FUNCTiONAL PROGRAMMiNG has recently enjoyed renewed interest from the mainstream programming community. Part of the reason is because emergent properties of the functional paradigm are well positi

Functional Programming without Lambda - Part 2 Lifting, Functor, Monad

Lifting Now, let's review map from another perspective. map :: (T -> R) -> [T] -> [R] accepts 2 parameters, a function f :: T -> R and a list list :: [T]. [T] is a generic type paramterized by T, it's not the same as T, but definitely shares s

Functional Programming.

I have been seeing Redux for sometime, but recently I come to realize that , it's part of so called functional programming. The basic idea for functional programming is so simple, Declare all dependency as function input, no hidden input ( we should

Functional Programming in Javascript 中文翻译 —— 目录和介绍

原著:[美] Dan Mantyla 目录 前言1 Javascript函数式编程的力量--举个例子2 函数式编程基础3 建立函数式编程环境4 在Javascript中实现函数式编程的技术5 类型理论6 高级主题以及Javascript的缺陷7 Javascript中的函数式和面型对象编程8 Javascript中的函数式和面型对象编程 关于翻译的这本书 现在市面上有两本专注于javascript函数式编程的书,一本是<Functional Javascript>(下文简称FJS), 另一本就

【转】This version of the rendering library is more recent than your version of ADT plug-in. Please update ADT plug-in

原文网址:http://1982106a.blog.163.com/blog/static/8436495620149239361692/ 预览layout.xml文件时提示: This version of the rendering library is more recent than your version of ADT plug-in. Please update ADT plug-in 导致无法正常预览布局文件: 问题根源:SDK版本过高,ADT版本低: 解决办法有好几种,如下:

Functional Programming Principles in ScalaScala函式编程原理 第一章笔记

所有non-trival编程语言都提供了 基本表达式(expression)去表示最简单的表达式 组合表达式的方法 抽象表达式的方法,也就是为表达式引入一个名字去引用它 substitional model 替代模型 sumOfSquares(3,2+2) sumOfSquares(3,4) square(3)+square(4) 9+square(4) 9+16 25 这种模型的核心概念是所有的表达式都规约成值,替代模型在lamada表达式中被形式化,构成了函数式编程的基石 substitio

Coursera公开课Functional Programming Principles in Scala习题解答:Week 2

引言 OK.时间很快又过去了一周,第一周有五一假期所以感觉时间绰绰有余,这周中间没有假期只能靠晚上加周末的时间来消化,其实还是有点紧张呢!后来发现每堂课的视频还有对应的课件(Slide).字幕(subtitles)可以下载,这样下载视频学习和在线学习就只差课程中间的Exercise了 Week 2主要讲函数,函数在Scala里是first-class citizen,可以在任意域内出现,这门课其实也是在借Scala来讲函数式编程原理.好了,不多说,进入习题解析. 这周的作业主要是使用函数来表示一