Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Sort ¶
func Sort[T constraints.Ordered](cmp func(a, b T) int) func(ro.Observable[T]) ro.Observable[T]
Sort sorts the observable values using the provided comparison function. Play: https://go.dev/play/p/3hL6m9jK5nV
Example ¶
// Sort values using the default comparison function for ordered types
observable := ro.Pipe1(
ro.Just(3, 1, 4, 1, 5, 9, 2, 6),
Sort(func(a, b int) int {
return a - b
}),
)
subscription := observable.Subscribe(ro.PrintObserver[int]())
defer subscription.Unsubscribe()
Output: Next: 1 Next: 1 Next: 2 Next: 3 Next: 4 Next: 5 Next: 6 Next: 9 Completed
Example (SortingStrings) ¶
// Sort strings
observable := ro.Pipe1(
ro.Just("zebra", "apple", "banana", "cherry"),
Sort(func(a, b string) int {
return strings.Compare(a, b)
}),
)
subscription := observable.Subscribe(ro.PrintObserver[string]())
defer subscription.Unsubscribe()
Output: Next: apple Next: banana Next: cherry Next: zebra Completed
func SortFunc ¶
func SortFunc[T comparable](cmp func(a, b T) int) func(ro.Observable[T]) ro.Observable[T]
SortFunc sorts the observable values using the provided comparison function. Play: https://go.dev/play/p/PzNTA9Vufy7
Example ¶
// Sort values using a custom comparison function
type User struct {
Name string
Age int
}
observable := ro.Pipe1(
ro.Just(
User{Name: "Alice", Age: 30},
User{Name: "Bob", Age: 25},
User{Name: "Charlie", Age: 35},
),
SortFunc(func(a, b User) int {
return a.Age - b.Age // Sort by age
}),
)
subscription := observable.Subscribe(ro.PrintObserver[User]())
defer subscription.Unsubscribe()
Output: Next: {Bob 25} Next: {Alice 30} Next: {Charlie 35} Completed
Example (ComplexLogic) ¶
// Sort with complex logic
type Product struct {
Name string
Price float64
Category string
}
observable := ro.Pipe1(
ro.Just(
Product{Name: "Laptop", Price: 999.99, Category: "Electronics"},
Product{Name: "Book", Price: 19.99, Category: "Books"},
Product{Name: "Phone", Price: 699.99, Category: "Electronics"},
Product{Name: "Pen", Price: 2.99, Category: "Office"},
),
SortFunc(func(a, b Product) int {
// Sort by category first, then by price
if a.Category != b.Category {
return strings.Compare(a.Category, b.Category)
}
if a.Price < b.Price {
return -1
}
if a.Price > b.Price {
return 1
}
return 0
}),
)
subscription := observable.Subscribe(ro.PrintObserver[Product]())
defer subscription.Unsubscribe()
Output: Next: {Book 19.99 Books} Next: {Phone 699.99 Electronics} Next: {Laptop 999.99 Electronics} Next: {Pen 2.99 Office} Completed
Example (SortingWithCustomStringLogic) ¶
// Sort strings by length, then alphabetically
observable := ro.Pipe1(
ro.Just("cat", "dog", "elephant", "ant", "bird"),
SortFunc(func(a, b string) int {
// Sort by length first
if len(a) != len(b) {
return len(a) - len(b)
}
// Then alphabetically
return strings.Compare(a, b)
}),
)
subscription := observable.Subscribe(ro.PrintObserver[string]())
defer subscription.Unsubscribe()
Output: Next: ant Next: cat Next: dog Next: bird Next: elephant Completed
func SortStableFunc ¶
func SortStableFunc[T comparable](cmp func(a, b T) int) func(ro.Observable[T]) ro.Observable[T]
SortStableFunc sorts the observable values using the provided stable comparison function. Play: https://go.dev/play/p/6b1tIxX9gfO
Example ¶
// Sort values using a custom comparison function with stable sorting
type Event struct {
Timestamp time.Time
Priority int
Message string
}
observable := ro.Pipe2(
ro.Just(
Event{Timestamp: time.Now(), Priority: 1, Message: "First"},
Event{Timestamp: time.Now().Add(time.Second), Priority: 2, Message: "Second"},
Event{Timestamp: time.Now().Add(2 * time.Second), Priority: 1, Message: "Third"},
),
SortStableFunc(func(a, b Event) int {
return a.Priority - b.Priority // Sort by priority, stable
}),
ro.Map(func(event Event) Event {
return Event{Priority: event.Priority, Message: event.Message}
}),
)
subscription := observable.Subscribe(ro.PrintObserver[Event]())
defer subscription.Unsubscribe()
Output: Next: {0001-01-01 00:00:00 +0000 UTC 1 First} Next: {0001-01-01 00:00:00 +0000 UTC 1 Third} Next: {0001-01-01 00:00:00 +0000 UTC 2 Second} Completed
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.