[Functional Programming] Define Discrete State Transitions using the State ADT
We build our first state transactions as two discrete transactions, each working on a specific portion of the state. Each of these transitions are governed by an acceptable range and we want to be able to apply these limit within our model.
With our discrete transactions we see that is easy to reason about and keeps our rules local to the data being worked on, without having to be considered in other transitions or, even worse, in the eventual view layer. Patterns like this make it easier to easily transport a given data model to any view layer.
const { curry, compose, State, mapProps } = require("crocks");
const { modify } = State;
const state = {
left: 8,
moves: 0
};
const inc = x => x + 1;
const dec = x => x - 1;
// Make sure 'x' in range of [min, max]
// if x < min, return min
// if x > max, return max
const limitRange = (min, max) => x => Math.min(Math.max(min, x), max);
// Run the 'fn', get the value, then pass into limitRange
const limitAfter = curry((min, max, fn) =>
compose(
limitRange(min, max),
fn
)
);
// For each key we passed in, apply fn to it
const over = (key, fn) => modify(mapProps({ [key]: fn }));
const limitMoves = limitAfter(0, 8);
const decLeft = () => over("left", limitMoves(dec));
const res = decLeft()
.chain(decLeft)
.execWith(state);
console.log(res); //{left: 6, moves: 0}
[Functional Programming] Define Discrete State Transitions using the State ADT的更多相关文章
- [Functional Programming] Read and Transform Values from a State ADT’s State (get)
Many times we need to access and transform state, either in part or in full, to be used when calcula ...
- [Functional Programming Monad] Combine Stateful Computations Using A State Monad
The true power of the State ADT really shows when we start combining our discrete, stateful transact ...
- [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 ne ...
- [Functional Programming] Introduction to State, thinking in State
Recently, I am learning Working with ADT. Got some extra thought about State Monad. Basiclly how to ...
- [Functional Programming Monad] Modify The State Of A State Monad
Using put to update our state for a given state transaction can make it difficult to modify a given ...
- [Functional Programming Monad] Map And Evaluate State With A Stateful Monad
We explore our first stateful transaction, by devising a means to echo our state value into the resu ...
- [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 re ...
- [Functional Programming Moand] Update The State Of A State Monad (put)
Stateful computations require the ability for their state to change overtime. We take a look on one ...
- [Functional Programming Monad] Substitute State Using Functions With A State Monad (get, evalWith)
We take a closer look at the get construction helper and see how we can use it to lift a function th ...
随机推荐
- poj1679 次最小生成树 kruskal(暴力枚举)
Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definit ...
- windows 安装 mysql5.7
1. 搜索“Mysql download”进入官网 或者点击链接 https://dev.mysql.com/downloads/ 进入官网,如下: 2. 下载对应的 MySQL 版本,如下: 点击 ...
- Linux 软连接 & 硬链接
1.Linux链接概念Linux链接分两种,一种被称为硬链接(Hard Link),另一种被称为符号链接(Symbolic Link).默认情况下,ln命令产生硬链接. [硬连接]硬连接指通过索引节点 ...
- 使序列有序的最少交换次数(minimum swaps)
交换相邻两数 如果只是交换相邻两数,那么最少交换次数为该序列的逆序数. 交换任意两数 数字的总个数减去循环节的个数?? A cycle is a set of elements, each of wh ...
- Android消息推送解决方案
前言 消息推送在Android开发中应用的场景是越来越多了,比如说电商产品进行活动宣传.资讯类产品进行新闻推送等等,如下图: 推送消息截图 本文将介绍Android中实现消息推送的7种主流解决方案 目 ...
- WebApi初探之路由配置
本文介绍了ASP.NET Web API路由HTTP请求控制器. 如果你熟悉ASP.NET MVC,Web API路由是和MVC路由非常相似的.主要差别是Web API使用HTTP方法而不是URI路径 ...
- 杭电oj2000-2011
2000 ASCII码排序 #include <stdio.h> int main(){ char a,b,c,t; while(scanf("%c%c%c", &a ...
- Linux/Android——input_handler之evdev (四) 【转】
转自:http://blog.csdn.net/u013491946/article/details/72638919 版权声明:免责声明: 本人在此发文(包括但不限于汉字.拼音.拉丁字母)均为随意敲 ...
- python--enum
# enum用于枚举,该模块下有一个Enum,我们定义的类要继承它 # 一旦继承,那么我们定义的key(仮),不能有重复值. # 如果要保证value(仮)不重复,那就引入unique,给我们定义的类 ...
- poj 1375(解析几何)
Intervals Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4292 Accepted: 1288 Descrip ...