Skip to main content
Help improve this documentation

This documentation is still new and evolving. If you spot any mistakes, unclear explanations, or missing details, please open an issue.

Your feedback helps us improve!

Conditional operatorsโ€‹

This page lists all conditional operations, available in the core package of ro.

  • Determines whether all elements of an observable sequence satisfy a condition.

    obs := ro.Pipe(
    ro.Just(1, 2, 3, 4, 5),
    ro.All(func(i int) bool {
    return i > 0
    }),
    )

    sub := obs.Subscribe(ro.PrintObserver[bool]())
    defer sub.Unsubscribe()

    // Next: true
    // Completed

    With context

    obs := ro.Pipe(
    ro.Just(1, 2, 3, 4, 5),
    ro.AllWithContext(func(ctx context.Context, n int) bool {
    return n > 0
    }),
    )

    sub := obs.Subscribe(ro.PrintObserver[bool]())
    defer sub.Unsubscribe()

    // Next: true
    // Completed

    With index

    obs := ro.Pipe(
    ro.Just(1, 2, 3, 4, 5),
    ro.AllI(func(n int, index int64) bool {
    return index < 3 // Only check first 3 elements
    }),
    )

    sub := obs.Subscribe(ro.PrintObserver[bool]())
    defer sub.Unsubscribe()

    // Next: true
    // Completed

    With index and context

    obs := ro.Pipe(
    ro.Just(1, 2, 3, 4, 5),
    ro.AllIWithContext(func(ctx context.Context, n int, index int64) bool {
    return n > 0 && index < 4
    }),
    )

    sub := obs.Subscribe(ro.PrintObserver[bool]())
    defer sub.Unsubscribe()

    // Next: true
    // Completed
    Prototypes:
    func All[T any](predicate func(item T) bool)
    func AllWithContext[T any](predicate func(ctx context.Context, item T) bool)
    func AllI[T any](predicate func(item T, index int64) bool)
    func AllIWithContext[T any](predicate func(ctx context.Context, item T, index int64) bool)
  • Determines whether any element of an observable sequence satisfies a condition.

    obs := ro.Pipe(
    ro.Just(1, 2, 3, 4, 5),
    ro.Contains(func(i int) bool {
    return i == 3
    }),
    )

    sub := obs.Subscribe(ro.PrintObserver[bool]())
    defer sub.Unsubscribe()

    // Next: true
    // Completed

    With context

    obs := ro.Pipe(
    ro.Just(1, 2, 3, 4, 5),
    ro.ContainsWithContext(func(ctx context.Context, n int) bool {
    return n == 3
    }),
    )

    sub := obs.Subscribe(ro.PrintObserver[bool]())
    defer sub.Unsubscribe()

    // Next: true
    // Completed

    With index

    obs := ro.Pipe(
    ro.Just("apple", "banana", "cherry"),
    ro.ContainsI(func(item string, index int64) bool {
    return index == 1 && item == "banana"
    }),
    )

    sub := obs.Subscribe(ro.PrintObserver[bool]())
    defer sub.Unsubscribe()

    // Next: true
    // Completed

    With index and context

    obs := ro.Pipe(
    ro.Just(1, 2, 3, 4, 5),
    ro.ContainsIWithContext(func(ctx context.Context, n int, index int64) bool {
    return n > 3 && index >= 3
    }),
    )

    sub := obs.Subscribe(ro.PrintObserver[bool]())
    defer sub.Unsubscribe()

    // Next: true
    // Completed
    Prototypes:
    func Contains[T any](predicate func(item T) bool)
    func ContainsWithContext[T any](predicate func(ctx context.Context, item T) bool)
    func ContainsI[T any](predicate func(item T, index int64) bool)
    func ContainsIWithContext[T any](predicate func(ctx context.Context, item T, index int64) bool)
  • Finds the first element in an observable sequence that satisfies a condition.

    obs := ro.Pipe(
    ro.Just(1, 2, 3, 4, 5),
    ro.Find(func(i int) bool {
    return i%2 == 0
    }),
    )

    sub := obs.Subscribe(ro.PrintObserver[int]())
    defer sub.Unsubscribe()

    // Next: 2
    // Completed

    With context

    obs := ro.Pipe(
    ro.Just(1, 2, 3, 4, 5),
    ro.FindWithContext(func(ctx context.Context, n int) bool {
    return n > 3
    }),
    )

    sub := obs.Subscribe(ro.PrintObserver[int]())
    defer sub.Unsubscribe()

    // Next: 4
    // Completed

    With index

    obs := ro.Pipe(
    ro.Just("a", "b", "c", "d", "e"),
    ro.FindI(func(item string, index int64) bool {
    return index >= 2 // Find item at position 2
    }),
    )

    sub := obs.Subscribe(ro.PrintObserver[string]())
    defer sub.Unsubscribe()

    // Next: c
    // Completed

    With index and context

    obs := ro.Pipe(
    ro.Just(10, 20, 30, 40, 50),
    ro.FindIWithContext(func(ctx context.Context, n int, index int64) bool {
    return n > 25 && index > 1
    }),
    )

    sub := obs.Subscribe(ro.PrintObserver[int]())
    defer sub.Unsubscribe()

    // Next: 30
    // Completed
    Prototypes:
    func Find[T any](predicate func(item T) bool)
    func FindWithContext[T any](predicate func(ctx context.Context, item T) bool)
    func FindI[T any](predicate func(item T, index int64) bool)
    func FindIWithContext[T any](predicate func(ctx context.Context, item T, index int64) bool)
  • Conditionally selects between two observables based on a condition function.

    condition := func() bool {
    return time.Now().Hour() < 12 // Before noon
    }

    obs := ro.Iif(
    condition,
    ro.Just("Good morning"),
    ro.Just("Good afternoon"),
    )

    sub := obs.Subscribe(ro.PrintObserver[string]())
    defer sub.Unsubscribe()

    // Next: Good morning (if before noon) or Good afternoon (if after noon)
    // Completed

    Dynamic condition based on data

    useFallback := false
    condition := func() bool {
    return !useFallback
    }

    primarySource := ro.Just(1, 2, 3)
    fallbackSource := ro.Just(0)

    obs := ro.Iif(condition, primarySource, fallbackSource)

    sub := obs.Subscribe(ro.PrintObserver[int]())
    defer sub.Unsubscribe()

    // Next: 1
    // Next: 2
    // Next: 3
    // Completed

    With error handling

    isValid := true
    condition := func() bool {
    return isValid
    }

    validSource := ro.Just("success")
    errorSource := ro.Throw[string](errors.New("invalid state"))

    obs := ro.Iif(condition, validSource, errorSource)

    sub := obs.Subscribe(ro.PrintObserver[string]())
    defer sub.Unsubscribe()

    // Next: success
    // Completed
    Prototype:
    func Iif[T any](condition func() bool, trueSource Observable[T], falseSource Observable[T])
  • DefaultIfEmptyโ€‹

    Emits a default value if the source observable completes without emitting any items.

    obs := ro.Pipe(
    ro.Empty[int](),
    ro.DefaultIfEmpty(42),
    )

    sub := obs.Subscribe(ro.PrintObserver[int]())
    defer sub.Unsubscribe()

    // Next: 42
    // Completed

    With non-empty source

    obs := ro.Pipe(
    ro.Just(1, 2, 3),
    ro.DefaultIfEmpty(42),
    )

    sub := obs.Subscribe(ro.PrintObserver[int]())
    defer sub.Unsubscribe()

    // Next: 1
    // Next: 2
    // Next: 3
    // Completed

    With context

    obs := ro.Pipe(
    ro.Empty[string](),
    ro.DefaultIfEmptyWithContext("default value"),
    )

    sub := obs.Subscribe(ro.PrintObserver[string]())
    defer sub.Unsubscribe()

    // Next: default value
    // Completed

    With filtered empty result

    obs := ro.Pipe(
    ro.Just(1, 2, 3, 4, 5),
    ro.Filter(func(i int) bool {
    return i > 10 // No items match
    }),
    ro.DefaultIfEmpty(-1),
    )

    sub := obs.Subscribe(ro.PrintObserver[int]())
    defer sub.Unsubscribe()

    // Next: -1
    // Completed
    Prototypes:
    func DefaultIfEmpty[T any](defaultValue T)
    func DefaultIfEmptyWithContext[T any](defaultValue T)
  • SequenceEqualโ€‹

    Determines whether two observable sequences emit the same sequence of values.

    source := ro.Just(1, 2, 3)
    compareTo := ro.Just(1, 2, 3)

    obs := ro.Pipe(
    source,
    ro.SequenceEqual(compareTo),
    )

    sub := obs.Subscribe(ro.PrintObserver[bool]())
    defer sub.Unsubscribe()

    // Next: true
    // Completed

    With different sequences

    source := ro.Just(1, 2, 3)
    compareTo := ro.Just(1, 2, 4)

    obs := ro.Pipe(
    source,
    ro.SequenceEqual(compareTo),
    )

    sub := obs.Subscribe(ro.PrintObserver[bool]())
    defer sub.Unsubscribe()

    // Next: false
    // Completed

    With different length sequences

    source := ro.Just(1, 2, 3)
    compareTo := ro.Just(1, 2)

    obs := ro.Pipe(
    source,
    ro.SequenceEqual(compareTo),
    )

    sub := obs.Subscribe(ro.PrintObserver[bool]())
    defer sub.Unsubscribe()

    // Next: false
    // Completed

    With strings

    source := ro.Just("hello", "world")
    compareTo := ro.Just("hello", "world")

    obs := ro.Pipe(
    source,
    ro.SequenceEqual(compareTo),
    )

    sub := obs.Subscribe(ro.PrintObserver[bool]())
    defer sub.Unsubscribe()

    // Next: true
    // Completed
    Prototype:
    func SequenceEqual[T comparable](compareTo Observable[T])