Working pipe operator today in pure JavaScript

https://news.ycombinator.com/rss Hits: 16
Summary

asPipes: working pipelines today in pure JavaScript 1 Summary asPipes is an experimental runtime abstraction that models the semantics of the proposed |> pipeline operator, implemented entirely in standard JavaScript (ES2020+). It demonstrates that pipeline-style composition can be expressed using the existing coercion semantics of the bitwise OR operator (|) and Symbol.toPrimitive. The implementation is small (<50 lines) and supports both synchronous and asynchronous evaluation with a familiar syntax: const greeting = pipe ( 'hello' ) | upper | ex ( '!!!' ) await greeting . run ( ) // → "HELLO!!!" ⸻ 2 Motivation The pipeline operator proposal (tc39/proposal-pipeline-operator) has been under discussion for several years, exploring multiple variants (F#, Smart, Hack, etc.). The asPipes experiment aims to: prototype F#-style semantics directly in today’s JavaScript; study ergonomics and readability in real-world code; show that deferred, referentially transparent composition can be achieved without syntax extensions; and inform the design conversation with practical, user-level feedback. ⸻ 3 Design Goals ✅ Composable — each transformation behaves like a unary function of the previous result. ✅ Deferred — no execution until .run() is called. ✅ Async-safe — promises and async functions are first-class citizens. ✅ Stateless — no global mutation; every pipeline owns its own context. ✅ Ergonomic — visually aligns with the future |> operator. ⸻ 4 Core API createAsPipes() Creates an isolated pipeline environment and returns: { pipe , // begin a pipeline asPipe // lift a function into a pipeable form } pipe(initialValue) Begins a new pipeline with initialValue. The returned object intercepts | operations via Symbol.toPrimitive. Call .run() to evaluate and retrieve the final result (async). asPipe(fn) Wraps a function fn so that it can be used in a pipeline: const upper = asPipe ( s => s . toUpperCase ( ) ) const ex = asPipe ( ( s , mark = '!' ) => s + mark ) Pipeable function...

First seen: 2025-10-08 09:13

Last seen: 2025-10-09 00:16