Skip to content

Commit

Permalink
Merge pull request #9 from tmontdev/feature/set
Browse files Browse the repository at this point in the history
adding set method
  • Loading branch information
tmontdev authored Jun 6, 2023
2 parents c2764ad + af08289 commit 3220ecb
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,5 @@ Added new methods: **Where**
Added new methods: **Map**

### v0.1.0
Added new methods: **Reduce, Every, Some, None, Pop, Shift**
Added new methods: **Reduce, Every, Some, None, Pop, Shift, Set**
String representation
3 changes: 3 additions & 0 deletions iterable.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ type Iterable[T any] interface {
// Shift removes the first element from the Iterable and returns itself
Shift() Iterable[T]

// Set sets the given element in the given index, and returns itself
Set(index int, element T) Iterable[T]

// String returns a string representation of the Iterable
String() string
}
9 changes: 9 additions & 0 deletions list.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,15 @@ func (l *List[T]) Shift() Iterable[T] {
return l
}

// Set sets the given element in the given index, and returns itself
// if the given index is not yet filled, panics
func (l *List[T]) Set(index int, element T) Iterable[T] {
l.ElementAt(index)
at := l.At(index)
*at = element
return l
}

// String returns a string representation of the List
func (l *List[T]) String() string {
return fmt.Sprint(l.Elements())
Expand Down
22 changes: 21 additions & 1 deletion list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -511,9 +511,23 @@ var shiftCases = []testCase[bool]{
},
}

var setCases = []testCase[bool]{
{
name: "List.Set",
input: oneTwoThreeList.Clone(),
expected: true,
expectPanic: false,
runnable: func(t *testing.T, list Iterable[any], parameters []any) bool {
set := list.Set(1, 4)
return set.Length() == list.Length() && set.ElementAt(0) == list.ElementAt(0) && set.ElementAt(1) == 4 && set.ElementAt(2) == list.ElementAt(2)

},
},
}

var stringCases = []testCase[string]{
{
name: "List.Shift.Double",
name: "List.String",
input: oneTwoThreeList.Clone(),
expected: fmt.Sprint(oneTwoThreeList.Elements()),
expectPanic: false,
Expand Down Expand Up @@ -600,3 +614,9 @@ func TestString(t *testing.T) {
caseRunner[string](t, v)
}
}

func TestSet(t *testing.T) {
for _, v := range setCases {
caseRunner[bool](t, v)
}
}

0 comments on commit 3220ecb

Please sign in to comment.