Back to Lo

It Maxby

docs/data/it-maxby.md

1.53.0485 B
Original Source

Searches maximum value using a custom comparison function. The comparison function should return true if the first argument is "greater than" the second.

Examples:

go
type Person struct {
    Name string
    Age  int
}
seq := func(yield func(Person) bool) {
    _ = yield(Person{"Alice", 30})
    _ = yield(Person{"Bob", 25})
    _ = yield(Person{"Charlie", 35})
}
oldest := it.MaxBy(seq, func(a, b Person) bool {
    return a.Age > b.Age
})
// oldest == Person{"Charlie", 35}