Sha256: 357df04e2da68c7aacb14d0a654faac17c2a2928c18ef81c74e70fb98bef21d6
Contents?: true
Size: 1.45 KB
Versions: 185
Compression:
Stored size: 1.45 KB
Contents
class List { constructor(arr) { this.values = arr || []; } append(otherList) { for (const el of otherList.values) { this.values.push(el); } return this; } concat(otherList) { return this.append(otherList); } filter(operation) { const filteredValues = []; for (const el of this.values) { if (operation(el)) { filteredValues.push(el); } } this.values = filteredValues; return this; } length() { let length = 0; for (const el of this.values) { length++; } return length; } map(operation) { const mappedValues = []; for (const el of this.values) { mappedValues.push(operation(el)); } this.values = mappedValues; return this; } foldl(operation, initialValue) { let acc = initialValue; for (const el of this.values) { acc = operation(acc, el); } return acc; } foldr(operation, initialValue) { let acc = initialValue; let index = this.length() - 1; while (index >= 0) { const el = this.values[index--]; acc = operation(acc, el); } return acc; } reverse() { const numElements = this.length(); let finalIndex = numElements - 1; for (let index = 0; index < numElements / 2; index++) { const temp = this.values[index]; this.values[index] = this.values[finalIndex]; this.values[finalIndex--] = temp; } return this; } } export default List;
Version data entries
185 entries across 185 versions & 1 rubygems