Skip to content

Instantly share code, notes, and snippets.

@mattpodwysocki
Last active December 15, 2015 03:09
Show Gist options
  • Save mattpodwysocki/5192213 to your computer and use it in GitHub Desktop.
Save mattpodwysocki/5192213 to your computer and use it in GitHub Desktop.
Changing select/map to use a scoped this argument in addition to passing the observable sequence.

Changing select/map to use a scoped this argument in addition to passing the observable sequence.

Changes from:

  selector: (T => Int => U)

to:

  selector: (T => Int => Observable<T> => U)

With optional scope binding.

This also affects

  • every
  • some
  • filter
/**
* Projects each element of an observable sequence into a new form by incorporating the element's index.
*
* 1 - source.select(function (value) { return value * value; });
* 2 - source.select(function (value, index) { return value * value + index; });
*
* @memberOf Observable#
* @param {Function} selector A transform function to apply to each source element; the second parameter of the function represents the index of the source element.
* @returns {Observable} An observable sequence whose elements are the result of invoking the transform function on each element of source.
*/
observableProto.map = function (selector) {
var parent = this, thisArg = arguments[1];
return new AnonymousObservable(function (observer) {
var count = 0;
return parent.subscribe(function (value) {
var result;
try {
result = selector.call(thisarg, value, count++, parent);
} catch (exception) {
observer.onError(exception);
return;
}
observer.onNext(result);
}, observer.onError.bind(observer), observer.onCompleted.bind(observer));
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment