Skip to content
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

Renames #89

Merged
merged 5 commits into from
May 5, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
7 changes: 3 additions & 4 deletions functions/select.go → functions/filter.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package functions

// Select will return a new slice containing only the elements that return
// Filter will return a new slice containing only the elements that return
// true from the condition. The returned slice may contain zero elements (nil).
//
// Unselect works in the opposite way as Select.
func (ss SliceType) Select(condition func(ElementType) bool) (ss2 SliceType) {
// FilterNot works in the opposite way of Filter.
func (ss SliceType) Filter(condition func(ElementType) bool) (ss2 SliceType) {
for _, s := range ss {
if condition(s) {
ss2 = append(ss2, s)
}
}

return
}
4 changes: 2 additions & 2 deletions functions/unselect.go → functions/filternot.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package functions

// Unselect works the same as Select, with a negated condition. That is, it will
// FilterNot works the same as Filter, with a negated condition. That is, it will
// return a new slice only containing the elements that returned false from the
// condition. The returned slice may contain zero elements (nil).
func (ss SliceType) Unselect(condition func(ElementType) bool) (ss2 SliceType) {
func (ss SliceType) FilterNot(condition func(ElementType) bool) (ss2 SliceType) {
for _, s := range ss {
if !condition(s) {
ss2 = append(ss2, s)
Expand Down
6 changes: 3 additions & 3 deletions functions/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ var Functions = []struct {
{"Min", "min.go", ForNumbersAndStrings},
{"Random", "random.go", ForAll},
{"Reverse", "reverse.go", ForAll},
{"Select", "select.go", ForAll},
{"Filter", "filter.go", ForAll},
{"Sort", "sort.go", ForNumbersAndStrings},
{"Sum", "sum.go", ForNumbers},
{"Shuffle", "shuffle.go", ForAll},
{"Top", "top.go", ForAll},
{"ToStrings", "to_strings.go", ForAll},
{"Transform", "transform.go", ForAll},
{"Map", "map.go", ForAll},
{"Unique", "unique.go", ForNumbersAndStrings},
{"Unselect", "unselect.go", ForAll},
{"FilterNot", "filternot.go", ForAll},
{"Values", "values.go", ForMaps},
}

Expand Down
6 changes: 3 additions & 3 deletions functions/transform.go → functions/map.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package functions

// Transform will return a new slice where each element has been transformed.
// The number of element returned will always be the same as the input.
// Map will return a new slice where each element has been mapped (transformed).
// The number of elements returned will always be the same as the input.
//
// Be careful when using this with slices of pointers. If you modify the input
// value it will affect the original slice. Be sure to return a new allocated
// object or deep copy the existing one.
func (ss SliceType) Transform(fn func(ElementType) ElementType) (ss2 SliceType) {
func (ss SliceType) Map(fn func(ElementType) ElementType) (ss2 SliceType) {
if ss == nil {
return nil
}
Expand Down
17 changes: 8 additions & 9 deletions pie/carpointers_pie.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,17 +194,16 @@ func (ss carPointers) Reverse() carPointers {
return sorted
}

// Select will return a new slice containing only the elements that return
// Filter will return a new slice containing only the elements that return
// true from the condition. The returned slice may contain zero elements (nil).
//
// Unselect works in the opposite way as Select.
func (ss carPointers) Select(condition func(*car) bool) (ss2 carPointers) {
// FilterNot works in the opposite way of Filter.
func (ss carPointers) Filter(condition func(*car) bool) (ss2 carPointers) {
for _, s := range ss {
if condition(s) {
ss2 = append(ss2, s)
}
}

return
}

Expand Down Expand Up @@ -261,13 +260,13 @@ func (ss carPointers) ToStrings(transform func(*car) string) Strings {
return result
}

// Transform will return a new slice where each element has been transformed.
// The number of element returned will always be the same as the input.
// Map will return a new slice where each element has been mapped (transformed).
// The number of elements returned will always be the same as the input.
//
// Be careful when using this with slices of pointers. If you modify the input
// value it will affect the original slice. Be sure to return a new allocated
// object or deep copy the existing one.
func (ss carPointers) Transform(fn func(*car) *car) (ss2 carPointers) {
func (ss carPointers) Map(fn func(*car) *car) (ss2 carPointers) {
if ss == nil {
return nil
}
Expand All @@ -280,10 +279,10 @@ func (ss carPointers) Transform(fn func(*car) *car) (ss2 carPointers) {
return
}

// Unselect works the same as Select, with a negated condition. That is, it will
// FilterNot works the same as Filter, with a negated condition. That is, it will
// return a new slice only containing the elements that returned false from the
// condition. The returned slice may contain zero elements (nil).
func (ss carPointers) Unselect(condition func(*car) bool) (ss2 carPointers) {
func (ss carPointers) FilterNot(condition func(*car) bool) (ss2 carPointers) {
for _, s := range ss {
if !condition(s) {
ss2 = append(ss2, s)
Expand Down
26 changes: 13 additions & 13 deletions pie/carpointers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ func TestCarPointers_Contains(t *testing.T) {
}
}

var carPointersSelectTests = []struct {
var carPointersFilterTests = []struct {
ss carPointers
condition func(*car) bool
expectedSelect carPointers
expectedUnselect carPointers
expectedTransform carPointers
expectedFilter carPointers
expectedFilterNot carPointers
expectedMap carPointers
}{
{
nil,
Expand All @@ -70,29 +70,29 @@ var carPointersSelectTests = []struct {
},
}

func TestCarPointers_Select(t *testing.T) {
for _, test := range carPointersSelectTests {
func TestCarPointers_Filter(t *testing.T) {
for _, test := range carPointersFilterTests {
t.Run("", func(t *testing.T) {
defer assertImmutableCarPointers(t, &test.ss)()
assert.Equal(t, test.expectedSelect, test.ss.Select(test.condition))
assert.Equal(t, test.expectedFilter, test.ss.Filter(test.condition))
})
}
}

func TestCarPointers_Unselect(t *testing.T) {
for _, test := range carPointersSelectTests {
func TestCarPointers_FilterNot(t *testing.T) {
for _, test := range carPointersFilterTests {
t.Run("", func(t *testing.T) {
defer assertImmutableCarPointers(t, &test.ss)()
assert.Equal(t, test.expectedUnselect, test.ss.Unselect(test.condition))
assert.Equal(t, test.expectedFilterNot, test.ss.FilterNot(test.condition))
})
}
}

func TestCarPointers_Transform(t *testing.T) {
for _, test := range carPointersSelectTests {
func TestCarPointers_Map(t *testing.T) {
for _, test := range carPointersFilterTests {
t.Run("", func(t *testing.T) {
defer assertImmutableCarPointers(t, &test.ss)()
assert.Equal(t, test.expectedTransform, test.ss.Transform(func(c *car) *car {
assert.Equal(t, test.expectedMap, test.ss.Map(func(c *car) *car {
return &car{
Name: strings.ToUpper(c.Name),
Color: c.Color,
Expand Down
17 changes: 8 additions & 9 deletions pie/cars_pie.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,17 +194,16 @@ func (ss cars) Reverse() cars {
return sorted
}

// Select will return a new slice containing only the elements that return
// Filter will return a new slice containing only the elements that return
// true from the condition. The returned slice may contain zero elements (nil).
//
// Unselect works in the opposite way as Select.
func (ss cars) Select(condition func(car) bool) (ss2 cars) {
// FilterNot works in the opposite way of Filter.
func (ss cars) Filter(condition func(car) bool) (ss2 cars) {
for _, s := range ss {
if condition(s) {
ss2 = append(ss2, s)
}
}

return
}

Expand Down Expand Up @@ -261,13 +260,13 @@ func (ss cars) ToStrings(transform func(car) string) Strings {
return result
}

// Transform will return a new slice where each element has been transformed.
// The number of element returned will always be the same as the input.
// Map will return a new slice where each element has been mapped (transformed).
// The number of elements returned will always be the same as the input.
//
// Be careful when using this with slices of pointers. If you modify the input
// value it will affect the original slice. Be sure to return a new allocated
// object or deep copy the existing one.
func (ss cars) Transform(fn func(car) car) (ss2 cars) {
func (ss cars) Map(fn func(car) car) (ss2 cars) {
if ss == nil {
return nil
}
Expand All @@ -280,10 +279,10 @@ func (ss cars) Transform(fn func(car) car) (ss2 cars) {
return
}

// Unselect works the same as Select, with a negated condition. That is, it will
// FilterNot works the same as Filter, with a negated condition. That is, it will
// return a new slice only containing the elements that returned false from the
// condition. The returned slice may contain zero elements (nil).
func (ss cars) Unselect(condition func(car) bool) (ss2 cars) {
func (ss cars) FilterNot(condition func(car) bool) (ss2 cars) {
for _, s := range ss {
if !condition(s) {
ss2 = append(ss2, s)
Expand Down
26 changes: 13 additions & 13 deletions pie/cars_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ func TestCars_Contains(t *testing.T) {
}
}

var carsSelectTests = []struct {
var carsFilterTests = []struct {
ss cars
condition func(car) bool
expectedSelect cars
expectedUnselect cars
expectedTransform cars
expectedFilter cars
expectedFilterNot cars
expectedMap cars
}{
{
nil,
Expand All @@ -61,29 +61,29 @@ var carsSelectTests = []struct {
},
}

func TestCars_Select(t *testing.T) {
for _, test := range carsSelectTests {
func TestCars_Filter(t *testing.T) {
for _, test := range carsFilterTests {
t.Run("", func(t *testing.T) {
defer assertImmutableCars(t, &test.ss)()
assert.Equal(t, test.expectedSelect, test.ss.Select(test.condition))
assert.Equal(t, test.expectedFilter, test.ss.Filter(test.condition))
})
}
}

func TestCars_Unselect(t *testing.T) {
for _, test := range carsSelectTests {
func TestCars_FilterNot(t *testing.T) {
for _, test := range carsFilterTests {
t.Run("", func(t *testing.T) {
defer assertImmutableCars(t, &test.ss)()
assert.Equal(t, test.expectedUnselect, test.ss.Unselect(test.condition))
assert.Equal(t, test.expectedFilterNot, test.ss.FilterNot(test.condition))
})
}
}

func TestCars_Transform(t *testing.T) {
for _, test := range carsSelectTests {
func TestCars_Map(t *testing.T) {
for _, test := range carsFilterTests {
t.Run("", func(t *testing.T) {
defer assertImmutableCars(t, &test.ss)()
assert.Equal(t, test.expectedTransform, test.ss.Transform(func(car car) car {
assert.Equal(t, test.expectedMap, test.ss.Map(func(car car) car {
car.Name = strings.ToUpper(car.Name)

return car
Expand Down
17 changes: 8 additions & 9 deletions pie/float64s_pie.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,17 +285,16 @@ func (ss Float64s) Reverse() Float64s {
return sorted
}

// Select will return a new slice containing only the elements that return
// Filter will return a new slice containing only the elements that return
// true from the condition. The returned slice may contain zero elements (nil).
//
// Unselect works in the opposite way as Select.
func (ss Float64s) Select(condition func(float64) bool) (ss2 Float64s) {
// FilterNot works in the opposite way of Filter.
func (ss Float64s) Filter(condition func(float64) bool) (ss2 Float64s) {
for _, s := range ss {
if condition(s) {
ss2 = append(ss2, s)
}
}

return
}

Expand Down Expand Up @@ -381,13 +380,13 @@ func (ss Float64s) ToStrings(transform func(float64) string) Strings {
return result
}

// Transform will return a new slice where each element has been transformed.
// The number of element returned will always be the same as the input.
// Map will return a new slice where each element has been mapped (transformed).
// The number of elements returned will always be the same as the input.
//
// Be careful when using this with slices of pointers. If you modify the input
// value it will affect the original slice. Be sure to return a new allocated
// object or deep copy the existing one.
func (ss Float64s) Transform(fn func(float64) float64) (ss2 Float64s) {
func (ss Float64s) Map(fn func(float64) float64) (ss2 Float64s) {
if ss == nil {
return nil
}
Expand Down Expand Up @@ -431,10 +430,10 @@ func (ss Float64s) Unique() Float64s {
return uniqueValues
}

// Unselect works the same as Select, with a negated condition. That is, it will
// FilterNot works the same as Filter, with a negated condition. That is, it will
// return a new slice only containing the elements that returned false from the
// condition. The returned slice may contain zero elements (nil).
func (ss Float64s) Unselect(condition func(float64) bool) (ss2 Float64s) {
func (ss Float64s) FilterNot(condition func(float64) bool) (ss2 Float64s) {
for _, s := range ss {
if !condition(s) {
ss2 = append(ss2, s)
Expand Down
18 changes: 9 additions & 9 deletions pie/float64s_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ func TestFloat64s_Contains(t *testing.T) {
var float64sSelectTests = []struct {
ss Float64s
condition func(float64) bool
expectedSelect Float64s
expectedUnselect Float64s
expectedTransform Float64s
expectedFilter Float64s
expectedFilterNot Float64s
expectedMap Float64s
}{
{
nil,
Expand All @@ -59,29 +59,29 @@ var float64sSelectTests = []struct {
},
}

func TestFloat64s_Select(t *testing.T) {
func TestFloat64s_Filter(t *testing.T) {
for _, test := range float64sSelectTests {
t.Run("", func(t *testing.T) {
defer assertImmutableFloat64s(t, &test.ss)()
assert.Equal(t, test.expectedSelect, test.ss.Select(test.condition))
assert.Equal(t, test.expectedFilter, test.ss.Filter(test.condition))
})
}
}

func TestFloat64s_Unselect(t *testing.T) {
func TestFloat64s_FilterNot(t *testing.T) {
for _, test := range float64sSelectTests {
t.Run("", func(t *testing.T) {
defer assertImmutableFloat64s(t, &test.ss)()
assert.Equal(t, test.expectedUnselect, test.ss.Unselect(test.condition))
assert.Equal(t, test.expectedFilterNot, test.ss.FilterNot(test.condition))
})
}
}

func TestFloat64s_Transform(t *testing.T) {
func TestFloat64s_Map(t *testing.T) {
for _, test := range float64sSelectTests {
t.Run("", func(t *testing.T) {
defer assertImmutableFloat64s(t, &test.ss)()
assert.Equal(t, test.expectedTransform, test.ss.Transform(func(a float64) float64 {
assert.Equal(t, test.expectedMap, test.ss.Map(func(a float64) float64 {
return a + 5.2
}))
})
Expand Down
Loading