Sha256: 2d27b7faf898f9ea444d90e9efcd2bd06098fe492cd7aee822cd7cce53098d52

Contents?: true

Size: 1.19 KB

Versions: 4

Compression:

Stored size: 1.19 KB

Contents

# frozen_string_literal: true
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::Words["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

4 entries across 4 versions & 1 rubygems

Version Path
wordlist-1.1.0 lib/wordlist/operators/unique.rb
wordlist-1.0.3 lib/wordlist/operators/unique.rb
wordlist-1.0.2 lib/wordlist/operators/unique.rb
wordlist-1.0.1 lib/wordlist/operators/unique.rb