TGViewer
JavaScript JavaScript @javascript · 31.1K subscribers
Post #3211 2.38K
CHALLENGE

class Pipeline {
#value;
#log = [];

constructor(value) {
this.#value = value;
}

map(fn) {
this.#value = fn(this.#value);
this.#log.push(`map:${this.#value}`);
return this;
}

filter(fn) {
if (Array.isArray(this.#value)) {
this.#value = this.#value.filter(fn);
this.#log.push(`filter:${this.#value}`);
}
return this;
}

reduce(fn, init) {
if (Array.isArray(this.#value)) {
this.#value = this.#value.reduce(fn, init);
this.#log.push(`reduce:${this.#value}`);
}
return this;
}

result() {
console.log(this.#log.join(' | '));
return this.#value;
}
}

new Pipeline([1, 2, 3, 4, 5, 6])
.filter(n => n % 2 === 0)
.map(arr => arr.map(n => n ** 2))
.reduce((acc, n) => acc + n, 0)
.result();
  • 👍 5
  • ❤ 4
  • 🔥 4
More from @javascript
  1. Sep 22, 2026Post #3511
  2. Sep 22, 2026CHALLENGE function Foo(x) { this.x = x; return { x: x * 2 }; } function Bar(x) { this.x =…
  3. Sep 21, 2026Post #3509
  4. Sep 21, 2026CHALLENGE console.log( typeof typeof 1, 1 + typeof 1, 2 ** 3 ** 2, 1 + 2 + '3', 1 < 2 < 3,…
  5. Sep 20, 2026Post #3507
  6. Sep 20, 2026CHALLENGE 'use strict'; function getValue() { return this.value; } const obj = { value: 42…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →