Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Setting Value Later #61

Open
brigand opened this issue Dec 31, 2013 · 2 comments
Open

Allow Setting Value Later #61

brigand opened this issue Dec 31, 2013 · 2 comments

Comments

@brigand
Copy link

brigand commented Dec 31, 2013

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
Copy link
Owner

dtao commented Jan 1, 2014

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
Copy link
Author

brigand commented Jan 1, 2014

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 [email protected] 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//issues/61#issuecomment-31416113
.

dtao added a commit that referenced this issue Jan 2, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants