Skip to content

Commit

Permalink
Merge pull request #6 from geniussportsgroup/develop
Browse files Browse the repository at this point in the history
More compliance
  • Loading branch information
lrleon committed Apr 13, 2021
2 parents da6e480 + f2b54a7 commit d466986
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 46 deletions.
83 changes: 43 additions & 40 deletions functional.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,20 @@ type Sequence interface {
Swap(other interface{}) interface{}
IsEmpty() bool
CreateIterator() interface{}
Create(items ...interface{}) interface{}
}

// A pair of interfaces. Returned by Zip
// Pair A pair of interfaces. Returned by Zip
type Pair struct {
Item1, Item2 interface{}
}

// Represent a tuple
// Tuple Represent a tuple
type Tuple struct {
l *[]interface{}
}

// Return a new tuple with the received elements
// NewTuple Return a new tuple with the received elements
func NewTuple(items ...interface{}) *Tuple {

s := make([]interface{}, 0, len(items))
Expand All @@ -42,7 +43,11 @@ func NewTuple(items ...interface{}) *Tuple {
return &tuple
}

// Build a tuple for storing n elements
func (tuple *Tuple) Create(items ...interface{}) interface{} {
return NewTuple(items...)
}

// BuildTuple Build a tuple for storing n elements
func BuildTuple(n int) *Tuple {
s := make([]interface{}, n, n)
return &Tuple{l: &s}
Expand Down Expand Up @@ -73,7 +78,7 @@ func (tuple *Tuple) Append(item interface{}, items ...interface{}) interface{} {
return tuple
}

// Return the length of the tuple
// Size Return the length of the tuple
func (tuple *Tuple) Size() int {
return len(*tuple.l)
}
Expand All @@ -85,7 +90,7 @@ func (tuple *Tuple) Swap(other interface{}) interface{} {
return tuple
}

// Return true if the tuple is empty
// IsEmpty Return true if the tuple is empty
func (tuple *Tuple) IsEmpty() bool {
return tuple.Size() == 0
}
Expand All @@ -95,47 +100,47 @@ type TupleIterator struct {
pos int
}

// Return an iterator to the tuple compliant with the interface Sequence
// CreateIterator Return an iterator to the tuple compliant with the interface Sequence
func (tuple *Tuple) CreateIterator() interface{} {
return &TupleIterator{
tuple: tuple,
pos: 0,
}
}

// Return an new iterator to the tuple
// NewTupleIterator Return an new iterator to the tuple
func NewTupleIterator(tuple Tuple) *TupleIterator {
return tuple.CreateIterator().(*TupleIterator)
}

// Return true if the iterator is on a element
// HasCurr Return true if the iterator is on a element
func (it *TupleIterator) HasCurr() bool {
return it.pos < len(*it.tuple.l)
}

// Return the element of which the iterator is positioned
// GetCurr Return the element of which the iterator is positioned
func (it *TupleIterator) GetCurr() interface{} {
return (*it.tuple.l)[it.pos]
}

// Advance the iterator to the next item of the tuple
// Next Advance the iterator to the next item of the tuple
func (it *TupleIterator) Next() interface{} {
it.pos++
return it
}

// Reset the iterator to the first element
// ResetFirst Reset the iterator to the first element
func (it *TupleIterator) ResetFirst() interface{} {
it.pos = 0
return it
}

// Return the n-th element of the tuple
// Nth Return the n-th element of the tuple
func (tuple *Tuple) Nth(i int) interface{} {
return (*tuple.l)[i]
}

// ReverseInPlace the subsequence between i and k
// ReverseInterval ReverseInPlace the subsequence between i and k
func (tuple *Tuple) ReverseInterval(i, j int) *Tuple {

sz := tuple.Size()
Expand All @@ -160,14 +165,14 @@ func (tuple *Tuple) ReverseInterval(i, j int) *Tuple {
return tuple
}

// Reverse the tuple in place
// ReverseInPlace Reverse the tuple in place
func (tuple *Tuple) ReverseInPlace() *Tuple {
return tuple.ReverseInterval(0, tuple.Size()-1)
}

// Return a reversed copy of tuple
// Reverse Return a reversed copy of tuple
func (tuple *Tuple) Reverse() *Tuple {
return tuple.clone().ReverseInterval(0, tuple.Size()-1)
return tuple.Clone().ReverseInterval(0, tuple.Size()-1)
}

func (tuple *Tuple) validateRotateIndexes(i, j, n int) {
Expand All @@ -186,7 +191,7 @@ func (tuple *Tuple) validateRotateIndexes(i, j, n int) {
}
}

// Rotate in place to right n positions the subsequence in [i, j]
// RotateIntervalRightInPlace Rotate in place to right n positions the subsequence in [i, j]
func (tuple *Tuple) RotateIntervalRightInPlace(i, j, n int) *Tuple {

tuple.validateRotateIndexes(i, j, n)
Expand All @@ -198,7 +203,7 @@ func (tuple *Tuple) RotateIntervalRightInPlace(i, j, n int) *Tuple {
return tuple
}

// Rotate in place to right n positions the subsequence in [i, j]
// RotateIntervalLeftInPlace Rotate in place to right n positions the subsequence in [i, j]
func (tuple *Tuple) RotateIntervalLeftInPlace(i, j, n int) *Tuple {

tuple.validateRotateIndexes(i, j, n)
Expand All @@ -210,35 +215,35 @@ func (tuple *Tuple) RotateIntervalLeftInPlace(i, j, n int) *Tuple {
return tuple
}

// Rotate in place the sequence n positions to right
// RotateRightInPlace Rotate in place the sequence n positions to right
func (tuple *Tuple) RotateRightInPlace(n int) *Tuple {
tuple.RotateIntervalRightInPlace(0, tuple.Size()-1, n)

return tuple
}

// Rotate in place the sequence n positions to left
// RotateLeftInPlace Rotate in place the sequence n positions to left
func (tuple *Tuple) RotateLeftInPlace(n int) *Tuple {
tuple.RotateIntervalLeftInPlace(0, tuple.Size()-1, n)

return tuple
}

// Return a new tuple copy of tuple rotate n position to right
// RotateRight Return a new tuple copy of tuple rotate n position to right
func (tuple *Tuple) RotateRight(n int) *Tuple {
return tuple.clone().RotateRightInPlace(n)
return tuple.Clone().RotateRightInPlace(n)
}

// Return a new tuple copy of tuple rotate n position to left
// RotateLeft Return a new tuple copy of tuple rotate n position to left
func (tuple *Tuple) RotateLeft(n int) *Tuple {
return tuple.clone().RotateLeftInPlace(n)
return tuple.Clone().RotateLeftInPlace(n)
}

func (tuple *Tuple) clone() *Tuple {
func (tuple *Tuple) Clone() *Tuple {
return NewTuple(*tuple.l...)
}

// Execute operation receiving every item of the sequence. Return seq
// ForEach Execute operation receiving every item of the sequence. Return seq
func ForEach(seq Sequence, operation func(interface{})) interface{} {

seq.Traverse(func(i interface{}) bool {
Expand All @@ -249,20 +254,18 @@ func ForEach(seq Sequence, operation func(interface{})) interface{} {
return seq
}

// Return true if all the elements of the sequence meets predicate
// All Return true if all the elements of the sequence meets predicate
func All(seq Sequence, predicate func(interface{}) bool) bool {

return seq.Traverse(predicate)
}

func Exist(seq Sequence, predicate func(interface{}) bool) bool {

return !All(seq, func(i interface{}) bool {
return !predicate(i)
})
}

// Return a new seq with the items of sequence transformed with transformation operation
// Map Return a new seq with the items of sequence transformed with transformation operation
func Map(seq Sequence, transformation func(interface{}) interface{}) *Seq.Slist {

ret := Seq.New()
Expand All @@ -272,7 +275,7 @@ func Map(seq Sequence, transformation func(interface{}) interface{}) *Seq.Slist
return ret
}

// Return a new seq with the items of sequence transformed with transformation operation for the items
// MapIf Return a new seq with the items of sequence transformed with transformation operation for the items
// satisfying the predicate
func MapIf(seq Sequence,
transformation func(interface{}) interface{},
Expand All @@ -287,7 +290,7 @@ func MapIf(seq Sequence,
return ret
}

// Return a list containing the items satisfying predicate
// Filter Return a list containing the items satisfying predicate
func Filter(seq Sequence, predicate func(interface{}) bool) *Seq.Slist {

ret := Seq.New()
Expand Down Expand Up @@ -364,7 +367,7 @@ func Find(seq Sequence, predicate func(item interface{}) bool) interface{} {
return nil
}

// Return a sequence containing the first n items from the sequence
// Take Return a sequence containing the first n items from the sequence
func Take(seq Sequence, n int) *Seq.Slist {

ret := Seq.New()
Expand All @@ -376,7 +379,7 @@ func Take(seq Sequence, n int) *Seq.Slist {
return ret
}

// Return a sequence containing the items after the first n from the sequence
// Drop Return a sequence containing the items after the first n from the sequence
func Drop(seq Sequence, n int) *Seq.Slist {

ret := Seq.New()
Expand All @@ -391,7 +394,7 @@ func Drop(seq Sequence, n int) *Seq.Slist {
return ret
}

// Return f(in, ..., f(i2, f(i1, initVal) ... ))
// Foldl Return f(in, ..., f(i2, f(i1, initVal) ... ))
func Foldl(seq Sequence, initVal interface{},
f func(acu, item interface{}) interface{}) interface{} {

Expand All @@ -402,7 +405,7 @@ func Foldl(seq Sequence, initVal interface{},
return retVal
}

// Return the n-th item in the sequence. Return nil if n is negative o greater than seq.Size()
// Nth Return the n-th item in the sequence. Return nil if n is negative o greater than seq.Size()
func Nth(seq Sequence, n int) interface{} {

if n < 0 || n >= seq.Size() {
Expand All @@ -419,7 +422,7 @@ func Nth(seq Sequence, n int) interface{} {
return nil // it should not be reached!
}

// Return the position in the sequence of the first element satisfying predicate. If no element satisfies
// Position Return the position in the sequence of the first element satisfying predicate. If no element satisfies
// predicate then it returns -1
func Position(seq Sequence, predicate func(item interface{}) bool) int {

Expand All @@ -433,7 +436,7 @@ func Position(seq Sequence, predicate func(item interface{}) bool) int {
return -1
}

// Zip all the lists into a list of tuples
// TZip Zip all the lists into a list of tuples
func TZip(list *Seq.Slist, lists ...*Seq.Slist) *Seq.Slist {

sz := len(lists) + 1
Expand All @@ -459,7 +462,7 @@ func TZip(list *Seq.Slist, lists ...*Seq.Slist) *Seq.Slist {
return ret
}

// Unzip a list of tuples into a tuple of lists
// TUnzip Unzip a list of tuples into a tuple of lists
func TUnzip(tupleList *Seq.Slist) *Tuple {

tupleSize := tupleList.First().(*Tuple).Size()
Expand Down
8 changes: 4 additions & 4 deletions functional_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ func TestTuple_Reverse(t *testing.T) {
func TestTuple_RotateIntervalRightInPlace(t *testing.T) {

tuple := NewTuple(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
tuplep := tuple.clone()
tuplep := tuple.Clone()
ForEach(tuple, func(i interface{}) {
fmt.Print(i, " ")
})
Expand All @@ -376,7 +376,7 @@ func TestTuple_RotateIntervalRightInPlace(t *testing.T) {
func TestTuple_RotateIntervalLeftInPlace(t *testing.T) {

tuple := NewTuple(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
tuplep := tuple.clone()
tuplep := tuple.Clone()
ForEach(tuple, func(i interface{}) {
fmt.Print(i, " ")
})
Expand All @@ -397,15 +397,15 @@ func TestTuple_RotateIntervalLeftInPlace(t *testing.T) {
func TestTuple_clone(t *testing.T) {

tuple := NewTuple(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
assert.True(t, All(Zip(tuple, tuple.clone()), func(pair interface{}) bool {
assert.True(t, All(Zip(tuple, tuple.Clone()), func(pair interface{}) bool {
return pair.(Pair).Item1.(int) == pair.(Pair).Item2.(int)
}))
}

func TestTuple_RotationsInPlace(t *testing.T) {

tuple := NewTuple(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
tuplep := tuple.clone()
tuplep := tuple.Clone()

tuple.RotateRightInPlace(3)

Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/geniussportsgroup/FunctionalLib
go 1.15

require (
github.com/geniussportsgroup/Slist v1.0.6
github.com/geniussportsgroup/treaps v1.1.8
github.com/geniussportsgroup/Slist v1.0.7
github.com/geniussportsgroup/treaps v1.2.1
github.com/stretchr/testify v1.7.0
)

0 comments on commit d466986

Please sign in to comment.