[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 initial values in it. Then use that function to craft a State ADT transition that will throw out whatever our previous state was and replace it with the original initial state.

We’ll not only build out an initialize state transaction, but also use that new transaction to craft an action creator to expose a means to dispatch at any time an action that will reset our state.

Set initial state:

We use PUT state to reset the state.

import State from "crocks/State";

const { put } = State;

// initialState :: () -> AppState
export const initialState = () => ({
  colors: ["orange", "green", "blue", "yellow"],
  shapes: ["triangle", "circle", "square"],
  cards: [],
  hint: {},
  isCorrect: null,
  left: 8,
  moves: 0,
  rank: 4,
  seed: 23
});

// initialize :: () -> State AppState ()
const initialize = () => put(initialState());

export default initialize;

Create action for reducer:

1. Create action const string

2. Action creator

3. Create reducer, bind action const to state ()

import { createAction, createReducer } from "../helpers";

import start, { markCardsUnselected } from "../model/game";
import initialize from "../model/initialize";

const HIDE_ALL_CARDS = "HIDE_ALL_CARDS";
const START_GAME = "START_GAME";
const RESET_GAME = "RESET_GAME";

// hideAllCards :: String -> Action String
export const hideAllCards = createAction(HIDE_ALL_CARDS);

// startGame :: String -> Action String
export const startGame = createAction(START_GAME);

// startGame :: String -> Action String
export const resetGame = createAction(RESET_GAME);

// reducer :: Reducer
const reducer = createReducer({
  HIDE_ALL_CARDS: markCardsUnselected,
  START_GAME: start,
  RESET_GAME: initialize
});

export default reducer;

Kick off:

Call the reducer with state, action.

import log from "./logger";

import reducer from "./data/reducers";

import { resetGame } from "./data/reducers/game";

import initialize from "./data/model/initialize";

log(reducer({}, resetGame()));

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

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

[Functional Programming ADT] Initialize Redux Application State Using The State ADT的相关文章

[Functional Programming] Transition State based on Existing State using the State ADT (liftState, composeK)

While sometimes outside input can have influence on how a given stateful transaction transitions, there are many times where the current state at the time of a transaction. We can see the power of this type of transaction by seeing what it would take

[Functional Programming + React] Provide a reasonable default value for mapStateToProps in case initial state is undefined

For example we have a component, it needs to call 'react-redux' connect function. import { compose, curry, option, propPath } from '../js/helper' const FilterButton = ({ active, onClick }) => { const classes = classnames('filterButton', { 'filterButt

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.

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

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

PROFESSIONAL FUNCTIONAL PROGRAMMING IN C# 学习

CHAPTER1 A Look at Functional Programming History CHAPTER2 Putting Functional Programming into a Modern Context CHAPTER3 Functions,Delegates,and Lambda Expressions CHAPTER4 Flexible Typing with Generics CHAPTER5 Lazy Listing with Iterators CHAPTER6 E

Important Programming Concepts (Even on Embedded Systems) Part V: State Machines

Earlier articles in this series: Part I: Idempotence Part II: Immutability Part III: Volatility Part IV: Singletons Oh, hell, this article just had to be about state machines, didn’t it? State machines! Those damned little circles and arrows and q’s.

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

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

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