|
|
|
|
|
by adgasf
2333 days ago
|
|
I find JavaScript unusable without this operator. Fortunately Babel supports it well. It makes a great alternative to fiddling with prototypes or wrapper objects when you want to extend something. For example, this is flat-map implemented as a free function: const flatMap = f => {
if (!f) {
throw new TypeError('f must be a function');
}
return xs => ({
[Symbol.iterator]: function * () {
for (const x of xs) {
yield * f(x);
}
}
});
};
// Usage
const xs = [ 1, 2, 3 ] |> flatMap(x => [ x, -x ]);
|
|