Sha256: 58e01713ac9bdd6ea3f6027d75ca4e8eb8700994a6404bb066a02449217305ac

Contents?: true

Size: 929 Bytes

Versions: 1

Compression:

Stored size: 929 Bytes

Contents

# frozen_string_literal: true

class Array
  # Creates a new array containing the values returned by the block.
  #
  # This implementation is considered immutable for two reasons:
  # 1. It does not modify the original array's size or structure.
  # 2. It creates and returns a new array with the mapped values.
  #
  # The original array's length is calculated once at the beginning
  # and remains constant throughout the iteration.
  #
  # If no block is given, returns an Enumerator object.
  #
  # @yield [Object] Passes each element of the array to the block.
  # @return [Array] A new array with the results of running the block once for every element.
  # @return [Enumerator] If no block is given.
  def map
    return to_enum(:map) { self.length } unless block_given?

    i = 0
    result = []
    length = self.length
    while i < length
      result << yield(self[i])
      i = i.succ
    end
    result
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
native_ruby-0.1.0 lib/native_ruby/iterators/immutable/array/map.rb