Sha256: 815a603d1e5443c30756ec4aac34e671643f75d0e38ded9c164cbafdb506c356

Contents?: true

Size: 866 Bytes

Versions: 3

Compression:

Stored size: 866 Bytes

Contents

# frozen_string_literal: true

module Refinements
  # Provides additional enhancements to the Array primitive.
  module Arrays
    refine Array do
      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

3 entries across 3 versions & 1 rubygems

Version Path
refinements-9.0.2 lib/refinements/arrays.rb
refinements-9.0.1 lib/refinements/arrays.rb
refinements-9.0.0 lib/refinements/arrays.rb