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

Proposal: Array.prototype.feed #42

Closed
Youmoo opened this Issue May 30, 2015 · 6 comments

Comments

Projects
None yet
3 participants
@Youmoo

Youmoo commented May 30, 2015

Array has some methods which supports functional coding ,such as map,reduce..
Say I wanna do some transforming with an array and log the result :

var result=[1,2,3].map(function(v){
    return v+1;
});
console.log(result);

It'll be better if we can log the result without declaring variable result
First I think about this:

[1, 2, 3].map(function(v) {
    return v + 1;
}).some(function(v, i, a) {
    console.log(a);
    return true;
});

But this is aginst the purpose of some.
I need something like this:

Array.prototype.feed = function(callback) {
    callback(this);
};
[1, 2, 3].map(function(v) {
    return v + 1;
}).feed(function(a) {
    console.log(a);
});

In practice, we often need map / filter / reduce arrays and deal with the result. So it'will be a pleasure if array has a feed method (more than a pleasure if Object.prototype also has that:) to support more functional programming

Sorry for my poor English, Hope I can be understood:)

@ghost

This comment has been minimized.

Show comment
Hide comment
@ghost

ghost May 30, 2015

Very cryptically written. As I understand you want something like in: in Pharo Smalltalk. AFAICT this construct is not Array-specific, it is useful in any object, if it would be added. What complicates it, is if result is null or undefined (which is not a problem in "everything is an object" Smalltalk). BTW what's wrong with plain

console.log([1,2,3].map(function(v){
    return v+1;
}));

except it is a bit awkward? In ES6, a more awkward

((_=[1,2,3].map(function(v){
    return v+1;
})) => console.log(_))()

is probably possible as well.

ghost commented May 30, 2015

Very cryptically written. As I understand you want something like in: in Pharo Smalltalk. AFAICT this construct is not Array-specific, it is useful in any object, if it would be added. What complicates it, is if result is null or undefined (which is not a problem in "everything is an object" Smalltalk). BTW what's wrong with plain

console.log([1,2,3].map(function(v){
    return v+1;
}));

except it is a bit awkward? In ES6, a more awkward

((_=[1,2,3].map(function(v){
    return v+1;
})) => console.log(_))()

is probably possible as well.

@Youmoo

This comment has been minimized.

Show comment
Hide comment
@Youmoo

Youmoo May 31, 2015

@Herby Thank you , your description is more clear 👍
Let everything in js have a feed method is all I want .

Youmoo commented May 31, 2015

@Herby Thank you , your description is more clear 👍
Let everything in js have a feed method is all I want .

@michaelficarra

This comment has been minimized.

Show comment
Hide comment
@michaelficarra

michaelficarra May 31, 2015

Member
// with the proposed Object.prototype.feed
[1,2,3].map(function(v){ return v + 1; }).feed(console.log.bind(console));
// or:
[1,2,3].map(_ => _ + 1).feed(_ => console.log(_));

// without it:
console.log([1,2,3].map(function(v){ return v + 1; }));
// or:
console.log([1,2,3].map(_ => _ + 1));

I can't see any justification for this.

Member

michaelficarra commented May 31, 2015

// with the proposed Object.prototype.feed
[1,2,3].map(function(v){ return v + 1; }).feed(console.log.bind(console));
// or:
[1,2,3].map(_ => _ + 1).feed(_ => console.log(_));

// without it:
console.log([1,2,3].map(function(v){ return v + 1; }));
// or:
console.log([1,2,3].map(_ => _ + 1));

I can't see any justification for this.

@Youmoo

This comment has been minimized.

Show comment
Hide comment
@Youmoo

Youmoo May 31, 2015

@michaelficarra It will make a more fluent and chain-able (like jQuery method chaining) programming experience.

Youmoo commented May 31, 2015

@michaelficarra It will make a more fluent and chain-able (like jQuery method chaining) programming experience.

@michaelficarra

This comment has been minimized.

Show comment
Hide comment
@michaelficarra

michaelficarra May 31, 2015

Member

@Youmoo Libraries like underscore do this without extending Object.prototype:

_([1,2,3]).map(_ => _ + 1).feed(_ => console.log(_)).done();
Member

michaelficarra commented May 31, 2015

@Youmoo Libraries like underscore do this without extending Object.prototype:

_([1,2,3]).map(_ => _ + 1).feed(_ => console.log(_)).done();
@bterlson

This comment has been minimized.

Show comment
Hide comment
@bterlson

bterlson Sep 22, 2015

Member

Closing this now. Proposals should have an identified champion and repository set up where work happens. This is not the place to brainstorm about proposals.

Member

bterlson commented Sep 22, 2015

Closing this now. Proposals should have an identified champion and repository set up where work happens. This is not the place to brainstorm about proposals.

@bterlson bterlson closed this Sep 22, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment