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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [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 ...

  4. [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 ...

  5. [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 ...

  6. [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 ...

  7. [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 ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. libaio.so.1: cannot open shared object file

    <pre code_snippet_id="275763" snippet_file_name="blog_20140404_1_5530152" nam ...

  2. 走近Docker

    一个容器实际上就是运行在宿主机上的一个进程,这个进程以及子进程会认为自己运行在一个独立的世界里. Docker相对于其他虚拟化技术的优势在于:创建.删除容器速度快,容器运行占用开销非常小.而相对于其他 ...

  3. Flask学习总结笔记推荐

    https://blog.csdn.net/kikaylee/article/category/6551426

  4. 自定义topo文件解析

    from mininet.topo import Topo from mininet.net import Mininet from mininet.util import dumpNodeConne ...

  5. 使用jquery实现省市二级列表

    这里讲用到 jquery 的  each  遍历方法  追加 节点或元素方法  append  appendTO   以及 remove 清除节点 <script> $(function( ...

  6. Centos7部署Open-Falcon监控

    参考博主:努力哥完成 一.Open-Falcon介绍 1.监控系统,可以从运营级别(基本配置即可),以及应用级别(二次开发,通过端口进行日志上报),对服务器.操作系统.中间件.应用进行全面的监控,及报 ...

  7. hdu 5139(离线处理+离散化下标)

    Formula Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  8. 如何让IE7,IE8支持css3

    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" /> 原理:在用ie浏览 ...

  9. Python的程序结构[4] -> 函数/Function[2] -> 匿名函数

    匿名函数 / Anonymous Function 匿名函数是一种不需要绑定函数名的函数 (i.e. functions that are not bound to a name).匿名函数通过 la ...

  10. POJ 3080-Blue Jeans【kmp,字符串剪接】

    Blue Jeans Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20695   Accepted: 9167 Descr ...