cognitect-labs / transducers-js

Transducers for JavaScript
Apache License 2.0
1.6k stars 44 forks source link

Creating transformers ... "@@"?? #44

Open shaunc opened 7 years ago

shaunc commented 7 years ago

Does the use of "@@" in the protocol mean that in es6 code I should actually do something like the following to define a transformer?

const init = Symbol.for('transducer/init');
const result = Symbol.for('transducer/result');
const step = Symbol.for('transducer/step');
function MapTransformer() {
  return {
    [init]() { return xf[init](); }
    [result](res) { return xf[result](res); }
    [step](res, input) { return xf[step](res, f(input)); }
  };
}

I'm confused what the "@@" are supposed to represent as it doesn't look like you have es6 support -- I don't suppose the above is actually interoperable with the library. But isn't "@@" for "well-known symbols" in es6?

seanpoulter commented 6 years ago

isn't "@@" for "well-known symbols" in es6?

Yep. Have a look at the Symbols chapter of You Don't Know JS: ES 6 & Beyond. Here's a note from the Built-in Symbols section:

The specification uses the @@ prefix notation to refer to the built-in symbols, the most common ones being: @@iterator, @@toStringTag, @@toPrimitive.


That leads to the question, are a String and a Symbol equivalent? You can use the following block to spot check if they're the same. It looks to me like you'll need a PR to support Symbols before your code with Symbols.

Array.prototype['@@custom'] = 'from String';
console.log([]['@@custom']);
// > 'value'

const sym = Symbol.for('custom');
console.log([][sym]);
// > undefined

Array.prototype[sym] = 'from Symbol'
console.log([][sym]);
// > 'from Symbol'