1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-03-01 00:35:28 +08:00

Compare commits

...

5 Commits

Author SHA1 Message Date
dudaodong
aebab7c944 refactor: break change, rename constructor of set (NewSet->New, NewSetFromSlice->FromSlice) 2024-02-25 09:32:32 +08:00
dudaodong
665bad4ca3 doc: update doc for IndexOfFunc and LastIndexOfFunc 2024-02-25 09:25:07 +08:00
donutloop
e4901e99e9 Fix optional doc links (#179) 2024-02-24 18:12:44 +08:00
donutloop
4277e8eca5 CopyOnWriteList add IndexOfFunc and LastIndexOfFunc (#178)
Allow users to apply functional predicates alongside 'index of' and 'last index of' search methods in this specialized list variant
2024-02-24 17:53:58 +08:00
donutloop
fdc93c8cc7 Change naming (#177)
Utilize terminology from the Go SDK rather than introducing novel terms to describe concepts.
2024-02-24 17:25:31 +08:00
13 changed files with 475 additions and 310 deletions

View File

@@ -608,6 +608,7 @@ import set "github.com/duke-git/lancet/v2/datastructure/set"
import tree "github.com/duke-git/lancet/v2/datastructure/tree"
import heap "github.com/duke-git/lancet/v2/datastructure/heap"
import hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
import optional "github.com/duke-git/lancet/v2/datastructure/optional"
```
#### Structure list:
@@ -630,6 +631,9 @@ import hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/datastructure/heap.md)]
- **<big>Hashmap</big>** : hash map structure.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/datastructure/hashmap.md)]
- **<big>Optional</big>** : Optional container.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/datastructure/optional.md)]
<h3 id="fileutil"> 9. Fileutil package implements some basic functions for file operations. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3>

View File

@@ -90,6 +90,35 @@ func lastIndexOf[T any](o T, e []T, start int, end int) int {
return -1
}
// LastIndexOfFunc returns the index of the last occurrence of the value in this list satisfying the
// functional predicate f(T) bool
// if not found return -1.
func (l *CopyOnWriteList[T]) LastIndexOfFunc(f func(T) bool) int {
index := -1
data := l.getList()
for i := len(data) - 1; i >= 0; i-- {
if f(data[i]) {
index = i
break
}
}
return index
}
// IndexOfFunc returns the first index satisfying the functional predicate f(v) bool
// if not found return -1.
func (l *CopyOnWriteList[T]) IndexOfFunc(f func(T) bool) int {
index := -1
data := l.getList()
for i, v := range data {
if f(v) {
index = i
break
}
}
return index
}
// get returns the element at the specified position in this list.
func get[T any](o []T, index int) *T {
return &o[index]

View File

@@ -1,8 +1,9 @@
package datastructure
import (
"github.com/duke-git/lancet/v2/internal"
"testing"
"github.com/duke-git/lancet/v2/internal"
)
func TestCopyOnWriteList_ValueOf(t *testing.T) {
@@ -233,3 +234,35 @@ func TestCopyOnWriteList_SubList(t *testing.T) {
subList = list.SubList(11, 1)
assert.Equal([]int{}, subList)
}
func TestCopyOnWriteListIndexOfFunc(t *testing.T) {
t.Parallel()
assert := internal.NewAssert(t, "TestIndexOfFunc")
list := NewCopyOnWriteList([]int{1, 2, 3})
i := list.IndexOfFunc(func(a int) bool { return a == 1 })
assert.Equal(0, i)
i = list.IndexOfFunc(func(a int) bool { return a == 4 })
assert.Equal(-1, i)
}
func TestNewCopyOnWriteListLastIndexOfFunc(t *testing.T) {
t.Parallel()
assert := internal.NewAssert(t, "TestLastIndexOfFunc")
list := NewCopyOnWriteList([]int{1, 2, 3, 3, 3, 3, 4, 5, 6, 9})
i := list.LastIndexOfFunc(func(a int) bool { return a == 3 })
assert.Equal(5, i)
i = list.LastIndexOfFunc(func(a int) bool { return a == 10 })
assert.Equal(-1, i)
i = list.LastIndexOfFunc(func(a int) bool { return a == 4 })
assert.Equal(6, i)
i = list.LastIndexOfFunc(func(a int) bool { return a == 1 })
assert.Equal(0, i)
}

View File

@@ -10,8 +10,8 @@ type Optional[T any] struct {
mu *sync.RWMutex
}
// Empty returns an empty Optional instance.
func Empty[T any]() Optional[T] {
// Default returns an default Optional instance.
func Default[T any]() Optional[T] {
return Optional[T]{mu: &sync.RWMutex{}}
}
@@ -20,29 +20,29 @@ func Of[T any](value T) Optional[T] {
return Optional[T]{value: &value, mu: &sync.RWMutex{}}
}
// OfNullable returns an Optional for a given value, which may be nil.
func OfNullable[T any](value *T) Optional[T] {
// FromNillable returns an Optional for a given value, which may be nil.
func FromNillable[T any](value *T) Optional[T] {
if value == nil {
return Empty[T]()
return Default[T]()
}
return Optional[T]{value: value, mu: &sync.RWMutex{}}
}
// IsPresent checks if there is a value present.
func (o Optional[T]) IsPresent() bool {
// IsNotNil checks if there is a value present.
func (o Optional[T]) IsNotNil() bool {
o.mu.RLock()
defer o.mu.RUnlock()
return o.value != nil
}
// IsEmpty checks if the Optional is empty.
func (o Optional[T]) IsEmpty() bool {
return !o.IsPresent()
// IsNil checks if the Optional is nil.
func (o Optional[T]) IsNil() bool {
return !o.IsNotNil()
}
// IfPresent performs the given action with the value if a value is present.
func (o Optional[T]) IfPresent(action func(value T)) {
// IfNotNil performs the given action with the value if a value is not nil.
func (o Optional[T]) IfNotNil(action func(value T)) {
o.mu.RLock()
defer o.mu.RUnlock()
@@ -51,20 +51,20 @@ func (o Optional[T]) IfPresent(action func(value T)) {
}
}
// IfPresentOrElse performs the action with the value if present, otherwise performs the empty-based action.
func (o Optional[T]) IfPresentOrElse(action func(value T), emptyAction func()) {
// IfNotNilOrElse performs the action with the value if present, otherwise performs the fallback action.
func (o Optional[T]) IfNotNilOrElse(action func(value T), fallbackAction func()) {
o.mu.RLock()
defer o.mu.RUnlock()
if o.value != nil {
action(*o.value)
} else {
emptyAction()
fallbackAction()
}
}
// Get returns the value if present, otherwise panics.
func (o Optional[T]) Get() T {
// Unwarp returns the value if not nil, otherwise panics.
func (o Optional[T]) Unwarp() T {
o.mu.RLock()
defer o.mu.RUnlock()
@@ -74,7 +74,7 @@ func (o Optional[T]) Get() T {
return *o.value
}
// OrElse returns the value if present, otherwise returns other.
// OrElse returns the value if is not nil, otherwise returns other.
func (o Optional[T]) OrElse(other T) T {
o.mu.RLock()
defer o.mu.RUnlock()
@@ -85,24 +85,24 @@ func (o Optional[T]) OrElse(other T) T {
return other
}
// OrElseGet returns the value if present, otherwise invokes supplier and returns the result.
func (o Optional[T]) OrElseGet(supplier func() T) T {
// OrElseGet returns the value if is not nil, otherwise invokes action and returns the result.
func (o Optional[T]) OrElseGet(action func() T) T {
o.mu.RLock()
defer o.mu.RUnlock()
if o.value != nil {
return *o.value
}
return supplier()
return action()
}
// OrElseThrow returns the value if present, otherwise returns an error.
func (o Optional[T]) OrElseThrow(errorSupplier func() error) (T, error) {
// OrElseTrigger returns the value if present, otherwise returns an error.
func (o Optional[T]) OrElseTrigger(errorHandler func() error) (T, error) {
o.mu.RLock()
defer o.mu.RUnlock()
if o.value == nil {
return *new(T), errorSupplier()
return *new(T), errorHandler()
}
return *o.value, nil
}

View File

@@ -7,11 +7,11 @@ import (
"github.com/duke-git/lancet/v2/internal"
)
func TestEmpty(t *testing.T) {
func TestDefault(t *testing.T) {
assert := internal.NewAssert(t, "TestEmpty")
opt := Empty[int]()
opt := Default[int]()
assert.ShouldBeTrue(opt.IsEmpty())
assert.ShouldBeTrue(opt.IsNil())
}
func TestOf(t *testing.T) {
@@ -19,30 +19,30 @@ func TestOf(t *testing.T) {
value := 42
opt := Of(value)
assert.ShouldBeTrue(opt.IsPresent())
assert.Equal(opt.Get(), value)
assert.ShouldBeTrue(opt.IsNotNil())
assert.Equal(opt.Unwarp(), value)
}
func TestOfNullable(t *testing.T) {
func TestFromNillable(t *testing.T) {
assert := internal.NewAssert(t, "TestOfNullable")
var value *int = nil
opt := OfNullable(value)
opt := FromNillable(value)
assert.ShouldBeFalse(opt.IsPresent())
assert.ShouldBeFalse(opt.IsNotNil())
value = new(int)
*value = 42
opt = OfNullable(value)
opt = FromNillable(value)
assert.ShouldBeTrue(opt.IsPresent())
assert.ShouldBeTrue(opt.IsNotNil())
}
func TestOrElse(t *testing.T) {
assert := internal.NewAssert(t, "TestOrElse")
optEmpty := Empty[int]()
optDefault := Default[int]()
defaultValue := 100
val := optEmpty.OrElse(defaultValue)
val := optDefault.OrElse(defaultValue)
assert.Equal(val, defaultValue)
optWithValue := Of(42)
@@ -53,72 +53,72 @@ func TestOrElse(t *testing.T) {
func TestOrElseGetHappyPath(t *testing.T) {
assert := internal.NewAssert(t, "TestOrElseGetHappyPath")
optWithValue := Of(42)
supplier := func() int { return 100 }
action := func() int { return 100 }
val := optWithValue.OrElseGet(supplier)
val := optWithValue.OrElseGet(action)
assert.Equal(val, 42)
}
func TestOrElseGet(t *testing.T) {
assert := internal.NewAssert(t, "TestOrElseGet")
optEmpty := Empty[int]()
supplier := func() int { return 100 }
optDefault := Default[int]()
action := func() int { return 100 }
val := optEmpty.OrElseGet(supplier)
assert.Equal(val, supplier())
val := optDefault.OrElseGet(action)
assert.Equal(val, action())
}
func TestOrElseThrow(t *testing.T) {
assert := internal.NewAssert(t, "TestOrElseThrow")
optEmpty := Empty[int]()
_, err := optEmpty.OrElseThrow(func() error { return errors.New("no value") })
func TestOrElseTrigger(t *testing.T) {
assert := internal.NewAssert(t, "OrElseTrigger")
optDefault := Default[int]()
_, err := optDefault.OrElseTrigger(func() error { return errors.New("no value") })
assert.Equal(err.Error(), "no value")
optWithValue := Of(42)
val, err := optWithValue.OrElseThrow(func() error { return errors.New("no value") })
val, err := optWithValue.OrElseTrigger(func() error { return errors.New("no value") })
assert.IsNil(err)
assert.Equal(val, 42)
}
func TestIfPresent(t *testing.T) {
assert := internal.NewAssert(t, "TestIfPresent")
func TestIfNotNil(t *testing.T) {
assert := internal.NewAssert(t, "IfNotNil")
called := false
action := func(value int) { called = true }
optEmpty := Empty[int]()
optEmpty.IfPresent(action)
optDefault := Default[int]()
optDefault.IfNotNil(action)
assert.ShouldBeFalse(called)
called = false // Reset for next test
optWithValue := Of(42)
optWithValue.IfPresent(action)
optWithValue.IfNotNil(action)
assert.ShouldBeTrue(called)
}
func TestIfPresentOrElse(t *testing.T) {
assert := internal.NewAssert(t, "TestIfPresentOrElse")
func TestIfNotNilOrElse(t *testing.T) {
assert := internal.NewAssert(t, "TestIfNotNilOrElse")
// Test when value is present
calledWithValue := false
valueAction := func(value int) { calledWithValue = true }
emptyAction := func() { t.Errorf("Empty action should not be called when value is present") }
fallbackAction := func() { t.Errorf("Empty action should not be called when value is present") }
optWithValue := Of(42)
optWithValue.IfPresentOrElse(valueAction, emptyAction)
optWithValue.IfNotNilOrElse(valueAction, fallbackAction)
assert.ShouldBeTrue(calledWithValue)
// Test when value is not present
calledWithEmpty := false
valueAction = func(value int) { t.Errorf("Value action should not be called when value is not present") }
emptyAction = func() { calledWithEmpty = true }
fallbackAction = func() { calledWithEmpty = true }
optEmpty := Empty[int]()
optEmpty.IfPresentOrElse(valueAction, emptyAction)
optDefault := Default[int]()
optDefault.IfNotNilOrElse(valueAction, fallbackAction)
assert.ShouldBeTrue(calledWithEmpty)
}
@@ -133,19 +133,19 @@ func TestGetWithPanicStandard(t *testing.T) {
r := recover()
assert.IsNil(r)
}()
val := optWithValue.Get()
val := optWithValue.Unwarp()
if val != 42 {
t.Errorf("Expected Get to return 42, got %v", val)
t.Errorf("Expected Unwarp to return 42, got %v", val)
}
}()
// Test when value is not present
optEmpty := Empty[int]()
optDefault := Default[int]()
func() {
defer func() {
r := recover()
assert.IsNotNil(r)
}()
_ = optEmpty.Get()
_ = optDefault.Unwarp()
}()
}

View File

@@ -7,15 +7,15 @@ package datastructure
// Set is a data container, like slice, but element of set is not duplicate.
type Set[T comparable] map[T]struct{}
// NewSet return a instance of set
func NewSet[T comparable](items ...T) Set[T] {
// New create a instance of set from given values.
func New[T comparable](items ...T) Set[T] {
set := make(Set[T])
set.Add(items...)
return set
}
// NewSetFromSlice create a set from slice
func NewSetFromSlice[T comparable](items []T) Set[T] {
// FromSlice create a set from given slice.
func FromSlice[T comparable](items []T) Set[T] {
set := make(Set[T])
for _, item := range items {
set.Add(item)
@@ -77,7 +77,7 @@ func (s Set[T]) ContainAll(other Set[T]) bool {
// Clone return a copy of set
func (s Set[T]) Clone() Set[T] {
set := NewSet[T]()
set := New[T]()
set.Add(s.Values()...)
return set
}
@@ -135,7 +135,7 @@ func (s Set[T]) Union(other Set[T]) Set[T] {
// Intersection creates a new set whose element both be contained in set s and other
func (s Set[T]) Intersection(other Set[T]) Set[T] {
set := NewSet[T]()
set := New[T]()
s.Iterate(func(value T) {
if other.Contain(value) {
set.Add(value)
@@ -147,7 +147,7 @@ func (s Set[T]) Intersection(other Set[T]) Set[T] {
// SymmetricDifference creates a new set whose element is in set1 or set2, but not in both sets
func (s Set[T]) SymmetricDifference(other Set[T]) Set[T] {
set := NewSet[T]()
set := New[T]()
s.Iterate(func(value T) {
if !other.Contain(value) {
set.Add(value)
@@ -165,7 +165,7 @@ func (s Set[T]) SymmetricDifference(other Set[T]) Set[T] {
// Minus creates an set of whose element in origin set but not in compared set
func (s Set[T]) Minus(comparedSet Set[T]) Set[T] {
set := NewSet[T]()
set := New[T]()
s.Iterate(func(value T) {
if !comparedSet.Contain(value) {

View File

@@ -6,18 +6,18 @@ import (
"github.com/duke-git/lancet/v2/internal"
)
func TestSet_NewSetFromSlice(t *testing.T) {
func TestSet_FromSlice(t *testing.T) {
t.Parallel()
assert := internal.NewAssert(t, "TestSet_NewSetFromSlice")
assert := internal.NewAssert(t, "TestSet_FromSlice")
s1 := NewSetFromSlice([]int{1, 2, 2, 3})
s1 := FromSlice([]int{1, 2, 2, 3})
assert.Equal(3, s1.Size())
assert.Equal(true, s1.Contain(1))
assert.Equal(true, s1.Contain(2))
assert.Equal(true, s1.Contain(3))
s2 := NewSetFromSlice([]int{})
s2 := FromSlice([]int{})
assert.Equal(0, s2.Size())
}
@@ -26,10 +26,10 @@ func TestSet_Add(t *testing.T) {
assert := internal.NewAssert(t, "TestSet_Add")
set := NewSet[int]()
set := New[int]()
set.Add(1, 2, 3)
cmpSet := NewSet(1, 2, 3)
cmpSet := New(1, 2, 3)
assert.Equal(true, set.Equal(cmpSet))
}
@@ -39,12 +39,12 @@ func TestSet_AddIfNotExist(t *testing.T) {
assert := internal.NewAssert(t, "TestSet_AddIfNotExist")
set := NewSet[int]()
set := New[int]()
set.Add(1, 2, 3)
assert.Equal(false, set.AddIfNotExist(1))
assert.Equal(true, set.AddIfNotExist(4))
assert.Equal(NewSet(1, 2, 3, 4), set)
assert.Equal(New(1, 2, 3, 4), set)
}
func TestSet_AddIfNotExistBy(t *testing.T) {
@@ -52,7 +52,7 @@ func TestSet_AddIfNotExistBy(t *testing.T) {
assert := internal.NewAssert(t, "TestSet_AddIfNotExistBy")
set := NewSet[int]()
set := New[int]()
set.Add(1, 2)
ok := set.AddIfNotExistBy(3, func(val int) bool {
@@ -75,7 +75,7 @@ func TestSet_Contain(t *testing.T) {
assert := internal.NewAssert(t, "TestSet_Contain")
set := NewSet[int]()
set := New[int]()
set.Add(1, 2, 3)
assert.Equal(true, set.Contain(1))
@@ -87,9 +87,9 @@ func TestSet_ContainAll(t *testing.T) {
assert := internal.NewAssert(t, "TestSet_ContainAll")
set1 := NewSet(1, 2, 3)
set2 := NewSet(1, 2)
set3 := NewSet(1, 2, 3, 4)
set1 := New(1, 2, 3)
set2 := New(1, 2)
set3 := New(1, 2, 3, 4)
assert.Equal(true, set1.ContainAll(set2))
assert.Equal(false, set1.ContainAll(set3))
@@ -100,7 +100,7 @@ func TestSet_Clone(t *testing.T) {
assert := internal.NewAssert(t, "TestSet_Clone")
set1 := NewSet(1, 2, 3)
set1 := New(1, 2, 3)
set2 := set1.Clone()
assert.Equal(true, set1.Size() == set2.Size())
@@ -112,11 +112,11 @@ func TestSet_Delete(t *testing.T) {
assert := internal.NewAssert(t, "TestSet_Delete")
set := NewSet[int]()
set := New[int]()
set.Add(1, 2, 3)
set.Delete(3)
assert.Equal(true, set.Equal(NewSet(1, 2)))
assert.Equal(true, set.Equal(New(1, 2)))
}
func TestSet_Equal(t *testing.T) {
@@ -124,9 +124,9 @@ func TestSet_Equal(t *testing.T) {
assert := internal.NewAssert(t, "TestSet_Equal")
set1 := NewSet(1, 2, 3)
set2 := NewSet(1, 2, 3)
set3 := NewSet(1, 2, 3, 4)
set1 := New(1, 2, 3)
set2 := New(1, 2, 3)
set3 := New(1, 2, 3, 4)
assert.Equal(true, set1.Equal(set2))
assert.Equal(false, set1.Equal(set3))
@@ -137,7 +137,7 @@ func TestSet_Iterate(t *testing.T) {
assert := internal.NewAssert(t, "TestSet_Iterate")
set := NewSet(1, 2, 3)
set := New(1, 2, 3)
arr := []int{}
set.Iterate(func(value int) {
arr = append(arr, value)
@@ -151,7 +151,7 @@ func TestSet_IsEmpty(t *testing.T) {
assert := internal.NewAssert(t, "TestSet_IsEmpty")
set := NewSet[int]()
set := New[int]()
assert.Equal(true, set.IsEmpty())
}
@@ -160,7 +160,7 @@ func TestSet_Size(t *testing.T) {
assert := internal.NewAssert(t, "TestSet_Size")
set := NewSet(1, 2, 3)
set := New(1, 2, 3)
assert.Equal(3, set.Size())
}
@@ -169,7 +169,7 @@ func TestSet_Values(t *testing.T) {
assert := internal.NewAssert(t, "TestSet_Values")
set := NewSet(1, 2, 3)
set := New(1, 2, 3)
values := set.Values()
assert.Equal(3, len(values))
@@ -180,12 +180,12 @@ func TestSet_Union(t *testing.T) {
assert := internal.NewAssert(t, "TestSet_Union")
set1 := NewSet(1, 2, 3)
set2 := NewSet(2, 3, 4, 5)
set1 := New(1, 2, 3)
set2 := New(2, 3, 4, 5)
unionSet := set1.Union(set2)
assert.Equal(NewSet(1, 2, 3, 4, 5), unionSet)
assert.Equal(New(1, 2, 3, 4, 5), unionSet)
}
func TestSet_Intersection(t *testing.T) {
@@ -193,11 +193,11 @@ func TestSet_Intersection(t *testing.T) {
assert := internal.NewAssert(t, "TestSet_Intersection")
set1 := NewSet(1, 2, 3)
set2 := NewSet(2, 3, 4, 5)
set1 := New(1, 2, 3)
set2 := New(2, 3, 4, 5)
intersectionSet := set1.Intersection(set2)
assert.Equal(NewSet(2, 3), intersectionSet)
assert.Equal(New(2, 3), intersectionSet)
}
func TestSet_SymmetricDifference(t *testing.T) {
@@ -205,10 +205,10 @@ func TestSet_SymmetricDifference(t *testing.T) {
assert := internal.NewAssert(t, "TestSet_SymmetricDifference")
set1 := NewSet(1, 2, 3)
set2 := NewSet(2, 3, 4, 5)
set1 := New(1, 2, 3)
set2 := New(2, 3, 4, 5)
assert.Equal(NewSet(1, 4, 5), set1.SymmetricDifference(set2))
assert.Equal(New(1, 4, 5), set1.SymmetricDifference(set2))
}
func TestSet_Minus(t *testing.T) {
@@ -216,16 +216,16 @@ func TestSet_Minus(t *testing.T) {
assert := internal.NewAssert(t, "TestSet_Minus")
set1 := NewSet(1, 2, 3)
set2 := NewSet(2, 3, 4, 5)
set3 := NewSet(2, 3)
set1 := New(1, 2, 3)
set2 := New(2, 3, 4, 5)
set3 := New(2, 3)
assert.Equal(NewSet(1), set1.Minus(set2))
assert.Equal(NewSet(4, 5), set2.Minus(set3))
assert.Equal(New(1), set1.Minus(set2))
assert.Equal(New(4, 5), set2.Minus(set3))
}
func TestEachWithBreak(t *testing.T) {
// s := NewSet(1, 2, 3, 4, 5)
// s := New(1, 2, 3, 4, 5)
// var sum int
@@ -244,7 +244,7 @@ func TestEachWithBreak(t *testing.T) {
// func TestPop(t *testing.T) {
// assert := internal.NewAssert(t, "TestPop")
// s := NewSet[int]()
// s := New[int]()
// val, ok := s.Pop()
// assert.Equal(0, val)
@@ -254,7 +254,7 @@ func TestEachWithBreak(t *testing.T) {
// s.Add(2)
// s.Add(3)
// // s = NewSet(1, 2, 3, 4, 5)
// // s = New(1, 2, 3, 4, 5)
// val, ok = s.Pop()
// assert.Equal(3, val)

View File

@@ -26,6 +26,8 @@ import (
- [Remove](#Remove)
- [IndexOf](#IndexOf)
- [LastIndexOf](#LastIndexOf)
- [IndexOfFunc](#IndexOfFunc)
- [LastIndexOfFunc](#LastIndexOfFunc)
- [IsEmpty](#IsEmpty)
- [Contain](#Contain)
- [ValueOf](#ValueOf)
@@ -198,6 +200,58 @@ func main() {
```
### <span id="IndexOfFunc">IndexOfFunc</span>
<p>返回第一个满足判断函数f(v)的元素的索引,如果找不到则返回-1。</p>
<b>函数签名:</b>
```go
func (l *CopyOnWriteList[T]) IndexOfFunc(f func(T) bool) int
```
<b>示例:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
l := list.NewCopyOnWriteList([]int{1, 2, 3})
fmt.Println(l.IndexOfFunc(func(a int) bool { return a == 1 })) //0
fmt.Println(l.IndexOfFunc(func(a int) bool { return a == 0 })) //-1
}
```
### <span id="LastIndexOfFunc">LastIndexOfFunc</span>
<p>返回最后一个满足判断函数f(v)的元素的索引,如果找不到则返回-1。</p>
<b>函数签名:</b>
```go
func (l *CopyOnWriteList[T]) LastIndexOfFunc(f func(T) bool) int
```
<b>示例:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
l := list.NewCopyOnWriteList([]int{1, 2, 3, 1})
fmt.Println(l.LastIndexOfFunc(func(a int) bool { return a == 1 })) // 3
fmt.Println(l.LastIndexOfFunc(func(a int) bool { return a == 0 })) //-1
}
```
### IsEmpty
如果此列表不包含任何元素,则返回 true。

View File

@@ -22,17 +22,16 @@ import (
## 目录
- [Of](#Of)
- [OfNullable](#OfNullable)
- [Empty](#Empty)
- [IsPresent](#IsPresent)
- [IsEmpty](#IsEmpty)
- [IfPresent](#IfPresent)
- [IfPresentOrElse](#IfPresentOrElse)
- [Get](#Get)
- [FromNillable](#FromNillable)
- [Default](#Default)
- [IsNotNil](#IsNotNil)
- [IsNil](#IsNil)
- [IsNotNil](#IsNotNil)
- [IfNotNilOrElse](#IfNotNilOrElse)
- [Umwarp](#Umwarp)
- [OrElse](#OrElse)
- [OrElseGet](#OrElseGet)
- [OrElseThrow](#OrElseThrow)
- [OrElseTrigger](#OrElseTrigger)
<div STYLE="page-break-after: always;"></div>
@@ -68,14 +67,13 @@ func main() {
}
```
### <span id="OfNullable">OfNullable</span>
### <span id="FromNillable">FromNillable</span>
<p>返回一个包含给定值的Optional该值可能为空 (nil)。</p>
<b>函数签名:</b>
```go
func OfNullable[T any](value *T) Optional[T]
func FromNillable[T any](value *T) Optional[T]
```
<b>示例:</b>
@@ -89,15 +87,15 @@ import (
func main() {
var value *int = nil
opt := optional.OfNullable(value)
opt := optional.FromNillable(value)
fmt.Println(opt.IsPresent())
fmt.Println(opt.IsNotNil())
value = new(int)
*value = 42
opt = optional.OfNullable(value)
opt = optional.FromNillable(value)
fmt.Println(opt.IsPresent())
fmt.Println(opt.IsNotNil())
// Output:
@@ -107,14 +105,13 @@ func main() {
```
### <span id="Empty">Empty</span>
### <span id="Default">Default</span>
<p>返回一个空Optional实例。</p>
<b>函数签名:</b>
```go
func Empty[T any]() Optional[T]
func Default[T any]() Optional[T]
```
<b>示例:</b>
@@ -127,8 +124,8 @@ import (
)
func main() {
optEmpty := OfNullable.Empty[int]()
fmt.Println(optEmpty.IsEmpty())
optDefault := optional.Default[int]()
fmt.Println(optDefault.IsNil())
// Output:
// true
@@ -136,14 +133,13 @@ func main() {
```
### <span id="IsEmpty">IsEmpty</span>
### <span id="IsNil">IsNil</span>
<p>验证Optional是否为空。</p>
<b>函数签名:</b>
```go
func (o Optional[T]) IsEmpty() bool
func (o Optional[T]) IsNil() bool
```
<b>示例:</b>
@@ -156,23 +152,21 @@ import (
)
func main() {
optEmpty := OfNullable.Empty[int]()
fmt.Println(optEmpty.IsEmpty())
optDefault := optional.Default[int]()
fmt.Println(optDefault.IsNil())
// Output:
// true
}
```
### <span id="IsPresent">IsPresent</span>
### <span id="IsNotNil">IsNotNil</span>
<p>检查当前Optional内是否存在值。</p>
<b>函数签名:</b>
```go
func (o Optional[T]) IsPresent() bool
func (o Optional[T]) IsNotNil() bool
```
<b>示例:</b>
@@ -181,20 +175,20 @@ package main
import (
"fmt"
optional "github.com/duke-git/lancet/v2/datastructure/optional"
"github.com/duke-git/lancet/v2/datastructure/optional"
)
func main() {
var value *int = nil
opt := optional.OfNullable(value)
opt := optional.FromNillable(value)
fmt.Println(opt.IsPresent())
fmt.Println(opt.IsNotNil())
value = new(int)
*value = 42
opt = optional.OfNullable(value)
opt = optional.FromNillable(value)
fmt.Println(opt.IsPresent())
fmt.Println(opt.IsNotNil())
// Output:
@@ -203,15 +197,13 @@ func main() {
}
```
### <span id="IfPresent">IfPresent</span>
### <span id="IfNotNil">IfNotNil</span>
<p>如果值存在则使用action方法执行给定的操作。</p>
<b>函数签名:</b>
```go
func (o Optional[T]) IfPresent(action func(value T))
func (o Optional[T]) IfNotNil(action func(value T))
```
<b>示例:</b>
@@ -220,23 +212,23 @@ package main
import (
"fmt"
optional "github.com/duke-git/lancet/v2/datastructure/optional"
"github.com/duke-git/lancet/v2/datastructure/optional"
)
func main() {
called := false
action := func(value int) { called = true }
optEmpty := optional.Empty[int]()
optEmpty.IfPresent(action)
optDefault := optional.Default[int]()
optDefault.IfNotNil(action)
fmt.Println(called)
called = false // Reset for next test
optWithValue := optional.Of(42)
optWithValue.IfPresent(action)
optWithValue.IfNotNil(action)
fmt.Println(optWithValue.IsPresent())
fmt.Println(optWithValue.IsNotNil())
// Output:
// false
@@ -245,14 +237,13 @@ func main() {
```
### <span id="IfPresentOrElse">IfPresentOrElse</span>
### <span id="IfNotNilOrElse">IfNotNilOrElse</span>
<p>根据是否存在值执行相应的操作:有值则执行指定操作,没有值则执行默认操作。</p>
<b>函数签名:</b>
```go
func (o Optional[T]) IfPresentOrElse(action func(value T), emptyAction func())
func (o Optional[T]) IfNotNilOrElse(action func(value T), fallbackAction func())
```
<b>示例:</b>
@@ -270,7 +261,7 @@ func main() {
emptyAction := func() { t.Errorf("Empty action should not be called when value is present") }
optWithValue := optional.Of(42)
optWithValue.IfPresentOrElse(valueAction, emptyAction)
optWithValue.IfNotNilOrElse(valueAction, emptyAction)
fmt.Println(calledWithValue)
@@ -278,8 +269,8 @@ func main() {
valueAction = func(value int) { t.Errorf("Value action should not be called when value is not present") }
emptyAction = func() { calledWithEmpty = true }
optEmpty := optional.Empty[int]()
optEmpty.IfPresentOrElse(valueAction, emptyAction)
optDefault := optional.Default[int]()
optDefault.IfNotNilOrElse(valueAction, emptyAction)
fmt.Println(calledWithEmpty)
@@ -289,13 +280,13 @@ func main() {
}
```
### <span id="Get">Get</span>
### <span id="Unwrap">Unwrap</span>
<p>如果存在返回该值否则引发panic。</p>
<b>函数签名:</b>
```go
func (o Optional[T]) Get() T
func (o Optional[T]) Unwrap() T
```
<b>示例:</b>
@@ -311,7 +302,7 @@ func main() {
value := 42
opt := optional.Of(value)
fmt.Println(opt.Get())
fmt.Println(opt.Unwrap())
// Output:
// 42
@@ -338,8 +329,8 @@ import (
)
func main() {
optEmpty := optional.Empty[int]()
val := optEmpty.OrElse(100)
optDefault := optional.Empty[int]()
val := optDefault.OrElse(100)
fmt.Println(val)
optWithValue := optional.Of(42)
@@ -359,7 +350,7 @@ func main() {
<b>函数签名:</b>
```go
func (o Optional[T]) OrElseGet(supplier func() T) T
func (o Optional[T]) OrElseGet(action func() T) T
```
<b>示例:</b>
@@ -372,10 +363,10 @@ import (
)
func main() {
optEmpty := optional.Empty[int]()
supplier := func() int { return 100 }
optDefault := optional.Default[int]()
action := func() int { return 100 }
val := optEmpty.OrElseGet(supplier)
val := optDefault.OrElseGet(action)
fmt.Println(val)
// Output:
@@ -383,14 +374,13 @@ func main() {
}
```
### <span id="OrElseThrow">OrElseThrow</span>
### <span id="OrElseTrigger">OrElseTrigger</span>
<p>检查Optional值是否存在如果存在则直接返回该值否则返回错误。</p>
<b>函数签名:</b>
```go
func (o Optional[T]) OrElseThrow(errorSupplier func() error) (T, error)
OrElseTrigger(errorHandler func() error) (T, error)
```
<b>示例:</b>
@@ -403,13 +393,13 @@ import (
)
func main() {
optEmpty := optional.Empty[int]()
_, err := optEmpty.OrElseThrow(func() error { return errors.New("no value") })
optDefault := optional.Default[int]()
_, err := optDefault.OrElseTrigger(func() error { return errors.New("no value") })
fmt.Println(err.Error())
optWithValue := optional.Of(42)
val, err := optWithValue.OrElseThrow(func() error { return errors.New("no value") })
val, err := optWithValue.OrElseTrigger(func() error { return errors.New("no value") })
fmt.Println(val)
fmt.Println(err)

View File

@@ -1,6 +1,6 @@
# Set
Set 集合数据结构类似列表。Set 中元素不重复。
集合数据结构类似列表。Set中元素不重复。
<div STYLE="page-break-after: always;"></div>
@@ -22,8 +22,8 @@ import (
## 目录
- [NewSet](#NewSet)
- [NewSetFromSlice](#NewSetFromSlice)
- [New](#New)
- [FromSlice](#FromSlice)
- [Values](#Values)
- [Add](#Add)
- [AddIfNotExist](#AddIfNotExist)
@@ -45,7 +45,7 @@ import (
## 文档
### <span id="NewSet">NewSet</span>
### <span id="New">New</span>
<p>返回Set结构体对象</p>
@@ -53,7 +53,7 @@ import (
```go
type Set[T comparable] map[T]bool
func NewSet[T comparable](items ...T) Set[T]
func New[T comparable](items ...T) Set[T]
```
<b>示例:</b>
@@ -67,19 +67,19 @@ import (
)
func main() {
st := set.NewSet[int](1,2,2,3)
st := set.New[int](1,2,2,3)
fmt.Println(st.Values()) //1,2,3
}
```
### <span id="NewSetFromSlice">NewSetFromSlice</span>
### <span id="FromSlice">FromSlice</span>
<p>基于切片创建集合</p>
<b>函数签名:</b>
```go
func NewSetFromSlice[T comparable](items []T) Set[T]
func FromSlice[T comparable](items []T) Set[T]
```
<b>示例:</b>
@@ -93,7 +93,7 @@ import (
)
func main() {
st := set.NewSetFromSlice([]int{1, 2, 2, 3})
st := set.FromSlice([]int{1, 2, 2, 3})
fmt.Println(st.Values()) //1,2,3
}
```
@@ -119,7 +119,7 @@ import (
)
func main() {
st := set.NewSet[int](1,2,2,3)
st := set.New[int](1,2,2,3)
fmt.Println(st.Values()) //1,2,3
}
```
@@ -145,7 +145,7 @@ import (
)
func main() {
st := set.NewSet[int]()
st := set.New[int]()
st.Add(1, 2, 3)
fmt.Println(st.Values()) //1,2,3
@@ -173,7 +173,7 @@ import (
)
func main() {
st := set.NewSet[int]()
st := set.New[int]()
st.Add(1, 2, 3)
r1 := st.AddIfNotExist(1)
@@ -206,7 +206,7 @@ import (
)
func main() {
st := set.NewSet[int]()
st := set.New[int]()
st.Add(1, 2)
ok := st.AddIfNotExistBy(3, func(val int) bool {
@@ -245,7 +245,7 @@ import (
)
func main() {
st := set.NewSet[int]()
st := set.New[int]()
st.Add(1, 2, 3)
set.Delete(3)
@@ -274,7 +274,7 @@ import (
)
func main() {
st := set.NewSet[int]()
st := set.New[int]()
st.Add(1, 2, 3)
fmt.Println(st.Contain(1)) //true
@@ -303,9 +303,9 @@ import (
)
func main() {
set1 := set.NewSet(1, 2, 3)
set2 := set.NewSet(1, 2)
set3 := set.NewSet(1, 2, 3, 4)
set1 := set.New(1, 2, 3)
set2 := set.New(1, 2)
set3 := set.New(1, 2, 3, 4)
fmt.Println(set1.ContainAll(set2)) //true
fmt.Println(set1.ContainAll(set3)) //false
@@ -333,7 +333,7 @@ import (
)
func main() {
set1 := set.NewSet(1, 2, 3)
set1 := set.New(1, 2, 3)
fmt.Println(set1.Size()) //3
}
@@ -360,7 +360,7 @@ import (
)
func main() {
set1 := set.NewSet(1, 2, 3)
set1 := set.New(1, 2, 3)
set2 := set1.Clone()
fmt.Println(set1.Size() == set2.Size()) //true
@@ -389,9 +389,9 @@ import (
)
func main() {
set1 := set.NewSet(1, 2, 3)
set2 := set.NewSet(1, 2, 3)
set3 := set.NewSet(1, 2, 3, 4)
set1 := set.New(1, 2, 3)
set2 := set.New(1, 2, 3)
set3 := set.New(1, 2, 3, 4)
fmt.Println(set1.Equal(set2)) //true
fmt.Println(set1.Equal(set3)) //false
@@ -419,7 +419,7 @@ import (
)
func main() {
set1 := set.NewSet(1, 2, 3)
set1 := set.New(1, 2, 3)
arr := []int{}
set.Iterate(func(item int) {
arr = append(arr, item)
@@ -450,7 +450,7 @@ import (
)
func main() {
s := set.NewSet(1, 2, 3, 4, 5)
s := set.New(1, 2, 3, 4, 5)
var sum int
@@ -487,8 +487,8 @@ import (
)
func main() {
set1 := set.NewSet(1, 2, 3)
set2 := set.NewSet()
set1 := set.New(1, 2, 3)
set2 := set.New()
fmt.Println(set1.IsEmpty()) //false
fmt.Println(set2.IsEmpty()) //true
@@ -516,8 +516,8 @@ import (
)
func main() {
set1 := set.NewSet(1, 2, 3)
set2 := set.NewSet(2, 3, 4, 5)
set1 := set.New(1, 2, 3)
set2 := set.New(2, 3, 4, 5)
set3 := set1.Union(set2)
fmt.Println(set3.Values()) //1,2,3,4,5
@@ -545,8 +545,8 @@ import (
)
func main() {
set1 := set.NewSet(1, 2, 3)
set2 := set.NewSet(2, 3, 4, 5)
set1 := set.New(1, 2, 3)
set2 := set.New(2, 3, 4, 5)
set3 := set1.Intersection(set2)
fmt.Println(set3.Values()) //2,3
@@ -574,8 +574,8 @@ import (
)
func main() {
set1 := set.NewSet(1, 2, 3)
set2 := set.NewSet(2, 3, 4, 5)
set1 := set.New(1, 2, 3)
set2 := set.New(2, 3, 4, 5)
set3 := set1.SymmetricDifference(set2)
fmt.Println(set3.Values()) //1,4,5
@@ -603,9 +603,9 @@ import (
)
func main() {
set1 := set.NewSet(1, 2, 3)
set2 := set.NewSet(2, 3, 4, 5)
set3 := set.NewSet(2, 3)
set1 := set.New(1, 2, 3)
set2 := set.New(2, 3, 4, 5)
set3 := set.New(2, 3)
res1 := set1.Minus(set2)
fmt.Println(res1.Values()) //1
@@ -636,7 +636,7 @@ import (
)
func main() {
s := set.NewSet[int]()
s := set.New[int]()
s.Add(1)
s.Add(2)
s.Add(3)

View File

@@ -26,6 +26,8 @@ import (
- [Remove](#Remove)
- [IndexOf](#IndexOf)
- [LastIndexOf](#LastIndexOf)
- [IndexOfFunc](#IndexOfFunc)
- [LastIndexOfFunc](#LastIndexOfFunc)
- [IsEmpty](#IsEmpty)
- [Contain](#Contain)
- [ValueOf](#ValueOf)
@@ -197,6 +199,59 @@ func main() {
```
### <span id="IndexOfFunc">IndexOfFunc</span>
<p> IndexOfFunc returns the first index satisfying the functional predicate f(v) bool. if not found return -1.</p>
<b>Signature:</b>
```go
func (l *CopyOnWriteList[T]) IndexOfFunc(f func(T) bool) int
```
<b>Example:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
l := list.NewCopyOnWriteList([]int{1, 2, 3})
fmt.Println(l.IndexOfFunc(func(a int) bool { return a == 1 })) //0
fmt.Println(l.IndexOfFunc(func(a int) bool { return a == 0 })) //-1
}
```
### <span id="LastIndexOfFunc">LastIndexOfFunc</span>
<p>LastIndexOfFunc returns the index of the last occurrence of the value in this list satisfying the functional predicate f(T) bool. if not found return -1.</p>
<b>Signature:</b>
```go
func (l *CopyOnWriteList[T]) LastIndexOfFunc(f func(T) bool) int
```
<b>Example:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
l := list.NewCopyOnWriteList([]int{1, 2, 3, 1})
fmt.Println(l.LastIndexOfFunc(func(a int) bool { return a == 1 })) // 3
fmt.Println(l.LastIndexOfFunc(func(a int) bool { return a == 0 })) //-1
}
```
### IsEmpty
Returns true if this list does not contain any elements.

View File

@@ -22,16 +22,16 @@ import (
## Index
- [Of](#Of)
- [OfNullable](#OfNullable)
- [Empty](#Empty)
- [IsPresent](#IsPresent)
- [IsEmpty](#IsEmpty)
- [IfPresent](#IfPresent)
- [IfPresentOrElse](#IfPresentOrElse)
- [Get](#Get)
- [FromNillable](#FromNillable)
- [Default](#Default)
- [IsNotNil](#IsNotNil)
- [IsNil](#IsNil)
- [IsNotNil](#IsNotNil)
- [IfNotNilOrElse](#IfNotNilOrElse)
- [Umwarp](#Umwarp)
- [OrElse](#OrElse)
- [OrElseGet](#OrElseGet)
- [OrElseThrow](#OrElseThrow)
- [OrElseTrigger](#OrElseTrigger)
@@ -68,13 +68,13 @@ func main() {
}
```
### <span id="OfNullable">OfNullable</span>
### <span id="FromNillable">FromNillable</span>
<p>Returns an Optional for a given value, which may be nil.</p>
<b>Signature:</b>
```go
func OfNullable[T any](value *T) Optional[T]
func FromNillable[T any](value *T) Optional[T]
```
<b>Example:</b>
@@ -88,15 +88,15 @@ import (
func main() {
var value *int = nil
opt := optional.OfNullable(value)
opt := optional.FromNillable(value)
fmt.Println(opt.IsPresent())
fmt.Println(opt.IsNotNil())
value = new(int)
*value = 42
opt = optional.OfNullable(value)
opt = optional.FromNillable(value)
fmt.Println(opt.IsPresent())
fmt.Println(opt.IsNotNil())
// Output:
@@ -106,13 +106,13 @@ func main() {
```
### <span id="Empty">Empty</span>
<p>Returns an empty Optional instance.</p>
### <span id="Default">Default</span>
<p>Returns an default Optional instance.</p>
<b>Signature:</b>
```go
func Empty[T any]() Optional[T]
func Default[T any]() Optional[T]
```
<b>Example:</b>
@@ -125,8 +125,8 @@ import (
)
func main() {
optEmpty := OfNullable.Empty[int]()
fmt.Println(optEmpty.IsEmpty())
optDefault := optional.Default[int]()
fmt.Println(optDefault.IsNil())
// Output:
// true
@@ -134,13 +134,13 @@ func main() {
```
### <span id="IsEmpty">IsEmpty</span>
<p>Checks if the Optional is empty.</p>
### <span id="IsNil">IsNil</span>
<p>Checks if the Optional is nil.</p>
<b>Signature:</b>
```go
func (o Optional[T]) IsEmpty() bool
func (o Optional[T]) IsNil() bool
```
<b>Example:</b>
@@ -153,8 +153,8 @@ import (
)
func main() {
optEmpty := OfNullable.Empty[int]()
fmt.Println(optEmpty.IsEmpty())
optDefault := optional.Default[int]()
fmt.Println(optDefault.IsNil())
// Output:
// true
@@ -162,13 +162,13 @@ func main() {
```
### <span id="IsPresent">IsPresent</span>
<p>Checks if there is a value present.</p>
### <span id="IsNotNil">IsNotNil</span>
<p>Checks if there is a value not nil.</p>
<b>Signature:</b>
```go
func (o Optional[T]) IsPresent() bool
func (o Optional[T]) IsNotNil() bool
```
<b>Example:</b>
@@ -182,15 +182,15 @@ import (
func main() {
var value *int = nil
opt := optional.OfNullable(value)
opt := optional.FromNillable(value)
fmt.Println(opt.IsPresent())
fmt.Println(opt.IsNotNil())
value = new(int)
*value = 42
opt = optional.OfNullable(value)
opt = optional.FromNillable(value)
fmt.Println(opt.IsPresent())
fmt.Println(opt.IsNotNil())
// Output:
@@ -200,13 +200,13 @@ func main() {
```
### <span id="IfPresent">IfPresent</span>
### <span id="IfNotNil">IfNotNil</span>
<p>Performs the given action with the value if a value is present.</p>
<b>Signature:</b>
```go
func (o Optional[T]) IfPresent(action func(value T))
func (o Optional[T]) IfNotNil(action func(value T))
```
<b>Example:</b>
@@ -222,16 +222,16 @@ func main() {
called := false
action := func(value int) { called = true }
optEmpty := optional.Empty[int]()
optEmpty.IfPresent(action)
optDefault := optional.Default[int]()
optDefault.IfNotNil(action)
fmt.Println(called)
called = false // Reset for next test
optWithValue := optional.Of(42)
optWithValue.IfPresent(action)
optWithValue.IfNotNil(action)
fmt.Println(optWithValue.IsPresent())
fmt.Println(optWithValue.IsNotNil())
// Output:
// false
@@ -240,13 +240,13 @@ func main() {
```
### <span id="IfPresentOrElse">IfPresentOrElse</span>
<p>Performs the action with the value if present, otherwise performs the empty-based action.</p>
### <span id="IfNotNilOrElse">IfNotNilOrElse</span>
<p>Performs the action with the value if not nil, otherwise performs the fallback action.</p>
<b>Signature:</b>
```go
func (o Optional[T]) IfPresentOrElse(action func(value T), emptyAction func())
func (o Optional[T]) IfNotNilOrElse(action func(value T), fallbackAction func())
```
<b>Example:</b>
@@ -264,7 +264,7 @@ func main() {
emptyAction := func() { t.Errorf("Empty action should not be called when value is present") }
optWithValue := optional.Of(42)
optWithValue.IfPresentOrElse(valueAction, emptyAction)
optWithValue.IfNotNilOrElse(valueAction, emptyAction)
fmt.Println(calledWithValue)
@@ -272,8 +272,8 @@ func main() {
valueAction = func(value int) { t.Errorf("Value action should not be called when value is not present") }
emptyAction = func() { calledWithEmpty = true }
optEmpty := optional.Empty[int]()
optEmpty.IfPresentOrElse(valueAction, emptyAction)
optDefault := optional.Default[int]()
optDefault.IfNotNilOrElse(valueAction, emptyAction)
fmt.Println(calledWithEmpty)
@@ -283,13 +283,13 @@ func main() {
}
```
### <span id="Get">Get</span>
<p>Returns the value if present, otherwise panics.</p>
### <span id="Unwrap">Unwrap</span>
<p>Returns the value if not nil, otherwise panics.</p>
<b>Signature:</b>
```go
func (o Optional[T]) Get() T
func (o Optional[T]) Unwrap() T
```
<b>Example:</b>
@@ -305,7 +305,7 @@ func main() {
value := 42
opt := optional.Of(value)
fmt.Println(opt.Get())
fmt.Println(opt.Unwrap())
// Output:
// 42
@@ -314,7 +314,7 @@ func main() {
### <span id="OrElse">OrElse</span>
<p>Returns the value if present, otherwise returns other.</p>
<p>Returns the value if not nill, otherwise returns other.</p>
<b>Signature:</b>
@@ -332,8 +332,8 @@ import (
)
func main() {
optEmpty := optional.Empty[int]()
val := optEmpty.OrElse(100)
optDefault := optional.Default[int]()
val := optDefault.OrElse(100)
fmt.Println(val)
optWithValue := optional.Of(42)
@@ -348,12 +348,12 @@ func main() {
### <span id="OrElseGet">OrElseGet</span>
<p>Returns the value if present, otherwise invokes supplier and returns the result.</p>
<p>Returns the value if not nil, otherwise invokes action and returns the result.</p>
<b>Signature:</b>
```go
func (o Optional[T]) OrElseGet(supplier func() T) T
func (o Optional[T]) OrElseGet(action func() T) T
```
<b>Example:</b>
@@ -366,10 +366,10 @@ import (
)
func main() {
optEmpty := optional.Empty[int]()
supplier := func() int { return 100 }
optDefault := optional.Default[int]()
action := func() int { return 100 }
val := optEmpty.OrElseGet(supplier)
val := optDefault.OrElseGet(action)
fmt.Println(val)
// Output:
@@ -378,13 +378,13 @@ func main() {
```
### <span id="OrElseThrow">OrElseThrow</span>
### <span id="OrElseTrigger">OrElseTrigger</span>
<p>Returns the value if present, otherwise returns an error.</p>
<b>Signature:</b>
```go
func (o Optional[T]) OrElseThrow(errorSupplier func() error) (T, error)
OrElseTrigger(errorHandler func() error) (T, error)
```
<b>Example:</b>
@@ -397,13 +397,13 @@ import (
)
func main() {
optEmpty := optional.Empty[int]()
_, err := optEmpty.OrElseThrow(func() error { return errors.New("no value") })
optDefault := optional.Default[int]()
_, err := optDefault.OrElseTrigger(func() error { return errors.New("no value") })
fmt.Println(err.Error())
optWithValue := optional.Of(42)
val, err := optWithValue.OrElseThrow(func() error { return errors.New("no value") })
val, err := optWithValue.OrElseTrigger(func() error { return errors.New("no value") })
fmt.Println(val)
fmt.Println(err)

View File

@@ -22,8 +22,8 @@ import (
## Index
- [NewSet](#NewSet)
- [NewSetFromSlice](#NewSetFromSlice)
- [New](#New)
- [FromSlice](#FromSlice)
- [Values](#Values)
- [Add](#Add)
- [AddIfNotExist](#AddIfNotExist)
@@ -46,7 +46,7 @@ import (
## Documentation
### <span id="NewSet">NewSet</span>
### <span id="New">New</span>
<p>Create a set instance</p>
@@ -54,7 +54,7 @@ import (
```go
type Set[T comparable] map[T]bool
func NewSet[T comparable](items ...T) Set[T]
func New[T comparable](items ...T) Set[T]
```
<b>Example:</b>
@@ -68,19 +68,19 @@ import (
)
func main() {
st := set.NewSet[int](1,2,2,3)
st := set.New[int](1,2,2,3)
fmt.Println(st.Values()) //1,2,3
}
```
### <span id="NewSetFromSlice">NewSetFromSlice</span>
### <span id="FromSlice">FromSlice</span>
<p>Create a set from slice</p>
<b>Signature:</b>
```go
func NewSetFromSlice[T comparable](items []T) Set[T]
func FromSlice[T comparable](items []T) Set[T]
```
<b>Example:</b>
@@ -94,7 +94,7 @@ import (
)
func main() {
st := set.NewSetFromSlice([]int{1, 2, 2, 3})
st := set.FromSlice([]int{1, 2, 2, 3})
fmt.Println(st.Values()) //1,2,3
}
```
@@ -120,7 +120,7 @@ import (
)
func main() {
st := set.NewSet[int](1,2,2,3)
st := set.New[int](1,2,2,3)
fmt.Println(st.Values()) //1,2,3
}
```
@@ -146,7 +146,7 @@ import (
)
func main() {
st := set.NewSet[int]()
st := set.New[int]()
st.Add(1, 2, 3)
fmt.Println(st.Values()) //1,2,3
@@ -174,7 +174,7 @@ import (
)
func main() {
st := set.NewSet[int]()
st := set.New[int]()
st.Add(1, 2, 3)
r1 := st.AddIfNotExist(1)
@@ -207,7 +207,7 @@ import (
)
func main() {
st := set.NewSet[int]()
st := set.New[int]()
st.Add(1, 2)
ok := st.AddIfNotExistBy(3, func(val int) bool {
@@ -246,7 +246,7 @@ import (
)
func main() {
st := set.NewSet[int]()
st := set.New[int]()
st.Add(1, 2, 3)
set.Delete(3)
@@ -275,7 +275,7 @@ import (
)
func main() {
st := set.NewSet[int]()
st := set.New[int]()
st.Add(1, 2, 3)
fmt.Println(st.Contain(1)) //true
@@ -304,9 +304,9 @@ import (
)
func main() {
set1 := set.NewSet(1, 2, 3)
set2 := set.NewSet(1, 2)
set3 := set.NewSet(1, 2, 3, 4)
set1 := set.New(1, 2, 3)
set2 := set.New(1, 2)
set3 := set.New(1, 2, 3, 4)
fmt.Println(set1.ContainAll(set2)) //true
fmt.Println(set1.ContainAll(set3)) //false
@@ -334,7 +334,7 @@ import (
)
func main() {
set1 := set.NewSet(1, 2, 3)
set1 := set.New(1, 2, 3)
fmt.Println(set1.Size()) //3
}
@@ -361,7 +361,7 @@ import (
)
func main() {
set1 := set.NewSet(1, 2, 3)
set1 := set.New(1, 2, 3)
set2 := set1.Clone()
fmt.Println(set1.Size() == set2.Size()) //true
@@ -390,9 +390,9 @@ import (
)
func main() {
set1 := set.NewSet(1, 2, 3)
set2 := set.NewSet(1, 2, 3)
set3 := set.NewSet(1, 2, 3, 4)
set1 := set.New(1, 2, 3)
set2 := set.New(1, 2, 3)
set3 := set.New(1, 2, 3, 4)
fmt.Println(set1.Equal(set2)) //true
fmt.Println(set1.Equal(set3)) //false
@@ -420,7 +420,7 @@ import (
)
func main() {
set1 := set.NewSet(1, 2, 3)
set1 := set.New(1, 2, 3)
arr := []int{}
set.Iterate(func(item int) {
arr = append(arr, item)
@@ -451,7 +451,7 @@ import (
)
func main() {
s := set.NewSet(1, 2, 3, 4, 5)
s := set.New(1, 2, 3, 4, 5)
var sum int
@@ -488,8 +488,8 @@ import (
)
func main() {
set1 := set.NewSet(1, 2, 3)
set2 := set.NewSet()
set1 := set.New(1, 2, 3)
set2 := set.New()
fmt.Println(set1.IsEmpty()) //false
fmt.Println(set2.IsEmpty()) //true
@@ -517,8 +517,8 @@ import (
)
func main() {
set1 := set.NewSet(1, 2, 3)
set2 := set.NewSet(2, 3, 4, 5)
set1 := set.New(1, 2, 3)
set2 := set.New(2, 3, 4, 5)
set3 := set1.Union(set2)
fmt.Println(set3.Values()) //1,2,3,4,5
@@ -546,8 +546,8 @@ import (
)
func main() {
set1 := set.NewSet(1, 2, 3)
set2 := set.NewSet(2, 3, 4, 5)
set1 := set.New(1, 2, 3)
set2 := set.New(2, 3, 4, 5)
set3 := set1.Intersection(set2)
fmt.Println(set3.Values()) //2,3
@@ -575,8 +575,8 @@ import (
)
func main() {
set1 := set.NewSet(1, 2, 3)
set2 := set.NewSet(2, 3, 4, 5)
set1 := set.New(1, 2, 3)
set2 := set.New(2, 3, 4, 5)
set3 := set1.SymmetricDifference(set2)
fmt.Println(set3.Values()) //1,4,5
@@ -604,9 +604,9 @@ import (
)
func main() {
set1 := set.NewSet(1, 2, 3)
set2 := set.NewSet(2, 3, 4, 5)
set3 := set.NewSet(2, 3)
set1 := set.New(1, 2, 3)
set2 := set.New(2, 3, 4, 5)
set3 := set.New(2, 3)
res1 := set1.Minus(set2)
fmt.Println(res1.Values()) //1
@@ -637,7 +637,7 @@ import (
)
func main() {
s := set.NewSet[int]()
s := set.New[int]()
s.Add(1)
s.Add(2)
s.Add(3)