Skip to content

Commit

Permalink
Merge pull request #22 from tmontdev/improve/docs
Browse files Browse the repository at this point in the history
docs reviewed
  • Loading branch information
tmontdev authored Nov 22, 2023
2 parents b9567b4 + 0bcf5bb commit 3668d6a
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import "github.com/tmontdev/collections"

## Working with Maps

> collections package provide the [Map](https://godocs.io/github.com/tmontdev/collections#Map) interface with helper methods to make your work with maps easier. The default implementation of [Map](https://godocs.io/github.com/tmontdev/collections#Map) is the [HashMap](https://godocs.io/github.com/tmontdev/collections#HashMap) which is based on a built-in map type.collections package provide the [Map](https://godocs.io/github.com/tmontdev/collections#Map) interface, with helper methods to turn your work easier.
> collections package provide the [Map](https://godocs.io/github.com/tmontdev/collections#Map) interface with helper methods to make your work with maps easier. The default implementation of [Map](https://godocs.io/github.com/tmontdev/collections#Map) is the [HashMap](https://godocs.io/github.com/tmontdev/collections#HashMap) which is based on a built-in map type.
Let's make an example of Telephone Spelling Alphabet with Maps, and see how it works!

Expand Down
3 changes: 3 additions & 0 deletions list.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ type List[T any] interface {
// String returns a string representation of the List.
String() string

// Join returns the string representation of each element in the List, separated by the given separator
Join(separator string) string

// Sort receives a Sorter function to sort its elements, and returns itself after sorted.
Sort(sorter Sorter[T]) List[T]

Expand Down
24 changes: 24 additions & 0 deletions list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,30 @@ var encodeCases = []listTestCase[string]{
},
nilTypeComparison: false,
},
{
name: "SimpleList.Join",
input: NewList[any](1, 2, 3),
parameters: nil,
expectPanic: false,
expected: "1|2|3",
runnable: func(t *testing.T, l List[any], parameters []any) string {

return l.Join("|")
},
nilTypeComparison: false,
},
{
name: "SimpleList.Join.Empty",
input: NewList[any](),
parameters: nil,
expectPanic: false,
expected: "",
runnable: func(t *testing.T, l List[any], parameters []any) string {

return l.Join("|")
},
nilTypeComparison: false,
},
}

func TestLength(t *testing.T) {
Expand Down
7 changes: 7 additions & 0 deletions safe_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,13 @@ func (s *SafeList[T]) String() string {
})
}

// Join returns the string representation of each element in the List, separated by the given separator
func (s *SafeList[T]) Join(separator string) string {
return protect[string, T](s, func() string {
return s.l.Join(separator)
})
}

// Sort receives a Sorter function to sort its elements, and returns itself after sorted.
func (s *SafeList[T]) Sort(sorter Sorter[T]) List[T] {
return s.self(func() any {
Expand Down
15 changes: 15 additions & 0 deletions simple_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,21 @@ func (l *SimpleList[T]) String() string {
return fmt.Sprint(l.Elements())
}

// Join returns the string representation of each element in the List, separated by the given separator
func (l *SimpleList[T]) Join(separator string) (joined string) {
if l.IsEmpty() {
return
}
for i, e := range l.Elements() {
if i == 0 {
joined += fmt.Sprintf("%v", e)
continue
}
joined += fmt.Sprintf("%s%v", separator, e)
}
return
}

// Sort receives a Sorter function to sort its elements, and returns itself after sorted.
func (l *SimpleList[T]) Sort(sorter Sorter[T]) List[T] {
changed := true
Expand Down

0 comments on commit 3668d6a

Please sign in to comment.