1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-11 16:22:26 +08:00

feat: add Pop for set

This commit is contained in:
dudaodong
2023-03-20 11:10:35 +08:00
parent 70d0adde42
commit 965e5fbcda
4 changed files with 256 additions and 162 deletions

View File

@@ -181,3 +181,15 @@ func (s Set[T]) EachWithBreak(iteratee func(item T) bool) {
}
}
}
// Pop delete the top element of set then return it, if set is empty, return nil-value of T and false.
func (s Set[T]) Pop() (v T, ok bool) {
if len(s) > 0 {
items := s.Values()
item := items[len(s)-1]
delete(s, item)
return item, true
}
return v, false
}