diff --git a/README.md b/README.md index c3ad01a..5d20951 100644 --- a/README.md +++ b/README.md @@ -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 \ No newline at end of file diff --git a/iterable.go b/iterable.go index e038aac..0a0c6dd 100644 --- a/iterable.go +++ b/iterable.go @@ -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 } diff --git a/list.go b/list.go index 989f2b4..9a89f67 100644 --- a/list.go +++ b/list.go @@ -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()) diff --git a/list_test.go b/list_test.go index 6a9c563..7330e6a 100644 --- a/list_test.go +++ b/list_test.go @@ -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, @@ -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) + } +}