peterschussheim
10/28/2016 - 5:28 PM

maybe-monad

maybe-monad

"use strict";

var _createClass = function() {
  function defineProperties(target, props) {
    for (var i = 0; i < props.length; i++) {
      var descriptor = props[i];
      descriptor.enumerable = descriptor.enumerable || false;
      descriptor.configurable = true;
      if ("value" in descriptor) descriptor.writable = true;
      Object.defineProperty(target, descriptor.key, descriptor);
    }
  }
  return function(Constructor, protoProps, staticProps) {
    if (protoProps) defineProperties(Constructor.prototype, protoProps);
    if (staticProps) defineProperties(Constructor, staticProps);
    return Constructor;
  };
}();

function _classCallCheck(instance, Constructor) {
  if (!(instance instanceof Constructor)) {
    throw new TypeError("Cannot call a class as a function");
  }
}

var Queue = function() {

  function Queue() {
    _classCallCheck(this, Queue);

    this.list = [];
    this.length = 0;
  }

  _createClass(Queue, [{
      key: "enqueue",
      value: function enqueue(value) {
        this.length++;
        this.list.push(value);
      }

    },

    {
      key: "dequeue",
      value: function dequeue() {
        // Don't do anything if we don't have any items.
        if (this.length === 0) return;

        // Shift the first item off the start of the list and return the value.
        this.length--;
        return this.list.shift();
      }

      /**
       * Same as stacks we're going to define a "peek" function for getting the next
       * value without removing it from the queue.
       */

    }, {
      key: "peek",
      value: function peek() {
        return this.list[0];
      }
    }
  ]);

  return Queue;
}();

maybe-monad

This Gist was automatically created by Carbide, a free online programming environment.

You can view a live, interactive version of this Gist here.