代码拉取完成,页面将自动刷新
// Package linq provides extra features for go's standard iter library.
package linq
import (
"cmp"
"errors"
"iter"
)
// Errors.
var (
ErrNotFound = errors.New("linq: element is not found")
ErrNotSingle = errors.New("linq: sequence is not single")
)
// Entry represents an entry of map.
type Entry[K comparable, V any] struct {
Key K // The map key.
Value V // The map value.
}
// Max returns the maximal element in s.
func Max[E cmp.Ordered](s Seq[E]) (E, error) {
return s.Max(cmp.Compare)
}
// Min returns the minimal element in s.
func Min[E cmp.Ordered](s Seq[E]) (E, error) {
return s.Min(cmp.Compare)
}
// Equal reports whether two sequences are equal.
func Equal[E comparable](s1, s2 Seq[E]) (bool, error) {
return s1.Equal(s2, func(e1, e2 E) bool { return e1 == e2 })
}
// Sort sorts a sequence of any ordered type in ascending order.
func Sort[E cmp.Ordered](s Seq[E]) Seq[E] {
return s.Sort(cmp.Compare)
}
// Map maps sequence using function f.
func Map[E, R any](s Seq[E], f func(E) (R, error)) Seq[R] {
return func(yield func(R, error) bool) {
for e, err := range s {
if err != nil {
var r R
yield(r, err)
return
}
if !yield(f(e)) {
return
}
}
}
}
// Zip applies a specified function to the corresponding elements of two sequences, producing a sequence of the results.
func Zip[X, Y, R any](xs Seq[X], ys Seq[Y], f func(X, Y) (R, error)) Seq[R] {
return func(yield func(R, error) bool) {
next, stop := iter.Pull2(iter.Seq2[Y, error](ys))
defer stop()
var zero R
for x, err := range xs {
if err != nil {
yield(zero, err)
return
}
y, err, ok := next()
if !ok {
return
}
if err != nil {
yield(zero, err)
return
}
if !yield(f(x, y)) {
return
}
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。