[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 ...
随机推荐
- bzoj1396&&2865 识别子串 后缀自动机+线段树
Input 一行,一个由小写字母组成的字符串S,长度不超过10^5 Output L行,每行一个整数,第i行的数据表示关于S的第i个元素的最短识别子串有多长. Sample Input agoodco ...
- python 读取数据库时,datetime类型无法被json序列化--解决方案
新增针对datetime的jsonencode: # -*- coding: utf-8 -*- import json from datetime import date, datetime cla ...
- 【bzoj3211】花神游历各国&&【bzoj3038】上帝造题的七分钟2
bzoj3038]上帝造题的七分钟2 Description XLk觉得<上帝造题的七分钟>不太过瘾,于是有了第二部. “第一分钟,X说,要有数列,于是便给定了一个正整数数列. 第二分钟, ...
- symfony3常用记忆
1.控制器里获取当前用户信息 $user = $this->getUser(); 2.判断当前用户是否登录 // yay! Use this to see if the user is logg ...
- 调用SMTP发送有附件的邮件
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- (转)python爬虫----(scrapy框架提高(1),自定义Request爬取)
摘要 之前一直使用默认的parse入口,以及SgmlLinkExtractor自动抓取url.但是一般使用的时候都是需要自己写具体的url抓取函数的. python 爬虫 scrapy scrapy提 ...
- Windows下VS2013创建与使用动态链接库(.dll)
一.创建动态链接库文件 ** 1.打开VS2013,选择文件,新建工程 2.选择新建W32控制台应用程序,这里将工程名改为makeDLL 3.在应用程序类型中选择DLL,点击完成 4.完成以上步 ...
- Scala学习随笔——控制语句
Scala只内置了为数不多的几种程序控制语句:if.while.for.try catch以及函数调用,这是因为从Scala诞生开始就包含了函数字面量,Scala内核没有定义过多的控制结构,而是可以通 ...
- MPchartAnadroid的对比柱状图
1.导入三方jar包(可参照) MPAndroidChart(GitHub上优秀得图表功能库) MPAndroidChart常见设置属性(一)——应用层 2.布局activity_main.xml(这 ...
- hdu 5101(思路题)
Select Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...