code stringlengths 24 2.07M | docstring stringlengths 25 85.3k | func_name stringlengths 1 92 | language stringclasses 1
value | repo stringlengths 5 64 | path stringlengths 4 172 | url stringlengths 44 218 | license stringclasses 7
values |
|---|---|---|---|---|---|---|---|
notifyAboutKnownIssue = (summary, url, regexes) => {
let handler = () => showErrorDiv(
'A known problem is happening. :(',
summary,
'',
summary,
url);
for (let regex of regexes) {
knownIssueRegexes.push({regex, handler});
}
handler();
} | @param {!string} recovery A short description of what happened and how it was recovered from.
@param {*} context Details about what caused the error and how we recovered.
@param {*} error The exception object. | notifyAboutKnownIssue | javascript | Strilanc/Quirk | src/fallback.js | https://github.com/Strilanc/Quirk/blob/master/src/fallback.js | Apache-2.0 |
handler = () => showErrorDiv(
'A known problem is happening. :(',
summary,
'',
summary,
url) | @param {!string} recovery A short description of what happened and how it was recovered from.
@param {*} context Details about what caused the error and how we recovered.
@param {*} error The exception object. | handler | javascript | Strilanc/Quirk | src/fallback.js | https://github.com/Strilanc/Quirk/blob/master/src/fallback.js | Apache-2.0 |
handler = () => showErrorDiv(
'A known problem is happening. :(',
summary,
'',
summary,
url) | @param {!string} recovery A short description of what happened and how it was recovered from.
@param {*} context Details about what caused the error and how we recovered.
@param {*} error The exception object. | handler | javascript | Strilanc/Quirk | src/fallback.js | https://github.com/Strilanc/Quirk/blob/master/src/fallback.js | Apache-2.0 |
drawErrorBox = msg => {
let canvas = document.getElementById("drawCanvas");
if (canvas === undefined) {
return;
}
let ctx = canvas.getContext("2d");
ctx.font = '12px monospace';
let lines = msg.split("\n");
let w = 0;
for (let line of lines) {
w = Math.max(w, ctx.measureT... | @param {!string} recovery A short description of what happened and how it was recovered from.
@param {*} context Details about what caused the error and how we recovered.
@param {*} error The exception object. | drawErrorBox | javascript | Strilanc/Quirk | src/fallback.js | https://github.com/Strilanc/Quirk/blob/master/src/fallback.js | Apache-2.0 |
drawErrorBox = msg => {
let canvas = document.getElementById("drawCanvas");
if (canvas === undefined) {
return;
}
let ctx = canvas.getContext("2d");
ctx.font = '12px monospace';
let lines = msg.split("\n");
let w = 0;
for (let line of lines) {
w = Math.max(w, ctx.measureT... | @param {!string} recovery A short description of what happened and how it was recovered from.
@param {*} context Details about what caused the error and how we recovered.
@param {*} error The exception object. | drawErrorBox | javascript | Strilanc/Quirk | src/fallback.js | https://github.com/Strilanc/Quirk/blob/master/src/fallback.js | Apache-2.0 |
function onErrorHandler(errorMsg, url, lineNumber, columnNumber, errorObj) {
try {
let location = simplifySrcUrls(
((errorObj instanceof Object)? errorObj.stack : undefined) ||
(url + ":" + lineNumber + ":" + columnNumber));
let body = [
'URL',
do... | Draws a user-visible error box when problems occur. | onErrorHandler | javascript | Strilanc/Quirk | src/fallback.js | https://github.com/Strilanc/Quirk/blob/master/src/fallback.js | Apache-2.0 |
desiredCanvasSizeFor = curInspector => {
return {
w: Math.max(canvasDiv.clientWidth, curInspector.desiredWidth()),
h: curInspector.desiredHeight()
};
} | @param {!DisplayedInspector} curInspector
@returns {{w: number, h: !number}} | desiredCanvasSizeFor | javascript | Strilanc/Quirk | src/main.js | https://github.com/Strilanc/Quirk/blob/master/src/main.js | Apache-2.0 |
desiredCanvasSizeFor = curInspector => {
return {
w: Math.max(canvasDiv.clientWidth, curInspector.desiredWidth()),
h: curInspector.desiredHeight()
};
} | @param {!DisplayedInspector} curInspector
@returns {{w: number, h: !number}} | desiredCanvasSizeFor | javascript | Strilanc/Quirk | src/main.js | https://github.com/Strilanc/Quirk/blob/master/src/main.js | Apache-2.0 |
constructor(action, cooldownMs, slowActionCooldownPumpUpFactor=0, waitWithRequestAnimationFrame=false) {
/** @type {!function() : void} */
this.action = action;
/** @type {!number} */
this.cooldownDuration = cooldownMs;
/** @type {!number} */
this.slowActionCooldownPumpup... | @param {!function() : void} action
@param {!number} cooldownMs
@param {!number} slowActionCooldownPumpUpFactor
@param {!boolean=false} waitWithRequestAnimationFrame
@constructor | constructor | javascript | Strilanc/Quirk | src/base/CooldownThrottle.js | https://github.com/Strilanc/Quirk/blob/master/src/base/CooldownThrottle.js | Apache-2.0 |
trigger() {
switch (this._state) {
case 'idle':
this._triggerIdle();
break;
case 'waiting':
// Already triggered. Do nothing.
break;
case 'running':
// Re-trigger.
this._state = 'r... | Asks for the action to be performed as soon as possible.
(No effect if the action was already requested but not performed yet.) | trigger | javascript | Strilanc/Quirk | src/base/CooldownThrottle.js | https://github.com/Strilanc/Quirk/blob/master/src/base/CooldownThrottle.js | Apache-2.0 |
function equate(subject, other) {
if (subject === other || (isExactlyNaN(subject) && isExactlyNaN(other))) {
return true;
}
// Custom equality.
let customEquality = tryEquate_custom(subject, other);
if (customEquality !== undefined) {
return customEquality;
}
if (isAtomic(su... | Determines if two values are currently equivalent.
Values that are equal according to === are currently equivalent.
NaN is currently equivalent to NaN.
Values with an `isEqualTo` method are currently equivalent to values that return true when passed to that method.
Collections of the same type that contain currently e... | equate | javascript | Strilanc/Quirk | src/base/Equate.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Equate.js | Apache-2.0 |
function tryEquate_custom(subject, other) {
if (!isAtomic(subject) && subject.constructor.prototype.hasOwnProperty("isEqualTo")) {
return subject.isEqualTo(other);
}
if (!isAtomic(other) && other.constructor.prototype.hasOwnProperty("isEqualTo")) {
return other.isEqualTo(subject);
}
... | @param {*} subject
@param {*} other
@returns {undefined|!boolean} | tryEquate_custom | javascript | Strilanc/Quirk | src/base/Equate.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Equate.js | Apache-2.0 |
function eqType(subject, other) {
return subject.constructor.name === other.constructor.name;
} | @param {*} subject
@param {*} other
@returns {!boolean} | eqType | javascript | Strilanc/Quirk | src/base/Equate.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Equate.js | Apache-2.0 |
function equate_Indexables(subject, other) {
if (subject.length !== other.length) {
return false;
}
for (let i = 0; i < subject.length; i++) {
if (!equate(subject[i], other[i])) {
return false;
}
}
return true;
} | @param {!(*[])} subject
@param {!(*[])} other
@returns {!boolean} | equate_Indexables | javascript | Strilanc/Quirk | src/base/Equate.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Equate.js | Apache-2.0 |
function equate_Iterables(subject, other) {
let otherIter = other[Symbol.iterator]();
for (let subjectItem of subject) {
let otherItemDone = otherIter.next();
if (otherItemDone.done || !equate(subjectItem, otherItemDone.value)) {
return false;
}
}
return otherIter.nex... | @param {!Iterable} subject
@param {!Iterable} other
@returns {!boolean} | equate_Iterables | javascript | Strilanc/Quirk | src/base/Equate.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Equate.js | Apache-2.0 |
function equate_Maps(subject, other) {
if (subject.size !== other.size) {
return false;
}
for (let [k, v] of subject) {
//noinspection JSUnusedAssignment
if (!other.has(k)) {
return false;
}
//noinspection JSUnusedAssignment
let otherV = other.get(... | @param {!Map} subject
@param {!Map} other
@returns {!boolean} | equate_Maps | javascript | Strilanc/Quirk | src/base/Equate.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Equate.js | Apache-2.0 |
function equate_Sets(subject, other) {
if (subject.size !== other.size) {
return false;
}
for (let k of subject) {
if (!other.has(k)) {
return false;
}
}
return true;
} | @param {!Set} subject
@param {!Set} other
@returns {!boolean} | equate_Sets | javascript | Strilanc/Quirk | src/base/Equate.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Equate.js | Apache-2.0 |
function equate_Objects(subject, other) {
let keys = objectKeys(subject);
if (!equate_Sets(keys, objectKeys(other))) {
return false;
}
for (let k of keys) {
if (k === Symbol.iterator) {
continue;
}
if (!equate(subject[k], other[k])) {
return false... | @param {!object} subject
@param {!object} other
@returns {!boolean} | equate_Objects | javascript | Strilanc/Quirk | src/base/Equate.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Equate.js | Apache-2.0 |
constructor(allowAbbreviation, maxAbbreviationError, fixedDigits, itemSeparator) {
this.allowAbbreviation = allowAbbreviation;
this.maxAbbreviationError = maxAbbreviationError;
this.fixedDigits = fixedDigits;
this.itemSeparator = itemSeparator;
} | @param {!boolean} allowAbbreviation
@param {!number} maxAbbreviationError
@param {int|undefined} fixedDigits
@param {!string} itemSeparator
@property {!boolean} allowAbbreviation Should outputs be shortened, if possible?
@property {!number} maxAbbreviationError How much error is abbreviating allowed to introduce?
@pro... | constructor | javascript | Strilanc/Quirk | src/base/Format.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Format.js | Apache-2.0 |
formatFloat(f) {
if (this.allowAbbreviation) {
return abbreviateFloat(f, this.maxAbbreviationError, this.fixedDigits);
}
if (this.fixedDigits !== undefined) {
return f.toFixed(this.fixedDigits);
}
return f + "";
} | @param {!boolean} allowAbbreviation
@param {!number} maxAbbreviationError
@param {int|undefined} fixedDigits
@param {!string} itemSeparator
@property {!boolean} allowAbbreviation Should outputs be shortened, if possible?
@property {!number} maxAbbreviationError How much error is abbreviating allowed to introduce?
@pro... | formatFloat | javascript | Strilanc/Quirk | src/base/Format.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Format.js | Apache-2.0 |
static parseFloat(text) {
if (text.length === 0) {
throw new Error("Not a number: '" + text + "'");
}
if (text[0] === "-") {
return -Format.parseFloat(text.substr(1));
}
if (text[0] === "\u221A") {
return Math.sqrt(Format.parseFloat(text.substr... | Parses the given text into a float. Works for text created by Format#formatFloat.
@param {!string} text
@throws
@returns {!number} | parseFloat | javascript | Strilanc/Quirk | src/base/Format.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Format.js | Apache-2.0 |
static simplifyByRounding(value, epsilon) {
if (value < 0) {
return -Format.simplifyByRounding(-value, epsilon);
}
let r = value % 1;
if (r <= epsilon || 1 - r <= epsilon) {
return Math.round(value);
}
let fraction = match(UNICODE_FRACTIONS, e =>... | Corrects a value to a nearby simple fraction or root thereof, such as sqrt(1/2), so it can be printed compactly.
@param {!number} value The value to round.
@param {!number} epsilon The maximum offset error introduced by the rounding. | simplifyByRounding | javascript | Strilanc/Quirk | src/base/Format.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Format.js | Apache-2.0 |
match = function(array, predicate) {
for (let item of array) {
if (predicate(item)) {
return item;
}
}
return undefined;
} | Returns the first element of an array matching the given predicate, or else returns undefined. | match | javascript | Strilanc/Quirk | src/base/Format.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Format.js | Apache-2.0 |
match = function(array, predicate) {
for (let item of array) {
if (predicate(item)) {
return item;
}
}
return undefined;
} | Returns the first element of an array matching the given predicate, or else returns undefined. | match | javascript | Strilanc/Quirk | src/base/Format.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Format.js | Apache-2.0 |
function abbreviateFloat(value, epsilon=0, digits=undefined) {
if (Math.abs(value) < epsilon) {
return "0";
}
if (value < 0) {
return "-" + abbreviateFloat(-value, epsilon, digits);
}
let fraction = match(UNICODE_FRACTIONS, e => Math.abs(e.value - value) <= epsilon);
if (fractio... | Returns a string representation of a float, taking advantage of unicode fractions and square roots.
@param {!number} value The value to represent as a string.
@param {!number=} epsilon The maximum error introduced by using an expression.
@param {!number|undefined=} digits The number of digits to use if no expression m... | abbreviateFloat | javascript | Strilanc/Quirk | src/base/Format.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Format.js | Apache-2.0 |
constructor(subscribe) {
/**
* @type {!(function(!(function(T): void)): !(function(): void))}
* @template T
* @private
*/
this._subscribe = subscribe;
} | @param {!function(!function(T):void): (!function():void)} subscribe
@template T | constructor | javascript | Strilanc/Quirk | src/base/Obs.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Obs.js | Apache-2.0 |
subscribe(observer) {
return this._subscribe(observer);
} | @param {!function(T):void} observer
@returns {!function():void} unsubscriber
@template T | subscribe | javascript | Strilanc/Quirk | src/base/Obs.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Obs.js | Apache-2.0 |
static of(...items) {
return new Observable(observer => {
for (let item of items) {
observer(item);
}
return () => {};
});
} | @param {T} items
@returns {!Observable.<T>} An observable that immediately forwards all the given items to any new subscriber.
@template T | of | javascript | Strilanc/Quirk | src/base/Obs.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Obs.js | Apache-2.0 |
snapshot() {
let result = [];
let unsub = this.subscribe(e => result.push(e));
unsub();
return result;
} | Subscribes to the receiving observable for a moment and returns any collected items.
@returns {!Array.<T>}
@template T | snapshot | javascript | Strilanc/Quirk | src/base/Obs.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Obs.js | Apache-2.0 |
map(transformFunc) {
return new Observable(observer => this.subscribe(item => observer(transformFunc(item))));
} | @param {!function(TIn) : TOut} transformFunc
@returns {!Observable.<TOut>} An observable with the same items, but transformed by the given function.
@template TIn, TOut | map | javascript | Strilanc/Quirk | src/base/Obs.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Obs.js | Apache-2.0 |
filter(predicate) {
return new Observable(observer => this.subscribe(item => { if (predicate(item)) { observer(item); }}));
} | @param {!function(T) : !boolean} predicate
@returns {!Observable.<T>} An observable with the same items, but skipping items that don't match the predicate.
@template T | filter | javascript | Strilanc/Quirk | src/base/Obs.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Obs.js | Apache-2.0 |
zipLatest(other, mergeFunc) {
return new Observable(observer => {
let has1 = false;
let has2 = false;
let last1;
let last2;
let unreg1 = this.subscribe(e1 => {
last1 = e1;
has1 = true;
if (has2) {
... | @param {!Observable.<T2>} other
@param {!function(T1, T2): TOut} mergeFunc
@returns {!Observable.<TOut>}
@template T1, T2, TOut | zipLatest | javascript | Strilanc/Quirk | src/base/Obs.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Obs.js | Apache-2.0 |
static requestAnimationTicker() {
return new Observable(observer => {
let iter;
let isDone = false;
iter = () => {
if (!isDone) {
observer(undefined);
window.requestAnimationFrame(iter);
}
};
... | Returns an observable that keeps requesting animations frame callbacks and calling observers when they arrive.
@returns {!Observable.<undefined>} | requestAnimationTicker | javascript | Strilanc/Quirk | src/base/Obs.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Obs.js | Apache-2.0 |
flattenLatest() {
return new Observable(observer => {
let unregLatest = () => {};
let isDone = false;
let unregAll = this.subscribe(subObservable => {
if (isDone) {
return;
}
let prevUnreg = unregLatest;
... | @returns {!Observable.<T>} An observable that subscribes to each sub-observables arriving on this observable
in turns, only forwarding items from the latest sub-observable.
@template T | flattenLatest | javascript | Strilanc/Quirk | src/base/Obs.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Obs.js | Apache-2.0 |
peek(action) {
return this.map(e => { action(e); return e; });
} | @param {!function(T):void} action
@returns {!Observable.<T>}
@template T | peek | javascript | Strilanc/Quirk | src/base/Obs.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Obs.js | Apache-2.0 |
flatten() {
return new Observable(observer => {
let unsubs = [];
unsubs.push(this.subscribe(observable => unsubs.push(observable.subscribe(observer))));
return () => {
for (let unsub of unsubs) {
unsub()
}
}
... | @returns {!Observable.<T>} An observable that forwards all the items from all the observables observed by the
receiving observable of observables.
@template T | flatten | javascript | Strilanc/Quirk | src/base/Obs.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Obs.js | Apache-2.0 |
throttleLatest(cooldownMillis) {
return new Observable(observer => {
let latest = undefined;
let isKilled = false;
let throttle = new CooldownThrottle(() => {
if (!isKilled) {
observer(latest);
}
}, cooldownMilli... | Starts a timer after each completed send, delays sending any more values until the timer expires, and skips
intermediate values when a newer value arrives from the source while the timer is still running down.
@param {!number} cooldownMillis
@returns {!Observable.<T>}
@template T | throttleLatest | javascript | Strilanc/Quirk | src/base/Obs.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Obs.js | Apache-2.0 |
static elementEvent(element, eventKey) {
return new Observable(observer => {
element.addEventListener(eventKey, observer);
return () => element.removeEventListener(eventKey, observer);
});
} | @param {!HTMLElement|!HTMLDocument} element
@param {!string} eventKey
@returns {!Observable.<*>} An observable corresponding to an event fired from an element. | elementEvent | javascript | Strilanc/Quirk | src/base/Obs.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Obs.js | Apache-2.0 |
skip(count) {
return new Observable(observer => {
let remaining = count;
return this.subscribe(item => {
if (remaining > 0) {
remaining -= 1;
} else {
observer(item);
}
})
})
} | @param {!int} count
@returns {!Observable.<T>}
@template T | skip | javascript | Strilanc/Quirk | src/base/Obs.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Obs.js | Apache-2.0 |
whenDifferent(equater = undefined) {
let eq = equater || ((e1, e2) => e1 === e2);
return new Observable(observer => {
let hasLast = false;
let last = undefined;
return this.subscribe(item => {
if (!hasLast || !eq(last, item)) {
last... | @returns {!Observable.<T>} An observable with the same events, but filtering out any event value that's the same
as the previous one.
@template T | whenDifferent | javascript | Strilanc/Quirk | src/base/Obs.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Obs.js | Apache-2.0 |
constructor() {
/**
* @type {!Array.<!function(T):void>}
* @private
* @template T
*/
this._observers = [];
/**
* @type {!Observable.<T>}
* @private
* @template T
*/
this._observable = new Observable(observer => {
... | @returns {!Observable.<T>} An observable with the same events, but filtering out any event value that's the same
as the previous one.
@template T | constructor | javascript | Strilanc/Quirk | src/base/Obs.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Obs.js | Apache-2.0 |
random() {
let p = this._next++;
if (p >= this._cache.length) {
this._cache.push(Math.random());
}
return this._cache[p];
} | @returns {!number} A random number uniformly sampled from [0, 1). | random | javascript | Strilanc/Quirk | src/base/RestartableRng.js | https://github.com/Strilanc/Quirk/blob/master/src/base/RestartableRng.js | Apache-2.0 |
static startingAt(state) {
return new Revision([state], 0, false);
} | Returns a cleared revision history, starting at the given state.
@param {*} state | startingAt | javascript | Strilanc/Quirk | src/base/Revision.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Revision.js | Apache-2.0 |
clear(state) {
this.history = [state];
this.index = 0;
this.isWorkingOnCommit = false;
this._changes.send(state);
this._latestActiveCommit.set(state);
} | Throws away all revisions and resets the given state.
@param {*} state
@returns {void} | clear | javascript | Strilanc/Quirk | src/base/Revision.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Revision.js | Apache-2.0 |
startedWorkingOnCommit() {
this.isWorkingOnCommit = true;
this._changes.send(undefined);
} | Indicates that there are pending changes, so that a following 'undo' will return to the current state instead of
the previous state.
@returns {void} | startedWorkingOnCommit | javascript | Strilanc/Quirk | src/base/Revision.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Revision.js | Apache-2.0 |
cancelCommitBeingWorkedOn() {
this.isWorkingOnCommit = false;
let result = this.history[this.index];
this._changes.send(result);
this._latestActiveCommit.set(result);
return result;
} | Indicates that pending changes were discarded, so that a following 'undo' should return to the previous state
instead of the current state.
@returns {*} The new current state. | cancelCommitBeingWorkedOn | javascript | Strilanc/Quirk | src/base/Revision.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Revision.js | Apache-2.0 |
commit(newCheckpoint) {
if (newCheckpoint === this.history[this.index]) {
this.cancelCommitBeingWorkedOn();
return;
}
this.isWorkingOnCommit = false;
this.index += 1;
this.history.splice(this.index, this.history.length - this.index);
this.history.p... | Throws away future states, appends the given state, and marks it as the current state
@param {*} newCheckpoint
@returns {void} | commit | javascript | Strilanc/Quirk | src/base/Revision.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Revision.js | Apache-2.0 |
redo() {
if (this.index + 1 === this.history.length) {
return undefined;
}
this.index += 1;
this.isWorkingOnCommit = false;
let result = this.history[this.index];
this._changes.send(result);
this._latestActiveCommit.set(result);
return result;
... | Marks the next state as the current state and returns it (or does nothing if there is no next state).
@returns {undefined|*} The new current state, or undefined if there's nothing to redo. | redo | javascript | Strilanc/Quirk | src/base/Revision.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Revision.js | Apache-2.0 |
toString() {
return 'Revision(' + describe({
index: this.index,
count: this.history.length,
workingOnCommit: this.isWorkingOnCommit,
head: this.history[this.index]
}) + ')';
} | @returns {!string} A description of the revision. | toString | javascript | Strilanc/Quirk | src/base/Revision.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Revision.js | Apache-2.0 |
isEqualTo(other) {
return other instanceof Revision &&
this.index === other.index &&
this.isWorkingOnCommit === other.isWorkingOnCommit &&
equate(this.history, other.history);
} | Determines if two revisions currently have the same state.
@param {*|!Revision} other
@returns {!boolean} | isEqualTo | javascript | Strilanc/Quirk | src/base/Revision.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Revision.js | Apache-2.0 |
emptyFallback = (result, alternative, errorMessage) => {
if (result !== EMPTY_SYGIL) {
return result;
}
if (alternative === THROW_IF_EMPTY) {
throw new Error(errorMessage);
}
return alternative;
} | A private sygil/sentinel value that shouldn't ever be present in a sequence, and so can be used as a placeholder for
"not set yet" (unlike undefined or null, which are allowed to appear in sequences). | emptyFallback | javascript | Strilanc/Quirk | src/base/Seq.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Seq.js | Apache-2.0 |
emptyFallback = (result, alternative, errorMessage) => {
if (result !== EMPTY_SYGIL) {
return result;
}
if (alternative === THROW_IF_EMPTY) {
throw new Error(errorMessage);
}
return alternative;
} | A private sygil/sentinel value that shouldn't ever be present in a sequence, and so can be used as a placeholder for
"not set yet" (unlike undefined or null, which are allowed to appear in sequences). | emptyFallback | javascript | Strilanc/Quirk | src/base/Seq.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Seq.js | Apache-2.0 |
constructor(obj, isIteratorFunction=false) {
let iterable;
let iterator;
if (obj instanceof Seq) {
// Avoid double-wrapping.
iterable = obj._iterable;
iterator = obj[Symbol.iterator];
} else if (isIteratorFunction) {
iterable = {[Symbol.ite... | Wraps the given array, collection, or other iterable.
Use fromGenerator for wrapping generator functions.
@param {!(T[])|!Seq.<T>|!Iterable.<T>|*} obj
@param {!boolean=} isIteratorFunction
@template T | constructor | javascript | Strilanc/Quirk | src/base/Seq.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Seq.js | Apache-2.0 |
static fromGenerator(generatorFunction) {
return new Seq(generatorFunction, true);
} | Creates a re-usable iterable from a generator function like <code>function*() { yield 1; }</code>.
Note that the obvious alternative, <code>new Seq(function*(){yield 1;}()}</code>, stops working after the
iterable has been iterated once.
@param {!function() : Iterator.<T>} generatorFunction
@returns {!Seq.<T>}
@templ... | fromGenerator | javascript | Strilanc/Quirk | src/base/Seq.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Seq.js | Apache-2.0 |
isEqualTo(other, comparator = (e1, e2) => e1 === e2) {
if (!isIterable(other)) {
return false;
}
if (other === this) {
return true;
}
let iter2 = other[Symbol.iterator]();
for (let e1 of this._iterable) {
let e2 = iter2.next();
... | Determines if the given iterable contains the same items as this sequence.
@param {*|!(T[])|!Seq.<T>|!Iterable.<T>} other
@param {!function(T, T|*) : !boolean} comparator
@template T | isEqualTo | javascript | Strilanc/Quirk | src/base/Seq.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Seq.js | Apache-2.0 |
toArray() {
return Array.from(this._iterable);
} | Returns an array containing the items of this sequence.
@returns {!(T[])}
@template T | toArray | javascript | Strilanc/Quirk | src/base/Seq.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Seq.js | Apache-2.0 |
toFloat32Array() {
let n = this.tryPeekCount();
if (n === undefined) {
return new Float32Array(this.toArray());
}
let buf = new Float32Array(n);
let i = 0;
for (let item of this._iterable) {
buf[i++] = item;
}
return buf;
} | Returns a Float32Array containing the items of this sequence.
@returns {!Float32Array} | toFloat32Array | javascript | Strilanc/Quirk | src/base/Seq.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Seq.js | Apache-2.0 |
toSet() {
return new Set(this._iterable);
} | Returns a set containing the distinct items of this sequence.
@returns {!Set.<T>}
@template T | toSet | javascript | Strilanc/Quirk | src/base/Seq.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Seq.js | Apache-2.0 |
join(joiner) {
return this.toArray().join(joiner);
} | Returns a string representation of the receiving sequence's items, separated by the given joiner.
@param {!string} joiner
@returns {!string} | join | javascript | Strilanc/Quirk | src/base/Seq.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Seq.js | Apache-2.0 |
toString() {
return `Seq[${this.join(", ")}]`;
} | Returns a string representation of the receiving sequence.
@returns {!string} | toString | javascript | Strilanc/Quirk | src/base/Seq.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Seq.js | Apache-2.0 |
static range(count) {
if (!Number.isInteger(count) || count < 0) {
throw new DetailedError("bad count", {count});
}
return Seq.fromGenerator(function*() {
for (let i = 0; i < count; i++) {
yield i;
}
});
} | Returns a sequence of natural numbers, starting at 0 and incrementing until just before the given count.
@param {!int} count
@returns {!Seq.<!int>} | range | javascript | Strilanc/Quirk | src/base/Seq.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Seq.js | Apache-2.0 |
static naturals() {
return Seq.fromGenerator(function*() {
let i = 0;
//noinspection InfiniteLoopJS
while (true) { //eslint-disable-line no-constant-condition
yield i;
i++;
}
})
} | Returns the sequence of natural numbers, starting at 0 and incrementing without bound.
@returns {!Seq.<!int>} | naturals | javascript | Strilanc/Quirk | src/base/Seq.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Seq.js | Apache-2.0 |
static repeat(item, repeatCount) {
if (repeatCount < 0) {
throw new Error("needed repeatCount >= 0");
}
return Seq.fromGenerator(function*() {
for (let i = 0; i < repeatCount; i++) {
yield item;
}
})
} | Returns a sequence of the same item repeated the given number of times.
@param {T} item
@param {!int} repeatCount
@returns {!Seq.<T>}
@template T | repeat | javascript | Strilanc/Quirk | src/base/Seq.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Seq.js | Apache-2.0 |
solidify() {
let knownSolidTypes = [
Float32Array,
Float64Array,
Int8Array,
Int16Array,
Int32Array,
Uint8Array,
Uint16Array,
Uint32Array
];
if (Array.isArray(this._iterable)) {
return thi... | Returns a sequence with the same items, but precomputed and stored. If the sequence is already solid, e.g. it is
backed by an array, then it is returned directly (and unchanged).
@returns {!Seq.<T>}
@template T | solidify | javascript | Strilanc/Quirk | src/base/Seq.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Seq.js | Apache-2.0 |
map(projection) {
let seq = this._iterable;
return Seq.fromGenerator(function*() {
for (let e of seq) {
yield projection(e);
}
});
} | Returns a sequence iterating the results of applying a transformation to the items of the receiving sequence.
@param {!function(T): R} projection
@returns {!Seq.<T>}
@template T, R | map | javascript | Strilanc/Quirk | src/base/Seq.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Seq.js | Apache-2.0 |
mapWithIndex(projection) {
let seq = this._iterable;
return Seq.fromGenerator(function*() {
let i = 0;
for (let e of seq) {
yield projection(e, i);
i += 1;
}
});
} | Returns a sequence iterating the results of applying a transformation to the indexed items of the receiving
sequence.
@param {!function(T, int): R} projection
@returns {!Seq.<T>}
@template T, R | mapWithIndex | javascript | Strilanc/Quirk | src/base/Seq.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Seq.js | Apache-2.0 |
flatMap(sequenceProjection) {
let seq = this._iterable;
return Seq.fromGenerator(function*() {
for (let e of seq) {
yield* sequenceProjection(e);
}
});
} | Returns a sequence iterating the concatenated results of applying an iterable-returning transformation to the
items of the receiving sequence.
@param {!function(T): !Iterable<R>} sequenceProjection
@returns {!Seq.<T>}
@template T, R | flatMap | javascript | Strilanc/Quirk | src/base/Seq.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Seq.js | Apache-2.0 |
filter(predicate) {
let seq = this._iterable;
return Seq.fromGenerator(function*() {
for (let e of seq) {
if (predicate(e)) {
yield e;
}
}
});
} | Returns a sequence iterating the items of the receiving sequence that match a predicate. Items that don't match
the predicate, by causing it to return a falsy value, are skipped.
@param {!function(T) : !boolean} predicate
@returns {!Seq.<T>}
@template T | filter | javascript | Strilanc/Quirk | src/base/Seq.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Seq.js | Apache-2.0 |
filterWithIndex(predicate) {
let seq = this._iterable;
return Seq.fromGenerator(function*() {
let i = 0;
for (let e of seq) {
if (predicate(e, i)) {
yield e;
}
i++;
}
});
} | Returns a sequence iterating the items of the receiving sequence that match a predicate over the item and the
item's index. Items that don't match the predicate are skipped.
@param {!function(T, int) : !boolean} predicate
@returns {!Seq.<T>}
@template T | filterWithIndex | javascript | Strilanc/Quirk | src/base/Seq.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Seq.js | Apache-2.0 |
fold(combiner, emptyErrorAlternative = THROW_IF_EMPTY) {
let accumulator = EMPTY_SYGIL;
for (let e of this._iterable) {
accumulator = accumulator === EMPTY_SYGIL ? e : combiner(accumulator, e);
}
return emptyFallback(
accumulator,
emptyErrorAlternative... | Combines the items of a sequence into a single result by iteratively applying a combining function. If the
sequence is empty, then either an error is thrown or the given alternative value is returned.
@param {!function(T, T) : T} combiner
@param {=A} emptyErrorAlternative The value to return if the sequence is empty. I... | fold | javascript | Strilanc/Quirk | src/base/Seq.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Seq.js | Apache-2.0 |
aggregate(seed, aggregator) {
let accumulator = seed;
for (let e of this._iterable) {
accumulator = aggregator(accumulator, e);
}
return accumulator;
} | Combines the items of a sequence into a single result by starting with a seed accumulator and iteratively
applying an aggregation function to the accumulator and next item to get the next accumulator.
@param {!function(A, T) : A} aggregator Computes the next accumulator value.
@param {A} seed The initial accumulator va... | aggregate | javascript | Strilanc/Quirk | src/base/Seq.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Seq.js | Apache-2.0 |
zip(other, combiner) {
let seq = this._iterable;
return Seq.fromGenerator(function*() {
let iter2 = other[Symbol.iterator]();
for (let item1 of seq) {
let item2 = iter2.next();
if (item2.done) {
break;
}
... | Combines this sequence with another by passing items with the same index through a combining function.
If one sequence is longer than the other, the lonely tail is discarded.
@param {!(T2[])|!Seq.<T2>|!Iterable.<T2>} other
@param {!function(T, T2) : R} combiner
@returns {!Seq.<R>}
@template T, T2, R | zip | javascript | Strilanc/Quirk | src/base/Seq.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Seq.js | Apache-2.0 |
max(emptyErrorAlternative = THROW_IF_EMPTY) {
return this.fold((e1, e2) => e1 < e2 ? e2 : e1, emptyErrorAlternative);
} | Returns the largest value in the sequence, as determined by the `<` operator. If the sequence is empty, then
either an error is thrown or the given alternative value is returned.
@param {=A} emptyErrorAlternative The value to return if the sequence is empty. If not provided, an error
is thrown when the sequence is emp... | max | javascript | Strilanc/Quirk | src/base/Seq.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Seq.js | Apache-2.0 |
min(emptyErrorAlternative = THROW_IF_EMPTY) {
return this.fold((e1, e2) => e1 < e2 ? e1 : e2, emptyErrorAlternative);
} | Returns the smallest value in the sequence, as determined by the `<` operator. If the sequence is empty, then
either an error is thrown or the given alternative value is returned.
@param {=A} emptyErrorAlternative The value to return if the sequence is empty. If not provided, an error
is thrown when the sequence is em... | min | javascript | Strilanc/Quirk | src/base/Seq.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Seq.js | Apache-2.0 |
maxBy(projection, emptyErrorAlternative = THROW_IF_EMPTY, isALessThanBComparator = (e1, e2) => e1 < e2) {
let curMaxItem = EMPTY_SYGIL;
let curMaxScore = EMPTY_SYGIL;
for (let item of this._iterable) {
// Delay computing the score for the first item, so that singleton lists never tou... | Returns the highest-scoring item in the sequence, as determined by a scoring function.
@param {!function(T) : !number} projection Determines the score of an item.
@param {=A} emptyErrorAlternative The value to return if the sequence is empty. If not provided, an error
is thrown when the sequence is empty.
@param {(fun... | maxBy | javascript | Strilanc/Quirk | src/base/Seq.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Seq.js | Apache-2.0 |
minBy(projection, emptyErrorAlternative = THROW_IF_EMPTY, isALessThanBComparator = (e1, e2) => e1 < e2) {
return this.maxBy(projection, emptyErrorAlternative, (e1, e2) => isALessThanBComparator(e2, e1));
} | Returns the lowest-scoring item in the sequence, as determined by a scoring function.
@param {!function(T) : !number} projection Determines the score of an item.
@param {=A} emptyErrorAlternative The value to return if the sequence is empty. If not provided, an error
is thrown when the sequence is empty.
@param {(func... | minBy | javascript | Strilanc/Quirk | src/base/Seq.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Seq.js | Apache-2.0 |
any(predicate) {
for (let e of this._iterable) {
if (predicate(e)) {
return true;
}
}
return false;
} | Determines if any of the items in the sequence matches the given predicate.
@param {!function(T) : !boolean} predicate
@returns {!boolean}
@template T | any | javascript | Strilanc/Quirk | src/base/Seq.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Seq.js | Apache-2.0 |
every(predicate) {
return !this.any(e => !predicate(e));
} | Determines if every item in the sequence matches the given predicate.
@param {!function(T) : !boolean} predicate
@returns {!boolean}
@template T | every | javascript | Strilanc/Quirk | src/base/Seq.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Seq.js | Apache-2.0 |
contains(value) {
return this.any(e => e === value);
} | Determines if the sequence contains a given value or not, as determined by the <code>===</code> operator.
@param {T|*} value
@returns {!boolean}
@template T | contains | javascript | Strilanc/Quirk | src/base/Seq.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Seq.js | Apache-2.0 |
sum() {
let total = 0;
let first = true;
for (let e of this._iterable) {
total = first ? e : total + e;
first = false;
}
return total;
} | Adds up the numbers in the sequence, using the `+` operator, and returns the total.
The empty sum is defined to be 0, to satisfy the invariant that X.concat([s]).sum() === X.sum() + s.
@returns {T|!number|*}
@template T | sum | javascript | Strilanc/Quirk | src/base/Seq.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Seq.js | Apache-2.0 |
product() {
return this.fold((a, e) => a * e, 1);
} | Multiplies up the numbers in the sequence, using the `*` operator, and returns the total.
The empty product is defined to be 1, to satisfy the invariant that X.concat([s]).product() === X.product() * s.
@returns {T|!number|*}
@template T | product | javascript | Strilanc/Quirk | src/base/Seq.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Seq.js | Apache-2.0 |
scan(seed, aggregator) {
let seq = this._iterable;
return Seq.fromGenerator(function*() {
let accumulator = seed;
yield accumulator;
for (let e of seq) {
accumulator = aggregator(accumulator, e);
yield accumulator;
}
... | Accumulates the items of a sequence into a seed, while yielding the results. For example,
<code>[1, 2, 3].scan((a, e) => a + e, "a")</code> yields <code>["a", "a1", "a12", "a123"]</code> and
<code>[1, 2, 3].scan((e1, e2) => e1 + e2, 0)</code> yields <code>[0, 1, 3, 6]</code<.
@param {A} seed
@param {!function(A, T) : ... | scan | javascript | Strilanc/Quirk | src/base/Seq.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Seq.js | Apache-2.0 |
reverse() {
return new Seq(this.toArray().reverse());
} | Returns a sequence containing the same items, but in the opposite order.
@returns {!Seq.<T>}
@template T | reverse | javascript | Strilanc/Quirk | src/base/Seq.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Seq.js | Apache-2.0 |
flatten() {
let seqSeq = this._iterable;
return Seq.fromGenerator(function*() {
for (let seq of seqSeq) {
yield* seq;
}
});
} | Flattens this sequence of iterables into a concatenated sequence.
@returns {!Seq.<C>}
@template C | flatten | javascript | Strilanc/Quirk | src/base/Seq.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Seq.js | Apache-2.0 |
concat(other) {
let seq = this._iterable;
return Seq.fromGenerator(function*() {
yield* seq;
yield* other;
});
} | Returns a sequence that iterates the receiving sequence's items and then the given iterable's items.
@param {*|!(A[])|!Seq.<A>} other
@returns {!Seq.<T|A>}
@template T, A | concat | javascript | Strilanc/Quirk | src/base/Seq.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Seq.js | Apache-2.0 |
withOverlayedItem(index, overlayedItem) {
if (index < 0) {
throw new Error("needed index >= 0");
}
let self = this;
return Seq.fromGenerator(function*() {
if (self.tryPeekCount() !== undefined && index >= self.tryPeekCount()) {
throw new Error("nee... | Returns a sequence with the same items, except the item at the given index is replaced.
If the index is not reached during iteration, an exception is thrown.
@param {!int} index
@param {A} overlayedItem
@returns {!Seq.<T|A>}
@template T, A | withOverlayedItem | javascript | Strilanc/Quirk | src/base/Seq.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Seq.js | Apache-2.0 |
withTransformedItem(index, itemTransformation) {
if (index < 0) {
throw new Error("needed index >= 0");
}
let self = this;
return Seq.fromGenerator(function*() {
if (self.tryPeekCount() !== undefined && index >= self.tryPeekCount()) {
throw new Err... | Returns a sequence with the same items, except the item at the given index is transformed by the given function.
If the index is not reached during iteration, an exception is thrown.
@param {!int} index
@param {!function(T) : A} itemTransformation
@returns {!Seq.<T|A>}
@template T, A | withTransformedItem | javascript | Strilanc/Quirk | src/base/Seq.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Seq.js | Apache-2.0 |
withInsertedItem(index, item) {
if (index < 0) {
throw new Error("needed index >= 0");
}
let self = this;
return Seq.fromGenerator(function*() {
if (self.tryPeekCount() !== undefined && index > self.tryPeekCount()) {
throw new Error("needed index <... | Returns a sequence with the same items, except the given extra item is yielded when the given index is reached.
If the insertion index is the length of the sequence, the inserted item is the last yielded item.
If the insertion index is past the length of the sequence, an error is thrown (but only at the end of iteratio... | withInsertedItem | javascript | Strilanc/Quirk | src/base/Seq.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Seq.js | Apache-2.0 |
takeWhile(predicate) {
let seq = this._iterable;
return Seq.fromGenerator(function*() {
for (let e of seq) {
if (!predicate(e)) {
break;
}
yield e;
}
});
} | Returns a sequence with the same items, until one of the items fails to match the given predicate. Then the
sequence is cut short just before yielding that item.
@param {!function(T) : !boolean} predicate
@returns {!Seq.<T>}
@template T | takeWhile | javascript | Strilanc/Quirk | src/base/Seq.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Seq.js | Apache-2.0 |
skipTailWhile(predicate) {
let seq = this._iterable;
return Seq.fromGenerator(function*() {
let tail = [];
for (let e of seq) {
if (predicate(e)) {
tail.push(e);
} else {
yield* tail;
tail... | Returns a sequence with the same items, except items are dropped from the end of the sequence until the last item
doesn't satisfy the given predicate.
@param {!function(T) : !boolean} predicate
@returns {!Seq.<T>}
@template T | skipTailWhile | javascript | Strilanc/Quirk | src/base/Seq.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Seq.js | Apache-2.0 |
skipWhile(predicate) {
let seq = this._iterable;
return Seq.fromGenerator(function*() {
let matched = true;
for (let e of seq) {
matched = matched && predicate(e);
if (!matched) {
yield e;
}
}
... | Returns a sequence with the same items, except items at the start of the sequence are skipped until an item
doesn't satisfy the given predicate.
@param {!function(T) : !boolean} predicate
@returns {!Seq.<T>}
@template T | skipWhile | javascript | Strilanc/Quirk | src/base/Seq.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Seq.js | Apache-2.0 |
take(maxTakeCount) {
if (maxTakeCount < 0) {
throw new Error("needed maxTakeCount >= 0");
}
if (maxTakeCount === 0) {
return new Seq([]);
}
let seq = this._iterable;
return Seq.fromGenerator(function*() {
let i = 0;
for (let... | Returns a sequence with the same items, except cut short if it exceeds the given maximum count.
@param {!int} maxTakeCount
@returns {!Seq.<T>}
@template T | take | javascript | Strilanc/Quirk | src/base/Seq.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Seq.js | Apache-2.0 |
skip(maxSkipCount) {
if (maxSkipCount < 0) {
throw new Error("needed maxSkipCount >= 0");
}
if (maxSkipCount === 0) {
return this;
}
let seq = this._iterable;
return Seq.fromGenerator(function*() {
let i = 0;
for (let e of ... | Returns a sequence with the same items, except the give number are skipped at the start.
@param {!int} maxSkipCount
@returns {!Seq.<T>}
@template T | skip | javascript | Strilanc/Quirk | src/base/Seq.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Seq.js | Apache-2.0 |
distinctBy(keySelector) {
let seq = this;
return Seq.fromGenerator(function() {
let keySet = new Set();
return seq.filter(e => {
let key = keySelector(e);
if (keySet.has(key)) {
return false;
}
ke... | Returns a sequence with the same items, except later items with the same key as earlier items get skipped.
@param {!function(T) : K} keySelector Items are considered distinct when their image, through this function, is
not already in the Set of seen images. The return type must support being inserted into a Set.
@retu... | distinctBy | javascript | Strilanc/Quirk | src/base/Seq.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Seq.js | Apache-2.0 |
segmentBy(keySelector) {
let seq = this;
return Seq.fromGenerator(function*() {
let group = [];
let lastKey = undefined;
for (let item of seq) {
let itemKey = keySelector(item);
if (group.length > 0 && itemKey !== lastKey) {
... | @param {!function(T):*} keySelector
@returns {!Seq.<T>}
@template T | segmentBy | javascript | Strilanc/Quirk | src/base/Seq.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Seq.js | Apache-2.0 |
single(emptyManyErrorAlternative = THROW_IF_EMPTY) {
let iter = this[Symbol.iterator]();
let first = iter.next();
if (!first.done && iter.next().done) {
return first.value;
}
if (emptyManyErrorAlternative === THROW_IF_EMPTY) {
if (first.done) {
... | Returns the single item in the sequence. If there are no items or multiple items in the sequence, either an error
is thrown or an alternative value is returned.
@param {=A} emptyManyErrorAlternative The value to return if the sequence is empty. If not provided, an error
is thrown when the sequence is empty or has more... | single | javascript | Strilanc/Quirk | src/base/Seq.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Seq.js | Apache-2.0 |
first(emptyErrorAlternative = THROW_IF_EMPTY) {
let iter = this[Symbol.iterator]();
let first = iter.next();
if (!first.done) {
return first.value;
}
if (emptyErrorAlternative === THROW_IF_EMPTY) {
throw new Error("Empty sequence has no first item.")
... | Returns the first item in the sequence.
@param {=A} emptyErrorAlternative The value to return if the sequence is empty. If not provided, an error
is thrown when the sequence is empty.
@returns {T|A}
@template T, A | first | javascript | Strilanc/Quirk | src/base/Seq.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Seq.js | Apache-2.0 |
last(emptyErrorAlternative = THROW_IF_EMPTY) {
//noinspection JSUnusedAssignment
let result = EMPTY_SYGIL;
for (let e of this._iterable) {
result = e;
}
return emptyFallback(result, emptyErrorAlternative, "Empty sequence has no last item.");
} | Returns the last item in the sequence.
@param {=A} emptyErrorAlternative The value to return if the sequence is empty. If not provided, an error
is thrown when the sequence is empty.
@returns {T|A}
@template T, A | last | javascript | Strilanc/Quirk | src/base/Seq.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Seq.js | Apache-2.0 |
tryPeekCount() {
if (Array.isArray(this._iterable) || !GENERIC_ARRAY_TYPES.every(t => !(this._iterable instanceof t))) {
return this._iterable.length;
}
if (this._iterable instanceof Map || this._iterable instanceof Set) {
return this._iterable.size;
}
ret... | If the sequence is of a known type with a known number of items, then returns the length of the sequence.
Otherwise, returns undefined.
It is guaranteed that the sequence will not be iterated by this method.
@returns {!int|undefined} | tryPeekCount | javascript | Strilanc/Quirk | src/base/Seq.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Seq.js | Apache-2.0 |
count() {
let known = this.tryPeekCount();
if (known !== undefined) {
return known;
}
let n = 0;
//noinspection JSUnusedLocalSymbols
for (let _ of this._iterable) {
n++;
}
return n;
} | Determines the number of items in the sequence.
Uses length/size methods of known types, when possible, but otherwise falls back to iterating all the items.
Gets stuck in a loop if the sequence is unbounded.
@returns {!int} | count | javascript | Strilanc/Quirk | src/base/Seq.js | https://github.com/Strilanc/Quirk/blob/master/src/base/Seq.js | Apache-2.0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.