The Map operator applies a function of your choosing to each item emitted by the source Observable, and returns an Observable that emits the results of these function applications.
map
keep keep-indexed map map-indexed
TBD
byLine cast encode map
RxGroovy implements this operator as map. For example, the following code maps a function that squares the incoming value onto the values in numbers:
numbers
numbers = Observable.from([1, 2, 3, 4, 5]); numbers.map({it * it}).subscribe( { println(it); }, // onNext { println("Error: " + it.getMessage()); }, // onError { println("Sequence complete"); } // onCompleted );
1 4 9 16 25 Sequence complete
This operator does not by default operate on any particular Scheduler.
map(Func1)
The cast operator is a specialized version of Map that transforms each item from the source Observable by casting it into a particular Class before reemitting it.
cast
cast(Class)
In the StringObservable class that is not part of the RxGroovy core there is also a specialty mapping operator, encode, that transforms an Observable that emits strings into an Observable that emits byte arrays that respect character boundaries of multibyte characters in the original strings.
StringObservable
encode
Also in the StringObservable class that is not part of the RxGroovy core there is a specialty mapping operator called byLine, that transforms an Observable that emits strings into an Observable that emits lines of text, by buffering the strings from the source Observable until a line-feed is found in one of them.
byLine
RxJava implements this operator as map.
In the StringObservable class that is not part of the RxJava core there is also a specialty mapping operator, encode, that transforms an Observable that emits strings into an Observable that emits byte arrays that respect character boundaries of multibyte characters in the original strings.
Also in the StringObservable class that is not part of the RxJava core there is a specialty mapping operator called byLine, that transforms an Observable that emits strings into an Observable that emits lines of text, by buffering the strings from the source Observable until a line-feed is found in one of them.
cast map
map pluck select
RxJS implements this operator as map or select (the two are synonymous). In addition to the transforming function, you may pass this operator an optional second parameter that will become the “this” context in which the transforming function will execute.
select
this
The transforming function gets three parameters:
// Using a value var md = Rx.Observable.fromEvent(document, 'mousedown').map(true); var mu = Rx.Observable.fromEvent(document, 'mouseup').map(false); // Using a function var source = Rx.Observable.range(1, 3) .select(function (x, idx, obs) { return x * x; }); var subscription = source.subscribe( function (x) { console.log('Next: ' + x); }, function (err) { console.log('Error: ' + err); }, function () { console.log('Completed'); });
Next: 1 Next: 4 Next: 9 Completed
There is also an operator called pluck which is a simpler version of this operator. It transforms the elements emitted by the source Observable by extracting a single named property from those elements and emitting that property in their place.
pluck
var source = Rx.Observable .fromArray([ { value: 0 }, { value: 1 }, { value: 2 } ]) .pluck('value'); var subscription = source.subscribe( function (x) { console.log('Next: ' + x); }, function (err) { console.log('Error: ' + err); }, function () { console.log('Completed'); });
Next: 0 Next: 1 Next: 2 Completed
map/select and pluck are found in each of the following distributions:
rx.js
rx.all.js
rx.all.compat.js
rx.compat.js
rx.lite.js
rx.lite.compat.js
Rx.helpers.pluck(property)
Cast Select
map mapWithIndex mapTo select pluck
RxPHP implements this operator as map.
Takes a transforming function that operates on each element.
//from https://github.com/ReactiveX/RxPHP/blob/master/demo/map/map.php $observable = \Rx\Observable::fromArray([21, 42]); $observable ->map(function ($elem) { return $elem * 2; }) ->subscribe($stdoutObserver);
Next value: 42 Next value: 84 Complete!
RxPHP also has an operator mapWithIndex.
mapWithIndex
Maps operator variant that calls the map selector with the index and value
//from https://github.com/ReactiveX/RxPHP/blob/master/demo/map/mapWithIndex.php $subscriptions = Rx\Observable::fromArray([21, 42]) ->mapWithIndex(function ($index, $elem) { return $index + $elem; }) ->subscribe($stdoutObserver);
Next value: 21 Next value: 43 Complete!
RxPHP also has an operator mapTo.
mapTo
Maps every value to the same value every time
//from https://github.com/ReactiveX/RxPHP/blob/master/demo/map/mapTo.php $subscription = Rx\Observable::fromArray([21, 42]) ->mapTo(1) ->subscribe($stdoutObserver);
Next value: 1 Next value: 1 Complete!
RxPHP also has an operator select.
Alias for Map
RxPHP also has an operator pluck.
Returns an Observable containing the value of a specified array index (if array) or property (if object) from all elements in the Observable sequence. If a property can't be resolved the observable will error.
//from https://github.com/ReactiveX/RxPHP/blob/master/demo/pluck/pluck.php $source = Rx\Observable::fromArray([ (object)['value' => 0], (object)['value' => 1], (object)['value' => 2] ]) ->pluck('value'); $subscription = $source->subscribe($stdoutObserver);
Next value: 0 Next value: 1 Next value: 2 Complete!
map map_with_index
map mapWithIndex