Skip to content

add a new functions - Remove #172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 27 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,16 +224,19 @@ The letters in brackets indicate:
| [`Shuffle`](#shuffle) | ✓ | ✓ | ✓ | | n | Shuffle returns shuffled slice by your rand.Source |
| [`Sort`](#sort) | ✓ | ✓ | | | n⋅log(n) | Sort works similar to sort.SliceType(). However, unlike sort.SliceType the slice returned will be reallocated as to not modify the input slice. |
| [`SortStableUsing`](#sortstableusing) | ✓ | | ✓ | | n⋅log(n) | SortStableUsing works similar to sort.SliceStable. However, unlike sort.SliceStable the slice returned will be reallocated as to not modify the input slice. |
| [`SortUsing`](#sortusing) | ✓ | | ✓ | | n⋅log(n) | SortUsing works similar to sort.Slice. However, unlike sort.Slice the slice returned will be reallocated as to not modify the input slice. |
| [`Stddev`](#stddev) | | ✓ | | | n | Stddev is the standard deviation |
| [`Strings`](#strings) (S) | ✓ | ✓ | ✓ | | n | Strings transforms each element to a string. |
| [`SubSlice`](#subslice) | ✓ | ✓ | ✓ | | n | SubSlice will return the subSlice from start to end(excluded) |
| [`Sum`](#sum) | | ✓ | | | n | Sum is the sum of all of the elements. |
| [`Top`](#top) | ✓ | ✓ | ✓ | | n | Top will return n elements from head of the slice if the slice has less elements then n that'll return all elements if n < 0 it'll return empty slice. |
| [`StringsUsing`](#stringsusing) | ✓ | ✓ | ✓ | | n | StringsUsing transforms each element to a string. |
| [`Unique`](#unique) | ✓ | ✓ | | | n | Unique returns a new slice with all of the unique values. |
| [`Unshift`](#unshift) | ✓ | ✓ | ✓ | | n | Unshift adds one or more elements to the beginning of the slice and returns the new slice. |
| [`Values`](#values) | | | | ✓ | n | Values returns the values in the map. |
| [`SortUsing`](#sortusing) | ✓ | | ✓ | | n⋅log(n) | SortUsing works similar to sort.Slice. However,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The format of the table has been mangled. Please fix.

unlike sort.Slice the slice returned will be reallocated as to not modify the input slice. | | [`Stddev`](#stddev)
| | ✓ | | | n | Stddev is the standard deviation | | [`Strings`](#strings) (S) | ✓ | ✓ | ✓ | | n | Strings
transforms each element to a string. | | [`SubSlice`](#subslice) | ✓ | ✓ | ✓ | | n | SubSlice will
return the subSlice from start to end(excluded) | | [`Sum`](#sum) | | ✓ | | | n | Sum is the
sum of all of the elements. | | [`Top`](#top) | ✓ | ✓ | ✓ | | n | Top will return n elements
from head of the slice if the slice has less elements then n that'll return all elements if n < 0 it'll return empty
slice. | | [`StringsUsing`](#stringsusing) | ✓ | ✓ | ✓ | | n | StringsUsing transforms each element to a string.
| | [`Unique`](#unique) | ✓ | ✓ | | | n | Unique returns a new slice with all of the unique values.
| | [`Unshift`](#unshift) | ✓ | ✓ | ✓ | | n | Unshift adds one or more elements to the beginning of
the slice and returns the new slice. | | [`Values`](#values) | | | | ✓ | n | Values returns the
values in the map. | | [`Remove`](#values) | ✓ | ✓ | | ✓ | 2n | Remove returns a new slice that does
not include any of the items. |

## Abs

Expand Down Expand Up @@ -750,17 +753,27 @@ See AreUnique().

## Unshift

Unshift adds one or more elements to the beginning of the slice
and returns the new slice.

Unshift adds one or more elements to the beginning of the slice and returns the new slice.

## Values

Values returns the values in the map.


Due to Go's randomization of iterating maps the order is not deterministic.

## Remove
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep the titles sorted.


Remove returns a new slice that does not include any of the items.

Usage Example:

```go
listA := pie.Strings{"1", "2"}
listB := pie.Strings{"1", "3"}

// [ "2" ]
fmt.Println(listA.Remove(listB...))
```

# FAQ

Expand Down
1 change: 1 addition & 0 deletions functions/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ var Functions = []struct {
{"Unique", "unique.go", ForNumbersAndStrings, "n"},
{"Unshift", "unshift.go", ForAll, "n"},
{"Values", "values.go", ForMaps, "n"},
{"Remove", "remove.go", ForNumbersAndStrings, "2n"},
Copy link
Owner

@elliotchance elliotchance Sep 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

O(2n) is not a valid complexity, the constant would be removed to O(n). Also, you must keep the functions sorted.

}

type ElementType float64
Expand Down
21 changes: 21 additions & 0 deletions functions/remove.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package functions

// Remove returns a new slice that does not include any of the items.
func (ss SliceType) Remove(items ...ElementType) (result SliceType) {
if len(items) == 0 {
return items
}

ss2 := make(map[ElementType]bool, len(items))
for _, item := range items {
ss2[item] = true
}

result = SliceType{}
for _, v := range ss {
if !ss2[v] {
result = append(result, v)
}
}
return
}
20 changes: 20 additions & 0 deletions pie/float64s_pie.go
Original file line number Diff line number Diff line change
Expand Up @@ -989,3 +989,23 @@ func (ss Float64s) Unshift(elements ...float64) (unshift Float64s) {

return
}

// Remove returns a new slice that does not include any of the items.
func (ss Float64s) Remove(items ...float64) (result Float64s) {
if len(items) == 0 {
return items
}

ss2 := make(map[float64]bool, len(items))
for _, item := range items {
ss2[item] = true
}

result = Float64s{}
for _, v := range ss {
if !ss2[v] {
result = append(result, v)
}
}
return
}
20 changes: 20 additions & 0 deletions pie/ints_pie.go
Original file line number Diff line number Diff line change
Expand Up @@ -989,3 +989,23 @@ func (ss Ints) Unshift(elements ...int) (unshift Ints) {

return
}

// Remove returns a new slice that does not include any of the items.
func (ss Ints) Remove(items ...int) (result Ints) {
if len(items) == 0 {
return items
}

ss2 := make(map[int]bool, len(items))
for _, item := range items {
ss2[item] = true
}

result = Ints{}
for _, v := range ss {
if !ss2[v] {
result = append(result, v)
}
}
return
}
20 changes: 20 additions & 0 deletions pie/strings_pie.go
Original file line number Diff line number Diff line change
Expand Up @@ -874,3 +874,23 @@ func (ss Strings) Unshift(elements ...string) (unshift Strings) {

return
}

// Remove returns a new slice that does not include any of the items.
func (ss Strings) Remove(items ...string) (result Strings) {
if len(items) == 0 {
return items
}

ss2 := make(map[string]bool, len(items))
for _, item := range items {
ss2[item] = true
}

result = Strings{}
for _, v := range ss {
if !ss2[v] {
result = append(result, v)
}
}
return
}
22 changes: 22 additions & 0 deletions template.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.