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.
Allโ
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
// CompletedWith 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
// CompletedWith 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
// CompletedWith 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
// CompletedPrototypes: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)Containsโ
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
// CompletedWith 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
// CompletedWith 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
// CompletedWith 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
// CompletedPrototypes: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)Findโ
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
// CompletedWith 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
// CompletedWith 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
// CompletedWith 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
// CompletedPrototypes: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)Iifโ
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)
// CompletedDynamic 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
// CompletedWith 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
// CompletedPrototype: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
// CompletedWith 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
// CompletedWith context
obs := ro.Pipe(
ro.Empty[string](),
ro.DefaultIfEmptyWithContext("default value"),
)
sub := obs.Subscribe(ro.PrintObserver[string]())
defer sub.Unsubscribe()
// Next: default value
// CompletedWith 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
// CompletedVariant: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
// CompletedWith 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
// CompletedWith 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
// CompletedWith 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
// CompletedPrototype:func SequenceEqual[T comparable](compareTo Observable[T])