Skip to content

Commit

Permalink
Problem: does not well for for named types
Browse files Browse the repository at this point in the history
Solution: document the problem, there's probably no easy solution
  • Loading branch information
vyskocilm committed Feb 26, 2024
1 parent 333496e commit 6a847ce
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# it: experimental iterator utilities for Go
# it: simply the best iterator utilities for Go

# The problem

Expand Down Expand Up @@ -444,6 +444,30 @@ func Example_break_da_chain() {
1. provide `FilterContext` et all
1. `it/itctx` package

## Named types

See https://go.dev/blog/deconstructing-type-parameters and `func Clone[S ~[]E,
E any](s S) S`. The `it` has the same problem

```diff
func PrintSorted(ms MySlice) string {
c := it.NewChain(it.FromSlice(ms)).Sort(slices.Sort).Slice()
- return c.String() // FAILS TO COMPILE
+ return MySlice(c).String() // this works
}
```

This is not a solution as the original `S` got lost.

```
func fromSlice[S ~[]E, E any](slice S) iter.Seq[E] {
```

Possible solutions:

1. ignore it - it is easy to go back to the original type
2. do not return iter.Seq and have a wrapper structure of type `Seq[S ~[]E, E any]`

# Other libraries

Inspiration and other cool projects.
Expand Down
26 changes: 25 additions & 1 deletion README.md.tpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# it: experimental iterator utilities for Go
# it: simply the best iterator utilities for Go

# The problem

Expand Down Expand Up @@ -181,6 +181,30 @@ actually a good idea. It is definitely doable and possible in Go.
1. provide `FilterContext` et all
1. `it/itctx` package

## Named types

See https://go.dev/blog/deconstructing-type-parameters and `func Clone[S ~[]E,
E any](s S) S`. The `it` has the same problem

```diff
func PrintSorted(ms MySlice) string {
c := it.NewChain(it.FromSlice(ms)).Sort(slices.Sort).Slice()
- return c.String() // FAILS TO COMPILE
+ return MySlice(c).String() // this works
}
```

This is not a solution as the original `S` got lost.

```
func fromSlice[S ~[]E, E any](slice S) iter.Seq[E] {
```
Possible solutions:
1. ignore it - it is easy to go back to the original type
2. do not return iter.Seq and have a wrapper structure of type `Seq[S ~[]E, E any]`
# Other libraries
Inspiration and other cool projects.
Expand Down
8 changes: 8 additions & 0 deletions chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ func (g Chain[T]) Slice() []T {
return AsSlice(g.seq)
}

func (g Chain[T]) Sort(sortFunc SortFunc[T]) Chain[T] {
return Chain[T]{seq: Sort(g.seq, sortFunc)}
}

// Mapable allows the operations to be chained via method calls and
// additionally T -> V and V -> T mapping can be added
type Mapable[T, V any] struct {
Expand Down Expand Up @@ -72,3 +76,7 @@ func (g Mapable[T, V]) Reduce(reduceFunc ReduceFunc[T], initial T) T {
func (g Mapable[T, V]) Slice() []T {
return AsSlice(g.seq)
}

func (g Mapable[T, V]) Sort(sortFunc SortFunc[T]) Mapable[T, V] {
return Mapable[T, V]{seq: Sort(g.seq, sortFunc)}
}

0 comments on commit 6a847ce

Please sign in to comment.