Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

112
Views
The best way to implement "I saw it at least once" in Combine

I need to implement something like "that particular value was emitted by publisher at least once in the past". So basically true/false. As an example to answer if zero was ever send here:

let publisher = PassthroughSubject<Int, Never>()
...
for _ in 1...3 {
    publisher.send(Int.random(in: 0...2))
}

I came up only with the following options and both feels unnatural somehow:

//Option #1 Using scan
let hadZero = publisher
    .scan(false) { $0 || $1 == 0 }

//Option #2 Merging with CurrentValueSubject
let hadZero = Publishers.Merge(
    CurrentValueSubject<Bool, Never>(false),
    publisher.contains { $0 == 0 }
)

Is there any better way to do it?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can use the contains operator, which emits true immediately when the value sought is emitted by its upstream or emits false when the upstream completes without having emitted the sought value.

let hadZero = publisher.contains(0)
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!