Sha256: a8cfdc1f3713eb7ede6c8d38c66bc756988f119348a22154d85fd6f5cf851dae

Contents?: true

Size: 961 Bytes

Versions: 7

Compression:

Stored size: 961 Bytes

Contents

# frozen_string_literal: true

require "refinements/shared/enumerables/many"

module Refinements
  # Provides additional enhancements to the Array primitive.
  module Arrays
    refine Array do
      import_methods Shared::Enumerables::Many

      def compress = dup.compress!

      def compress!
        compact!
        delete_if { |element| element.respond_to?(:empty?) && element.empty? }
      end

      def excluding(*elements) = self - elements.flatten

      def filter_find(&block) = block ? lazy.map(&block).find(&:itself) : lazy

      def including(*elements) = self + elements.flatten

      def intersperse(*elements) = product([elements]).tap(&:pop).flatten.push(last)

      def maximum(key) = map(&key).max

      def mean = size.zero? ? 0 : sum(0.0) / size

      def minimum(key) = map(&key).min

      def pad(value, max: size) = dup.fill(value, size..(max - 1))

      def ring(&) = [last, *self, first].each_cons(3, &)
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
refinements-9.4.0 lib/refinements/arrays.rb
refinements-9.3.3 lib/refinements/arrays.rb
refinements-9.3.2 lib/refinements/arrays.rb
refinements-9.3.1 lib/refinements/arrays.rb
refinements-9.3.0 lib/refinements/arrays.rb
refinements-9.2.0 lib/refinements/arrays.rb
refinements-9.1.0 lib/refinements/arrays.rb