zoukankan      html  css  js  c++  java
  • [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}
  • 相关阅读:
    单片机中的类型转换
    vs2013CCyusb报错(CyAPI.obj)
    c/c++ 去掉空格函数
    keil关于正点原子的sys.h工程报错修改
    【C语言】华软C语言程序设计复习
    c/c++中,clock函数的用法和作用
    vs mfc出现错误“MSB8301”解决办法
    vs出现“未将对象引用到实例的错误”
    keil uv5 代码格式化
    嵌入式软件面试
  • 原文地址:https://www.cnblogs.com/Answer1215/p/10234219.html
Copyright © 2011-2022 走看看