from typing import Callable, Any
from rx.core import AnonymousObservable, Observable
from rx.core.typing import Mapper, MapperIndexed, Observer, Disposable, Scheduler

# pylint: disable=redefined-builtin
def _map(mapper: Mapper = None) -> Callable[[Observable], Observable]:
    def map(source: Observable) -> Observable:
        """Partially applied map operator.

        Project each element of an observable sequence into a new form
        by incorporating the element's index.

        Example:
            >>> map(source)

        Args:
            source: The observable source to transform.

        Returns:
            Returns an observable sequence whose elements are the
            result of invoking the transform function on each element
            of the source.
        """

        def identity(value): return value

        mapper_ = mapper or identity

        def subscribe(obv: Observer, scheduler: Scheduler) -> Disposable:
            def on_next(value: Any) -> None:
                try:
                    result = mapper_(value)
                except Exception as err:  # pylint: disable=broad-except
                    obv.on_error(err)
                else:
                    obv.on_next(result)

            return source.subscribe_(on_next, obv.on_error, obv.on_completed, scheduler)
        return AnonymousObservable(subscribe)
    return map


def _map_indexed(mapper_indexed: MapperIndexed = None) -> Callable[[Observable], Observable]:
    def map_indexed(source: Observable) -> Observable:
        """Partially applied indexed map operator.

        Project each element of an observable sequence into a new form
        by incorporating the element's index.

        Example:
            >>> ret = map_indexed(source)

        Args:
            source: The observable source to transform.

        Returns:
            Returns an observable sequence whose elements are the
            result of invoking the transform function on each element
            of the source.
        """

        def identity(value, index): return value

        mapper_indexed_ = mapper_indexed or identity

        def subscribe(obv: Observer, scheduler: Scheduler) -> Disposable:
            count = 0

            def on_next(value: Any) -> None:
                nonlocal count

                try:
                    result = mapper_indexed_(value, count)
                except Exception as err:  # pylint: disable=broad-except
                    obv.on_error(err)
                else:
                    count += 1
                    obv.on_next(result)

            return source.subscribe_(on_next, obv.on_error, obv.on_completed, scheduler)
        return AnonymousObservable(subscribe)
    return map_indexed

@@ -25,16 +25,56 @@
 ble, Any
+%0A%0Afrom rx.internal.basic import identity
 %0Afrom rx
@@ -201,16 +201,17 @@
 eduler%0A%0A
+%0A
 # pylint
@@ -301,32 +301,67 @@
 %5D, Observable%5D:%0A
+%0A    _mapper = mapper or identity%0A%0A
     def map(sour
@@ -867,89 +867,8 @@
 %22%22%0A%0A
-        def identity(value): return value%0A%0A        mapper_ = mapper or identity%0A%0A
     
@@ -912,32 +912,39 @@
 duler: Scheduler
+ = None
 ) -%3E Disposable:
@@ -1039,23 +1039,23 @@
 esult = 
+_
 mapper
-_
 (value)%0A
@@ -1474,16 +1474,139 @@
 vable%5D:%0A
+    def _identity(value: Any, index: int) -%3E Any:%0A        return value%0A%0A    _mapper_indexed = mapper_indexed or _identity%0A%0A
     def 
@@ -2150,112 +2150,8 @@
 %22%22%0A%0A
-        def identity(value, index): return value%0A%0A        mapper_indexed_ = mapper_indexed or identity%0A%0A
     
@@ -2203,16 +2203,23 @@
 cheduler
+ = None
 ) -%3E Dis
@@ -2377,16 +2377,17 @@
 esult = 
+_
 mapper_i
@@ -2392,17 +2392,16 @@
 _indexed
-_
 (value, 

Move mapper selection out of inner function