Module: Async::Enumerable::Methods::Predicates::One
- Defined in:
- lib/async/enumerable/methods/predicates/one.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#one?(pattern = nil) {|item| ... } ⇒ Boolean
Returns true if exactly one element satisfies the condition (parallel).
Class Method Details
.included(base) ⇒ Object
8 9 10 11 12 |
# File 'lib/async/enumerable/methods/predicates/one.rb', line 8 def self.included(base) base.include(::Enumerable) # Dependency base.include(Configurable) # Dependency for collection resolution base.include(ConcurrencyBounder) # Dependency end |
Instance Method Details
#one?(pattern = nil) {|item| ... } ⇒ Boolean
Returns true if exactly one element satisfies the condition (parallel).
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/async/enumerable/methods/predicates/one.rb', line 17 def one?(pattern = nil, &block) # Delegate pattern/no-block cases to wrapped enumerable to avoid break issues if pattern return __async_enumerable_collection.one?(pattern) elsif !block_given? return __async_enumerable_collection.one? end count = Concurrent::AtomicFixnum.new(0) __async_enumerable_bounded_concurrency(early_termination: true) do || __async_enumerable_collection.each do |item| break if count.value > 1 .async do if block.call(item) if count.increment > 1 # Stop the barrier early when we have too many matches .stop end end end end end count.value == 1 end |