Collections #
In go-optics Collection provides an immutable wrapper around a concrete collection like a slice or map. Go-optics also offers collection operations that modify these. In conjunction ths enables these generic operations to be performed on arbitrary concrete collection types.
Collection Conversions #
By convention a ___ToCol Iso is used to convert to and from a Collection. The following built in collection Isos are provided
SliceToColfor slicesMapToColfor mapsStringToColfor strings
For convenience the inverse functions are also provided.
ColToSlicefor slicesColToMapfor mapsColToStringfor strings
Operations #
The built in collection operations can then be used to modify a Collection.
| |
This example prints
| |
Note that the collection operation was applied and the result converted back to a []int.
The makelens tool automatically converts slices and maps to Collection to ensure immutability.
| |
The following collection operations are provided.
EqColcompares a focused collection with a given collectionDiffColdiffs a focused collection with a given collectionFilteredColremoves elements of a collection based on aPredicate.AppendColadds elements to the end of a collection.PrependColadds elements to the start of a collection.SubColreturns a sub collection with a given start positions and length.ReversedColreverses the order of a collection.OrderedColsorts the collection according to anOrderByPredicate
All operations are compatible with the Modify action. For example we can change the collection operation in the first example to FilteredCol
| |
This example now prints.
| |
Mismatched Collection Error types #
The collection type has the following signature.
type Collection[I,A,ERR] interface{...}
In addition to the expected index and value type parameters, I and A, Collection has a 3rd type parameter ERR to track whether the Collection may raise an error during iteration. The Modify action needs to iterate the Collection this means that the ERR of the Collection must match the ERR of the Optic. This causes issues when the error types of the [Optic] and [Collection] don’t match.
| |
In this example the FilteredCol operations is an
Optic[Void, Collection[int, string, Err], Collection[int, string, Err], Collection[int, string, Err], Collection[int, string, Err], ReturnOne, ReadWrite, UniDir, Err]
The source and focus collection error types are Err.
The built int collection conversions all return an error type of Pure and tying to us them together in a Modify action would cause a compile error. Go-optics provides collection error reconstraints to solve this compatibility issue.
| |
SliceToCol has is an Optic[int,[]string,[]string,Collection[int,string.Pure],Collection[int,string.Pure],ReturnOne,ReadWrite,BiDir,Pure]
We need some way to change the focused collection error type to Err. The ColFocusErr provides this function. It takes an optic of type
Optic[J, S, T, Collection[I, A, OAERR], Collection[I, B, OBERR], RET, RW, DIR, ERR]
Where OAERR and OERR and ERR are any type and converts the optic to.
Optic[J, S, T, Collection[I, A, Err], Collection[I, B, Err], RET, RW, DIR, Err]
Effectively making the optic compatible with the OrderedCol optic that has a source collection error type of Err .
By normalising the source,focus or both to either Pure or Err. Collection optics can be made compatible with one another.
The following collection error type reconstraints are provided.
| Function | Description |
|---|---|
ColSourcePure | Convert the source collection error type to Pure |
ColFocusPure | Convert the focus collection error type to Pure |
ColSourceFocusPure | Convert the source and focus collection error type to Pure |
ColSourceErr | Convert the source collection error type to Err |
ColFocusErr | Convert the focus collection error type to Err |
ColSourceFocusErr | Convert the source and focus collection error type to Err |
There are 2 additional reconstraints that operate directly on an instance of a Collection |
ColErrwhich converts aCollection[I,A,any]to aCollection[I,A,Err]ColPurewhich converts aCollection[I,A,TPure]to aCollection[I,A,Pure]
This example shows how to use ColErr
| |
Together these functions enable the collection error types of any Collection or collection operation Optic to be normalized to Pure or Err error types.