#--
# Credit goes to Florian Gross.
#++
require 'facets/core/enumerable/filter_collect'

module Enumerable

  # Collects/Maps and compacts items in one single step.
  # The items for which the supplied block returns +nil+ will not
  # end up in the resulting array.
  #
  #   # Return telephone numbers of all persons that have a telephone number.
  #   persons.compact_collect { |person| person.telephone_no }
  #
  # Also see Enumerable#collect, Enumerable#map, Array#compact.
  #
  def compact_collect #:yield:
    filter_collect do |item|
      new_item = yield(item)
      throw(:skip) if new_item.nil?
      new_item
    end
  end

  alias_method :compact_map, :compact_collect

end



#  _____         _
# |_   _|__  ___| |_
#   | |/ _ \/ __| __|
#   | |  __/\__ \ |_
#   |_|\___||___/\__|
#
=begin test

  require 'test/unit'

  class TCEnumerable < Test::Unit::TestCase

    def test_compact_collect
      a = [1,2,nil,4].compact_collect { |e| e }
      assert_equal( [1,2,4], a )
    end

  end

=end
