Skip to content

Commit

Permalink
feat(iter): update iter functions
Browse files Browse the repository at this point in the history
  • Loading branch information
leaxoy committed Dec 7, 2023
1 parent acc5904 commit 2b6f48a
Show file tree
Hide file tree
Showing 11 changed files with 872 additions and 268 deletions.
93 changes: 78 additions & 15 deletions collections/ordered/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ func (self *Map[K, V]) Insert(key K, value V) optional.Optional[V] {
}

func (self *Map[K, V]) InsertIter(it iter.Seq[MapEntry[K, V]]) {
iter.ForEach(it, func(me MapEntry[K, V]) {
self.insertEntry(me)
})
iter.CollectFunc(it, func(me MapEntry[K, V]) bool { self.insertEntry(me); return true })
}

func (self *Map[K, V]) insertEntry(entry MapEntry[K, V]) {
Expand Down Expand Up @@ -91,7 +89,7 @@ func (self *Map[K, V]) Clone() *Map[K, V] {
// Reverse returns a reversed copy of the Map.
func (self *Map[K, V]) Reverse() *Map[K, V] {
newTree := NewMap[K, V](invert(self.cmp))
iter.ForEach(self.EntryIter(), newTree.insertEntry)
iter.ForEach(self.Entries(), newTree.insertEntry)
return newTree
}

Expand All @@ -101,11 +99,26 @@ func (self *Map[K, V]) ContainsKey(key K) bool {
return ok
}

// ContainsAll tests is all elements in [Seq] is a valid map key.
func (self *Map[K, V]) ContainsAll(it iter.Seq[K]) bool {
return it.All(self.ContainsKey)
}

// ContainsAny tests is any elements in [Seq] is a valid map key.
func (self *Map[K, V]) ContainsAny(it iter.Seq[K]) bool {
return it.Any(self.ContainsKey)
}

// Remove removes the MapEntry for the given key.
func (self *Map[K, V]) Remove(key K) {
self.inner.Delete(self.keyEntry(key))
}

// RemoveIter remove all elements in [Seq].
func (self *Map[K, V]) RemoveIter(it iter.Seq[K]) {
it.ForEach(self.Remove)
}

// First returns the first MapEntry.
func (self *Map[K, V]) First() optional.Optional[MapEntry[K, V]] {
return optional.FromPair(self.inner.Min())
Expand All @@ -126,29 +139,79 @@ func (self *Map[K, V]) PopLast() optional.Optional[MapEntry[K, V]] {
return optional.FromPair(self.inner.PopMax())
}

// KeyIter returns an iterator over the keys in the map.
func (self *Map[K, V]) KeyIter() iter.Seq[K] {
return iter.Map(self.inner.Scan, func(e MapEntry[K, V]) K { return e.Key() })
// Keys returns an iterator over the keys in the map.
func (self *Map[K, V]) Keys() iter.Seq[K] {
return func(yield func(K) bool) {
self.inner.Scan(func(item MapEntry[K, V]) bool { return yield(item.Key()) })
}
}

// ValueIter returns an iterator over the keys in the map.
func (self *Map[K, V]) ValueIter() iter.Seq[V] {
return iter.Map(self.inner.Scan, func(e MapEntry[K, V]) V { return e.Value() })
func (self *Map[K, V]) KeysMut() iter.Seq[*KeyItem[K, V]] {
return func(yield func(*KeyItem[K, V]) bool) {
self.inner.Scan(func(item MapEntry[K, V]) bool {
return yield(&KeyItem[K, V]{key: item.Key(), m: self})
})
}
}

// EntryIter returns an iterator over the keys in the map.
func (self *Map[K, V]) EntryIter() iter.Seq[MapEntry[K, V]] {
return self.inner.Scan
// Values returns an iterator over the keys in the map.
func (self *Map[K, V]) Values() iter.Seq[V] {
return func(yield func(V) bool) {
self.inner.Scan(func(item MapEntry[K, V]) bool { return yield(item.Value()) })
}
}

func (self *Map[K, V]) ValuesMut() iter.Seq[*ValueItem[K, V]] {
return func(yield func(*ValueItem[K, V]) bool) {
self.inner.Scan(func(item MapEntry[K, V]) bool {
return yield(&ValueItem[K, V]{key: item.Key(), m: self})
})
}
}

// Entries returns an iterator over the keys in the map.
func (self *Map[K, V]) Entries() iter.Seq[MapEntry[K, V]] { return self.inner.Scan }

// Len returns the number of entries in the map.
func (self *Map[K, V]) Len() int {
return self.inner.Len()
}

func (self *Map[K, V]) Merge(o *Map[K, V]) {
func (self *Map[K, V]) MergeKeep(o *Map[K, V]) {
self.MergeFunc(o, func(_ K, prev V, _ V) V { return prev })
}

func (self *Map[K, V]) MergeOverwrite(o *Map[K, V]) {
self.MergeFunc(o, func(key K, prev V, current V) V { return current })
}

func (self *Map[K, V]) MergeFunc(o *Map[K, V], solve func(key K, prev V, current V) V) {
o.inner.Scan(func(item MapEntry[K, V]) bool {
self.Insert(item.Key(), item.Value())
prev := self.GetEntry(item.Key())
v := item.Value()
if prev.IsSome() {
v = solve(item.Key(), prev.Value().Value(), v)
}
self.Insert(item.Key(), v)
return true
})
}

type KeyItem[K any, V any] struct {
key K
m *Map[K, V]
}

func (k *KeyItem[K, V]) Key() K { return k.key }
func (k *KeyItem[K, V]) Set(v V) { k.m.Insert(k.key, v) }
func (k *KeyItem[K, V]) Remove() { k.m.Remove(k.key) }

type ValueItem[K any, V any] struct {
val V
key K
m *Map[K, V]
}

func (v *ValueItem[K, V]) Value() V { return v.val }
func (v *ValueItem[K, V]) Set(val V) { v.m.Insert(v.key, val) }
func (v *ValueItem[K, V]) Remove() { v.m.Remove(v.key) }
28 changes: 19 additions & 9 deletions collections/ordered/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (self *Set[T]) InsertMany(elements ...T) {
}

func (self *Set[T]) InsertIter(it iter.Seq[T]) {
iter.ForEach(it, func(t T) { self.inner.Set(t) })
it.ForEach(func(t T) { self.inner.Set(t) })
}

// Remove removes elements from the set.
Expand Down Expand Up @@ -119,7 +119,7 @@ func (self *Set[T]) Difference(o *Set[T]) *Set[T] {
// Union returns the union of the two sets.
func (self *Set[T]) Union(o *Set[T]) *Set[T] {
cloned := self.Clone()
iter.ForEach(o.AscendIter(), cloned.Insert)
cloned.InsertIter(o.AscendIter())
return cloned
}

Expand All @@ -134,16 +134,26 @@ func (self *Set[T]) SupersetOf(o *Set[T]) bool {
}

// Len returns the number of elements in the set.
func (self *Set[T]) Len() int {
return self.inner.Len()
}
func (self *Set[T]) Len() int { return self.inner.Len() }

// AscendIter returns an iter over the set in ascend order.
func (self *Set[T]) AscendIter() iter.Seq[T] {
return self.inner.Scan
func (self *Set[T]) AscendIter() iter.Seq[T] { return self.inner.Scan }

func (self *Set[T]) AscendIterMut() iter.Seq[*SetItem[T]] {
return iter.Map(self.inner.Scan, func(e T) *SetItem[T] { return &SetItem[T]{item: e, s: self} })
}

// DescendIter returns an iter over the set in descend order.
func (self *Set[T]) DescendIter() iter.Seq[T] {
return self.inner.Reverse
func (self *Set[T]) DescendIter() iter.Seq[T] { return self.inner.Reverse }

func (self *Set[T]) DescendIterMut() iter.Seq[*SetItem[T]] {
return iter.Map(self.inner.ReverseMut, func(e T) *SetItem[T] { return &SetItem[T]{item: e, s: self} })
}

type SetItem[E any] struct {
item E
s *Set[E]
}

func (s *SetItem[E]) Value() E { return s.item }
func (s *SetItem[E]) Remove() { s.s.Remove(s.item) }
18 changes: 4 additions & 14 deletions collections/stack/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ func FromSlice[E any](elems ...E) *Stack[E] {
return &Stack[E]{elements: elems}
}

func (s *Stack[E]) Push(elem E) {
s.elements = append(s.elements, elem)
}
func (s *Stack[E]) Push(elem E) { s.elements = append(s.elements, elem) }

func (s *Stack[E]) Pop() (e E, ok bool) {
if len(s.elements) == 0 {
Expand All @@ -40,14 +38,6 @@ func (s *Stack[E]) Peek() (e E, ok bool) {
return
}

func (s *Stack[E]) IsEmpty() bool {
return len(s.elements) == 0
}

func (s *Stack[E]) Size() int {
return len(s.elements)
}

func (s *Stack[E]) Iter() iter.Seq[E] {
return slices.BackwardSeq(s.elements)
}
func (s *Stack[E]) IsEmpty() bool { return len(s.elements) == 0 }
func (s *Stack[E]) Size() int { return len(s.elements) }
func (s *Stack[E]) Iter() iter.Seq[E] { return slices.Backward(s.elements) }
32 changes: 32 additions & 0 deletions iter/iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,35 @@ package iter
//
// see: https://github.com/golang/go/issues/61897
type Seq[E any] func(yield func(E) bool)

func (s Seq[E]) ForEach(f func(E)) { ForEach(s, f) }
func (s Seq[E]) TryForEach(f func(E) error) error { return TryForEach(s, f) }
func (s Seq[E]) Map(f func(E) E) Seq[E] { return Map(s, f) }
func (s Seq[E]) MapWhile(f func(E) (E, bool)) Seq[E] { return MapWhile(s, f) }
func (s Seq[E]) TryFold(init E, f func(E, E) (E, error)) (E, error) { return TryFold(s, init, f) }
func (s Seq[E]) Fold(init E, f func(E, E) E) E { return Fold(s, init, f) }
func (s Seq[E]) Reduce(f func(E, E) E) (E, bool) { return Reduce(s, f) }
func (s Seq[E]) Filter(f func(E) bool) Seq[E] { return Filter(s, f) }
func (s Seq[E]) FilterMap(f func(E) (E, bool)) Seq[E] { return FilterMap(s, f) }
func (s Seq[E]) MaxFunc(f func(E, E) int) (E, bool) { return MaxFunc(s, f) }
func (s Seq[E]) MinFunc(f func(E, E) int) (E, bool) { return MinFunc(s, f) }
func (s Seq[E]) Find(f func(E) bool) (E, bool) { return Find(s, f) }
func (s Seq[E]) FindMap(f func(E) (E, bool)) (E, bool) { return FindMap(s, f) }
func (s Seq[E]) Index(f func(E) bool) (int, bool) { return Index(s, f) }
func (s Seq[E]) All(f func(E) bool) bool { return All(s, f) }
func (s Seq[E]) Any(f func(E) bool) bool { return Any(s, f) }
func (s Seq[E]) CountFunc(f func(E) bool) int { return CountFunc(s, f) }
func (s Seq[E]) Size() int { return Size(s) }
func (s Seq[E]) IsSortedFunc(f func(E, E) int) bool { return IsSortedFunc(s, f) }
func (s Seq[E]) StepBy(n int) Seq[E] { return StepBy(s, n) }
func (s Seq[E]) Take(n int) Seq[E] { return Take(s, n) }
func (s Seq[E]) TakeWhile(f func(E) bool) Seq[E] { return TakeWhile(s, f) }
func (s Seq[E]) Skip(n int) Seq[E] { return Skip(s, n) }
func (s Seq[E]) SkipWhile(f func(E) bool) Seq[E] { return SkipWhile(s, f) }
func (s Seq[E]) Chain(y Seq[E]) Seq[E] { return Chain(s, y) }
func (s Seq[E]) CollectFunc(collect func(E) bool) { CollectFunc(s, collect) }
func (s Seq[E]) Intersperse(sep E) Seq[E] { return Intersperse(s, sep) }
func (s Seq[E]) First(f func(E) bool) (E, bool) { return First(s, f) }
func (s Seq[E]) Last(f func(E) bool) (E, bool) { return Last(s, f) }
func (s Seq[E]) Inspect(f func(E)) Seq[E] { return Inspect(s, f) }
func (s Seq[E]) DedupFunc(f func(E, E) bool) Seq[E] { return DedupFunc(s, f) }
Loading

0 comments on commit 2b6f48a

Please sign in to comment.