Skip to content

Commit

Permalink
Adding predicated Single function Singlep
Browse files Browse the repository at this point in the history
  • Loading branch information
marstr committed Sep 27, 2017
1 parent baa7b57 commit 64ef442
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ var (
errMultipleElements = errors.New("Enumerator encountered multiple elements")
)

// IsErrorNoElements determines whethr or not the given error is the result of no values being
// returned when one or more were expected.
func IsErrorNoElements(err error) bool {
return err == errNoElements
}

// IsErrorMultipleElements determines whether or not the given error is the result of multiple values
// being returned when one or zero were expected.
func IsErrorMultipleElements(err error) bool {
return err == errMultipleElements
}

// Identity is a trivial Transform which applies no operation on the value.
var Identity Transform = func(value interface{}) interface{} {
return value
Expand Down Expand Up @@ -445,6 +457,14 @@ func Single(iter Enumerable) (retval interface{}, err error) {
return
}

// Singlep retrieces the only element from a list that matches a criteria. If
// no match is found, or two or more are found, `Singlep` returns nil and an
// error.
func Singlep(iter Enumerable, pred Predicate) (retval interface{}, err error) {
iter = Where(iter, pred)
return Single(iter)
}

type skipper struct {
original Enumerable
skipCount uint
Expand Down

0 comments on commit 64ef442

Please sign in to comment.