parser-combinator
Version:
Parser combinators
93 lines (83 loc) • 2.85 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
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; }; }();
exports.default = function () {
if (arguments.length === 1 && Array.isArray(arguments[0])) {
return new List(arguments[0]);
}
return new List(Array.prototype.slice.call(arguments));
};
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
/*
* Parsec
* https://github.com/d-plaindoux/parsec
*
* Copyright (c) 2015-2016 Didier Plaindoux
* Licensed under the LGPL2 license.
*/
var List = function () {
function List(value) {
_classCallCheck(this, List);
this.value = value;
}
_createClass(List, [{
key: "size",
value: function size() {
return this.value.length;
}
}, {
key: "isEmpty",
value: function isEmpty() {
return this.value.length === 0;
}
}, {
key: "add",
value: function add(element) {
return new List(this.value.concat([element]));
}
}, {
key: "append",
value: function append(list) {
return new List(this.value.concat(list.value));
}
}, {
key: "filter",
value: function filter(funcall) {
var result = [];
for (var i = 0; i < this.value.length; i++) {
if (funcall(this.value[i])) {
result.push(this.value[i]);
}
}
return new List(result);
}
}, {
key: "map",
value: function map(funcall) {
return new List(this.value.map(funcall));
}
}, {
key: "flatmap",
value: function flatmap(funcall) {
var result = new List([]);
this.value.forEach(function (value) {
result = result.append(funcall(value));
});
return result;
}
}, {
key: "array",
value: function array() {
return this.value.slice();
}
}, {
key: "join",
value: function join(sep) {
return this.value.join(sep);
}
}]);
return List;
}();
//# sourceMappingURL=list.js.map