Skip to content

Instantly share code, notes, and snippets.

@jefflembeck
Created January 14, 2014 05:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jefflembeck/8413366 to your computer and use it in GitHub Desktop.
Save jefflembeck/8413366 to your computer and use it in GitHub Desktop.
I feel like this functionality should and likely already does exist. I just can't seem to get it in my head.
Array.prototype.superMap = function( fn ){
return this.map( function( x ){
return fn.call(x);
}
};
@jefflembeck
Copy link
Author

[ "hi", "how", "are", "you" ].superMap( String.prototype.toUppercase ) 

is the DREAM (for some value of dream)

@jefflembeck
Copy link
Author

[ "hi", "how", "are", "you" ].map( String.prototype.toUppercase.call );

TypeError: String.prototype.toUppercase is undefined

@raganwald
Copy link

[allong.es] has send that can be used to make something like this:

allong = require('allong.es');
var send = allong.es.send;

[ "hi", "how", "are", "you" ].map(send('toUpperCase'));
  //=>
    [ 'HI',
      'HOW',
      'ARE',
      'YOU' ]

Not exactly the same, but there are some other features you may want to consider if you implement superMap, such as being able to take some parameters:

var map = allong.es.map;

map([ "hi", "how", "are", "you" ], send('concat', ' (pause)'))
  //=>
    [ 'hi (pause)',
      'how (pause)',
      'are (pause)',
      'you (pause)' ]

@jefflembeck
Copy link
Author

That's fantastic. Thanks @raganwald. Picking up your book on this. :)

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