Skip to content

Commit

Permalink
Merge pull request #11 from tmontdev/feature/sort
Browse files Browse the repository at this point in the history
adding method sort
  • Loading branch information
tmontdev authored Jun 25, 2023
2 parents e46d854 + 78f45b9 commit 04e3026
Show file tree
Hide file tree
Showing 7 changed files with 260 additions and 181 deletions.
29 changes: 16 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[![Go Documentation](https://godocs.io/github.com/tmontdev/iterable?status.svg)](https://godocs.io/github.com/tmontdev/iterable)
[![Go Report Card](https://goreportcard.com/badge/github.com/tmontdev/iterable)](https://goreportcard.com/report/github.com/tmontdev/iterable)
[![Go Documentation](https://godocs.io/github.com/tmontdev/collection?status.svg)](https://godocs.io/github.com/tmontdev/collection)
[![Go Report Card](https://goreportcard.com/badge/github.com/tmontdev/collection)](https://goreportcard.com/report/github.com/tmontdev/collection)
[![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=tmontdev_iterable&metric=reliability_rating)](https://sonarcloud.io/summary/new_code?id=tmontdev_iterable)
[![Sourcegraph](https://sourcegraph.com/github.com/tmontdev/iterable/-/badge.svg)](https://sourcegraph.com/github.com/tmontdev/iterable?badge)
![visitors](https://visitor-badge.laobi.icu/badge?page_id=tmontdev.iterable)
[![MIT License](https://img.shields.io/badge/License-MIT-green.svg)](https://github.com/tmontdev/iterable/blob/main/LICENSE)
[![Sourcegraph](https://sourcegraph.com/github.com/tmontdev/collection/-/badge.svg)](https://sourcegraph.com/github.com/tmontdev/collection?badge)
![visitors](https://visitor-badge.laobi.icu/badge?page_id=tmontdev.collection)
[![MIT License](https://img.shields.io/badge/License-MIT-green.svg)](https://github.com/tmontdev/collection/blob/main/LICENSE)

<!-- TOC -->

- [Iterable](#iterable)
- [Collection](#collection)
- [Install](#install)
- [Usage](#usage-)
- [Release Notes](#release-notes)
Expand All @@ -16,23 +16,25 @@

<!-- TOC -->

# Iterable
**Iterable** package provides a simple interface to handle data collections in golang.
# Collection

**Collection** package provides a simple interface to handle data collections in golang.

## Install

```shell
go get github.com/tmontdev/iterable
go get github.com/tmontdev/collection
```

## Usage [![Go Documentation](https://godocs.io/github.com/tmontdev/iterable?status.svg)](https://godocs.io/github.com/tmontdev/iterable)
## Usage [![Go Documentation](https://godocs.io/github.com/tmontdev/collection?status.svg)](https://godocs.io/github.com/tmontdev/collection)

**To get get usage instructions, see our [godoc](https://godocs.io/github.com/tmontdev/iterable)**
**To get get usage instructions, see our [godoc](https://godocs.io/github.com/tmontdev/collection)**

## Release Notes

### v0.1.0

**Iterable** interface with the primary methods:
**collection** interface with the primary methods:

- **Length**
- **IsEmpty**
Expand Down Expand Up @@ -62,5 +64,6 @@ go get github.com/tmontdev/iterable

### v0.2.0

**Iterable** got new methods:
**collection** got new methods:

- **Interval**
95 changes: 95 additions & 0 deletions collection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package collection

type Collection[T any] interface {
// Length method returns how many element are in the Collection
Length() int

// IsEmpty returns true if Length is zero
IsEmpty() bool

// IsNotEmpty returns true if Length is not zero
IsNotEmpty() bool

// At returns the pointer of the element in the given index from the Collection
// If there is no element in the index, nil will be returned
At(int) *T

// ElementAt returns the element in the given index from the Collection
// If there is no element in the index, panics
ElementAt(int) T

// Elements returns a built-in slice with all elements in the Collection
Elements() []T

// Push add the given elements in the Collection, and returns itself
Push(...T) Collection[T]

// Clone returns an identical Collection from the original
Clone() Collection[T]

// FirstElement returns the first element in the Collection
// if isEmpty, panics
FirstElement() T

// First returns the pointer of the first element in the Collection
// if isEmpty, nil will be returned
First() *T

// LastElement returns the last element in the Collection
// if isEmpty, panics
LastElement() T

// Last returns the pointer of the last element in the Collection
// if isEmpty, nil will be returned
Last() *T

// FirstIndexWhere returns the index of the first element witch satisfies the predicate
// if no element satisfies the predicate, -1 will be returned
FirstIndexWhere(handler Predicate[T]) int

// LastIndexWhere returns the index of the last element witch satisfies the predicate
// if no element satisfies the predicate, -1 will be returned
LastIndexWhere(handler Predicate[T]) int

// IndexWhere returns a Collection[int] for all element index witch satisfies the predicate
// if no element satisfies the predicate, an empty Collection will be returned
IndexWhere(handler Predicate[T]) Collection[int]

// Where returns a Collection with all the elements witch satisfies the predicate.
// if no element satisfies the predicate, an empty Collection will be returned
Where(handler Predicate[T]) Collection[T]

// Map iterates over the elements of the Collection calling Mapper, and return a new Collection with the results.
Map(handler Mapper[T]) Collection[any]

// Reduce executes the Reducer for each element from the list with the given accumulator, and each result will be accumulator for the next
// The final result will be returned
Reduce(reducer Reducer[T], accumulator any) any

// Every returns true if every element in the Collection satisfy the predicate
Every(handler Predicate[T]) bool

// Some returns true if at least one element in the Collection satisfy the predicate
Some(handler Predicate[T]) bool

// None returns true no element in the Collection satisfy the predicate
None(handler Predicate[T]) bool

// Pop removes the last element from the Collection and returns itself
Pop() Collection[T]

// Shift removes the first element from the Collection and returns itself
Shift() Collection[T]

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

// Interval returns a new Collection with all elements between from and to given indexes
Interval(from, to int) Collection[T]

// String returns a string representation of the Collection
String() string

// Sort receive a Sorter function to sort its elements, and returns itself after sorted
Sort(sorter Sorter[T]) Collection[T]
}
8 changes: 5 additions & 3 deletions functions.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package iterable
package collection

type PredicateHandler[T any] func(T) bool
type Predicate[T any] func(T) bool

type MapHandler[T any] func(T) any
type Mapper[T any] func(T) any

type Reducer[T any] func(any, T, int) any

type Sorter[T any] func(T, T) int
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/tmontdev/iterable
module github.com/tmontdev/collection

go 1.20
92 changes: 0 additions & 92 deletions iterable.go

This file was deleted.

Loading

0 comments on commit 04e3026

Please sign in to comment.