XReact

Crossing two reactx monads.

by C4Phone

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-beta1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css">
<!-- Bootstrap -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>

<form class="modal-dialog">
  Adjustment (in milliseconds) =
  <input id="adjustment" class="form-control" />
  <br /> Current time = <pre id="showTime"> </pre>
  <br /> Answer = <pre id="answer"> </pre>
</form>


<script>
  var initPipe = x => () => x;

  var setGetAgent = () => {
    var state = {
      value: undefined
    };
    return {
      get: key => (pipeState) => {
        pipeState[key] = state.value;
        return pipeState;
      },
      set: key => (pipeState) => {
        state.value = pipeState[key];
        return pipeState;
      },
      impericalSet: val => {
        state.value = val;
      }
    };
  };

  var adjustAgent = setGetAgent();
  var timeAgent = setGetAgent();

  var calcAdjust = (keyAdjust, keyTime, keyAns) => (pipeState) => {
    ans = new Date(pipeState[keyTime].getTime() + pipeState[keyAdjust]);
    pipeState[keyAns] = ans;
    return pipeState;
  };

  var tracePipe = pipe => {
    console.log("pipe: ", pipe);
    return pipe;
  }

  var updateAnswer = pipeState =>
    Promise.resolve({})
    .then(adjustAgent.get('x'))
    .then(timeAgent.get('y'))
    .then(calcAdjust('x', 'y', 'ans'))
    .then(dict => $("#answer").html(dict.ans))
    // recover pipeState of outer Promise
    .then(() => pipeState);

  var updateTime = pipeState =>
    Promise.resolve({})
    .then(timeAgent.get('ans'))
    .then(({ans}) => $("#showTime").html(ans))
    // recover pipeState of outer Promise
    .then(() => pipeState);

 ...