question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Official spec for transformer protocol

See original GitHub issue

@swannodette if you recall, this was something I had asked about and while I don’t recall the exact reason for not having a transformer protocol implemented in transducers-js, I do remember there was a reason.

I’ve been thinking about it a bit lately, and I do think it’s worthwhile to be able to define, particularly for JavaScript where it’s much simpler to bake this into the prototype rather than defining as a lookup map of handlers as in transit-js.

As more libraries in JavaScript begin implementing this protocol (this was prompted by @kevinbeaty’s excellent transducer PR on the ramda.js project), I wanted to see if there could be an agreed upon spec for the transformer before things get too far along.

This is the implementation kicked off by @jlongster

var t = require('./transducers');
Immutable.Vector.prototype[t.protocols.transformer] = {
  init: function() {
    return Immutable.Vector().asMutable();
  },
  result: function(vec) {
    return vec.asImmutable();
  },
  step: function(vec, x) {
    return vec.push(x);
  }
};

For starters - the use of a Symbol('transformer') is problematic as Symbol() creates a unique value (Symbol('transformer') !== Symbol('transformer')), and so you lose any interop when defined independently in multiple libraries.

I’d propose all transformer protocols be implemented as a @@transformer string (similar to @@iterator) until if/when the transformer is officially recognized in the well known symbols list.

I’d also propose that the spec behave similarly to an iterator, in that it is a function which returns the transformer, rather than as just an object proposed above:

Immutable.Vector.prototype['@@transformer'] = function() {
  return {
    init: function() {
      return new Immutable.Vector().asMutable();
    },
    result: function(vec) {
      return vec.asImmutable();
    },
    step: function(vec, x) {
      return vec.push(x);
    }
  };
}

This makes the implementation more useful, as it can refer to the current object in the init to know what value makes to init, should the object be subclassed:

SomeObject.prototype['@@transformer'] = function() {
  var obj = this;
  return {
    init: function() {
      return new obj.constructor()
    },
    step: function(result, arr) {
      return result.set(arr[0], arr[1]);
    },
    result: function(obj) {
      return obj;
    }    
  };
}

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:38 (14 by maintainers)

github_iconTop GitHub Comments

1reaction
swannodettecommented, Mar 19, 2015

@tgriesser @kevinbeaty

Seems to me the logic should be more like the following:

var TRANSFORMER = null;

if(typeof Symbol != "undefined") {
    TRANSFORMER = Symbol.for("cognitect/transformer");
} else {
    TRANSFORMER = "@@cognitect/transformer";
}

Globally stealing names seems like a really bad idea.

0reactions
Gozalacommented, Apr 3, 2015

https://github.com/gozala/transducers also uses this interface 😉

Read more comments on GitHub >

github_iconTop Results From Across the Web

Complete Guide to Power Transformer Standards
This paper introduces the relevant current standards of power and distribution transformers, industry standards and IEC standards, etc.,
Read more >
Specifications - BidNet
TECHNICAL SPECIFICATIONS. 28MVA, 115/12.47 kV Substation Power Transformer. TABLE OF CONTENTS. 1.0. GENERAL REQUIREMENTS .
Read more >
Energy Conservation Program: Test Procedure for Distribution ...
Specification for Stabilization of Current and Voltage ... The current DOE test procedure for distribution transformers appear at title 10 ...
Read more >
Guidelines for installing transformers, part 1. - EC&M
The successful operation of a transformer is dependent on proper installation as well as on good design and manufacture.
Read more >
Single Phase Padmounted Transformer Specifications for ...
SINGLE PHASE PADMOUNTED TRANSFORMER SPECIFICATIONS. 2. Section I: Introduction. 1. This specification covers the material and design requirements of PSEG ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found