dtao / lazy.js

Like Underscore, but lazier
http://danieltao.com/lazy.js/
MIT License
6.01k stars 268 forks source link

Allow Setting Value Later #61

Open brigand opened 10 years ago

brigand commented 10 years ago

It'd be great to be able to create pure functional behaviors, and then later apply them to values. A jsfiddle of the below.

Something along the lines of this:

var makePeople = Lazy([]).map(function(s){
    var parts = s.split(" ");
    return {
        first: parts[0],
        last: parts[1],
        full: s
    };
});

var data = ["John Doe", "Ann Robljsakld"];
makePeople.of(data);

The result would be:

[
  {
    "first": "John",
    "last": "Doe",
    "full": "John Doe"
  },
  {
    "first": "Ann",
    "last": "Robljsakld",
    "full": "Ann Robljsakld"
  }
] 

Suggested implementation:

Lazy.Sequence.prototype.of = function(source) {
    var previousSource = this.source, returnValue;
    this.source = source;

    if (this.startsWith) {
        returnValue = this.toString();
    }
    else if (this.pairs) {
        returnValue = this.toObject();
    }
    else {
        returnValue = this.toArray();    
    }

    this.source = previousSource;
    return returnValue;
};
dtao commented 10 years ago

What's interesting about this suggestion is that it's already sort of available. Consider this:

var names    = [],
    sequence = Lazy(names).map(function(name) {
      var parts = name.split(' ');
      return {
        first: parts[0],
        last: parts[1],
        full: name
      };
    });

names.push("John Doe");
names.push("Ann Robljsakld");

sequence.toArray(); // you'll see it contains the output you suggested

Which demonstrates that a Lazy.Sequence is not a "snapshot" but rather provides a view into an underlying data source.

I guess the missing piece would be an interface for specifying a sequence with no underlying source (i.e. without needing a reference to an empty array), or for "swapping out" one source for another. I'll think on this!

brigand commented 10 years ago

Cool :-)

I'm working on a project which needs this kind of functionality. It's a JSON transformation library, and users need a way to specify the transformations before data is available, and then apply it to multiple pieces of data.

On Tue, Dec 31, 2013 at 4:31 PM, Dan Tao notifications@github.com wrote:

What's interesting about this suggestion is that it's already sort of available. Consider this:

var names = [], sequence = Lazy(names).map(function(name) { var parts = name.split(' '); return { first: parts[0], last: parts[1], full: name }; }); names.push("John Doe");names.push("Ann Robljsakld"); sequence.toArray(); // you'll see it contains the output you suggested

Which demonstrates that a Lazy.Sequence is not a "snapshot" but rather provides a view into an underlying data source.

I guess the missing piece would be an interface for specifying a sequence with no underlying source (i.e. without needing a reference to an empty array), or for "swapping out" one source for another. I'll think on this!

— Reply to this email directly or view it on GitHubhttps://github.com/dtao/lazy.js/issues/61#issuecomment-31416113 .