def _map_slice(slice_, mapper):
  """Applies `mapper` to a contiguous slice `slice_`."""
  assert slice_.step is None or slice_.step == 1
  new_slice = slice(mapper(slice_.start), 1 + mapper(slice_.stop -1))
  assert slice_.stop - slice_.start == new_slice.stop - new_slice.start
  return new_slice