Skip to content

Commit

Permalink
chore: optimize tuple 'checkItem' return values (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
jjeffcaii authored Dec 8, 2020
1 parent d9a20bd commit 4fb36b5
Showing 1 changed file with 12 additions and 16 deletions.
28 changes: 12 additions & 16 deletions tuple/tuple.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,49 +49,46 @@ type tuple struct {
inner []*reactor.Item
}

func (t tuple) checkItem(item *reactor.Item) *reactor.Item {
func (t tuple) checkItem(item *reactor.Item) (reactor.Any, error) {
if item != nil {
return item
return item.V, item.E
}
return &reactor.Item{}
return nil, nil
}
func (t tuple) First() (reactor.Any, error) {
if len(t.inner) < 1 {
return nil, errIndexOutOfBounds
}
next := t.checkItem(t.inner[0])
return next.V, next.E
return t.checkItem(t.inner[0])
}

func (t tuple) Second() (reactor.Any, error) {
if len(t.inner) < 2 {
return nil, errIndexOutOfBounds
}
next := t.checkItem(t.inner[1])
return next.V, next.E
return t.checkItem(t.inner[1])
}

func (t tuple) Last() (reactor.Any, error) {
if len(t.inner) < 1 {
return nil, errIndexOutOfBounds
}
next := t.checkItem(t.inner[len(t.inner)-1])
return next.V, next.E
return t.checkItem(t.inner[len(t.inner)-1])
}

func (t tuple) ForEachWithIndex(callback func(v reactor.Any, e error, index int) (ok bool)) {
for i := 0; i < len(t.inner); i++ {
var next = t.checkItem(t.inner[i])
if !callback(next.V, next.E, i) {
value, err := t.checkItem(t.inner[i])
if !callback(value, err, i) {
break
}
}
}

func (t tuple) ForEach(f func(any reactor.Any, err error) bool) {
func (t tuple) ForEach(f func(v reactor.Any, e error) bool) {
for _, next := range t.inner {
var tmp = t.checkItem(next)
if !f(tmp.V, tmp.E) {
value, err := t.checkItem(next)
if !f(value, err) {
break
}
}
Expand All @@ -105,6 +102,5 @@ func (t tuple) Get(index int) (reactor.Any, error) {
if index < 0 || index >= len(t.inner) {
return nil, errIndexOutOfBounds
}
item := t.checkItem(t.inner[index])
return item.V, item.E
return t.checkItem(t.inner[index])
}

0 comments on commit 4fb36b5

Please sign in to comment.