mirror of
https://github.com/gohugoio/hugo.git
synced 2026-08-24 15:28:54 +00:00
45c00b7c16
Closes #14936 Closes #14950 Closes #14965
67 lines
1.4 KiB
Go
67 lines
1.4 KiB
Go
package hiter
|
|
|
|
// Common iterator functions.
|
|
// Some of these are based on this discsussion: https://github.com/golang/go/issues/61898
|
|
|
|
import "iter"
|
|
|
|
// Concat returns an iterator over the concatenation of the sequences.
|
|
// Any nil sequences are ignored.
|
|
func Concat[V any](seqs ...iter.Seq[V]) iter.Seq[V] {
|
|
return func(yield func(V) bool) {
|
|
for _, seq := range seqs {
|
|
if seq == nil {
|
|
continue
|
|
}
|
|
for e := range seq {
|
|
if !yield(e) {
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Concat2 returns an iterator over the concatenation of the sequences.
|
|
// Any nil sequences are ignored.
|
|
func Concat2[K, V any](seqs ...iter.Seq2[K, V]) iter.Seq2[K, V] {
|
|
return func(yield func(K, V) bool) {
|
|
for _, seq := range seqs {
|
|
if seq == nil {
|
|
continue
|
|
}
|
|
for k, v := range seq {
|
|
if !yield(k, v) {
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Lock returns an iterator that locks before iterating and unlocks after.
|
|
func Lock[V any](seq iter.Seq[V], lock, unlock func()) iter.Seq[V] {
|
|
return func(yield func(V) bool) {
|
|
lock()
|
|
defer unlock()
|
|
for e := range seq {
|
|
if !yield(e) {
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Lock2 returns an iterator that locks before iterating and unlocks after.
|
|
func Lock2[K, V any](seq iter.Seq2[K, V], lock, unlock func()) iter.Seq2[K, V] {
|
|
return func(yield func(K, V) bool) {
|
|
lock()
|
|
defer unlock()
|
|
for k, v := range seq {
|
|
if !yield(k, v) {
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}
|