Sha256: a254f2953702fad9bd0daaaa2cff023e0035c5fc719452699263eed4385ce19b

Contents?: true

Size: 1.16 KB

Versions: 1

Compression:

Stored size: 1.16 KB

Contents

require 'wordlist/operators/unary_operator'
require 'wordlist/unique_filter'

module Wordlist
  module Operators
    #
    # Lazily enumerates over only the unique words in the wordlist, filtering
    # out duplicates.
    #
    # @since 1.0.0
    #
    class Unique < UnaryOperator

      #
      # Enumerates over the unique words in the wordlist.
      #
      # @yield [word]
      #   The given block will be passed each unique word from the wordlist.
      #
      # @yieldparam [String] word
      #   A unique word from the wordlist.
      #
      # @return [Enumerator]
      #   If no block is given, an Enumerator object will be returned.
      #
      # @example
      #   wordlist= Wordlist::List["foo", "bar", "baz", "qux"]
      #   (wordlist + wordlist).uniq.each do |word|
      #     puts word
      #   end
      #   # foo
      #   # bar
      #   # baz
      #   # qux
      #
      # @api public
      #
      def each
        return enum_for(__method__) unless block_given?

        unique_filter = UniqueFilter.new

        @wordlist.each do |word|
          if unique_filter.add?(word)
            yield word
          end
        end
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
wordlist-1.0.0 lib/wordlist/operators/unique.rb