Skip to content

Commit

Permalink
fix: doc and parameter order
Browse files Browse the repository at this point in the history
Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
  • Loading branch information
CarstenLeue committed Jul 19, 2023
1 parent 34bf363 commit 7983454
Show file tree
Hide file tree
Showing 22 changed files with 91 additions and 34 deletions.
48 changes: 36 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ This library aims to provide a set of data types and functions that make it easy

#### 🧘🏽 Each package fulfils a single purpose

✔️ Each of the top level packages (e.g. Option, Either, Task, ...) fulfils the purpose of defining the respective data type and implementing the set of common operations for this data type.
✔️ Each of the top level packages (e.g. Option, Either, ReaderIOEither, ...) fulfils the purpose of defining the respective data type and implementing the set of common operations for this data type.

#### 🧘🏽 Handle errors explicitly

Expand All @@ -41,7 +41,7 @@ This library aims to provide a set of data types and functions that make it easy

#### 🧘🏽 Leave concurrency to the caller

✔️ All operations are synchronous by default, including `Task`. Concurrency must be coded by the consumer of these functions explicitly, but the implementation is ready to deal with concurrent usage.
✔️ All pure are synchronous by default. The I/O operations are asynchronous per default.

#### 🧘🏽 Before you launch a goroutine, know when it will stop

Expand All @@ -65,7 +65,7 @@ This library aims to provide a set of data types and functions that make it easy

#### 🧘🏽 Moderation is a virtue

✔️ The library does not implement its own goroutines and also does not require any expensive synchronization primitives. Coordination of Tasks is implemented via atomic counters without additional primitives. Channels are only used in the `Wait` function of a Task that should be invoked at most once in a complete application.
✔️ The library does not implement its own goroutines and also does not require any expensive synchronization primitives. Coordination of IO operations is implemented via atomic counters without additional primitives.

#### 🧘🏽 Maintainability counts

Expand All @@ -81,33 +81,57 @@ All monadic operations are implemented via generics, i.e. they offer a type safe

Downside is that this will result in different versions of each operation per type, these versions are generated by the golang compiler at build time (unlike type erasure in languages such as Java of TypeScript). This might lead to large binaries for codebases with many different types. If this is a concern, you can always implement type erasure on top, i.e. use the monadic operations with the `any` type as if generics were not supported. You loose type safety, but this might result in smaller binaries.

### Ordering of Generic Type Parameters

In go we need to specify all type parameters of a function on the global function definition, even if the function returns a higher order function and some of the type parameters are only applicable to the higher order function. So the following is not possible:

```go
func Map[A, B any](f func(A) B) [R, E any]func(fa ReaderIOEither[R, E, A]) ReaderIOEither[R, E, B]
```

Note that the parameters `R` and `E` are not needed by the first level of `Map` but only by the resulting higher order function. Instead we need to specify the following:

```go
func Map[R, E, A, B any](f func(A) B) func(fa ReaderIOEither[R, E, A]) ReaderIOEither[R, E, B]
```

which overspecifies `Map` on the global scope. As a result the go compiler will not be able to auto-detect these parameters, it can only auto detect `A` and `B` since they appear in the argument of `Map`. We need to explicitly pass values for these type parameters when `Map` is being used.

Because of this limitation the order of parameters on a function matters. We want to make sure that we define those parameters that cannot be auto-detected, first, and the parameters that can be auto-detected, last. This can lead to inconsistencies in parameter ordering, but we believe that the gain in convenience is worth it. The parameter order of `Ap` is e.g. different from that of `Map`:

```go
func Ap[B, R, E, A any](fa ReaderIOEither[R, E, A]) func(fab ReaderIOEither[R, E, func(A) B]) ReaderIOEither[R, E, B]
```

because `R`, `E` and `A` can be determined from the argument to `Ap` but `B` cannot.

### Use of the [~ Operator](https://go.googlesource.com/proposal/+/master/design/47781-parameterized-go-ast.md)

The FP library attempts to be easy to consume and one aspect of this is the definition of higher level type definitions instead of having to use their low level equivalent. It is e.g. more convenient and readable to use

```go
TaskEither[E, A]
ReaderIOEither[R, E, A]
```

than

```go
func(func(Either.Either[E, A]))
func(R) func() Either.Either[E, A]
```

although both are logically equivalent. At the time of this writing the go type system does not support generic type aliases, only generic type definition, i.e. it is not possible to write:

```go
type TaskEither[E, A any] = T.Task[ET.Either[E, A]]
type ReaderIOEither[R, E, A any] = RD.Reader[R, IOE.IOEither[E, A]]
```

only

```go
type TaskEither[E, A any] T.Task[ET.Either[E, A]]
type ReaderIOEither[R, E, A any] RD.Reader[R, IOE.IOEither[E, A]]
```

This makes a big difference, because in the second case the type `TaskEither[E, A any]` is considered a completely new type, not compatible to its right hand side, so it's not just a shortcut but a fully new type.
This makes a big difference, because in the second case the type `ReaderIOEither[R, E, A any]` is considered a completely new type, not compatible to its right hand side, so it's not just a shortcut but a fully new type.

From the implementation perspective however there is no reason to restrict the implementation to the new type, it can be generic for all compatible types. The way to express this in go is the [~](https://go.googlesource.com/proposal/+/master/design/47781-parameterized-go-ast.md) operator. This comes with some quite complicated type declarations in some cases, which undermines the goal of the library to be easy to use.

Expand All @@ -117,16 +141,16 @@ For that reason there exist sub-packages called `Generic` for all higher level t

Go does not support higher kinded types (HKT). Such types occur if a generic type itself is parametrized by another generic type. Example:

The `Map` operation for `Task` is defined as:
The `Map` operation for `ReaderIOEither` is defined as:

```go
func Map[A, B any](f func(A) B) func(Task[A]) Task[B]
func Map[R, E, A, B any](f func(A) B) func(fa ReaderIOEither[R, E, A]) ReaderIOEither[R, E, B]
```

and in fact the equivalent operations for all other mondas follow the same pattern, we could try to introduce a new type for `Task` (without a parameter) as a HKT, e.g. like so (made-up syntax, does not work in go):
and in fact the equivalent operations for all other mondas follow the same pattern, we could try to introduce a new type for `ReaderIOEither` (without a parameter) as a HKT, e.g. like so (made-up syntax, does not work in go):

```go
func Map[HKT, A, B any](f func(A) B) func(HKT[A]) HKT[B]
func Map[HKT, R, E, A, B any](f func(A) B) func(HKT[R, E, A]) HKT[R, E, B]
```

this would be the completely generic method signature for all possible monads. In particular in many cases it is possible to compose functions independent of the concrete knowledge of the actual `HKT`. From the perspective of a library this is the ideal situation because then a particular algorithm only has to be implemented and tested once.
Expand Down
6 changes: 3 additions & 3 deletions context/readerioeither/gen.go

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

3 changes: 1 addition & 2 deletions context/readerioeither/generic/eq.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import (
"context"

E "github.com/IBM/fp-go/either"
ET "github.com/IBM/fp-go/either"
EQ "github.com/IBM/fp-go/eq"
G "github.com/IBM/fp-go/readerioeither/generic"
)

// Eq implements the equals predicate for values contained in the IOEither monad
func Eq[GRA ~func(context.Context) GIOA, GIOA ~func() E.Either[error, A], A any](eq EQ.Eq[ET.Either[error, A]]) func(context.Context) EQ.Eq[GRA] {
func Eq[GRA ~func(context.Context) GIOA, GIOA ~func() E.Either[error, A], A any](eq EQ.Eq[E.Either[error, A]]) func(context.Context) EQ.Eq[GRA] {
return G.Eq[GRA](eq)
}
6 changes: 3 additions & 3 deletions context/readerioeither/generic/gen.go

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

2 changes: 1 addition & 1 deletion either/gen.go

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

2 changes: 1 addition & 1 deletion function/binds.go

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

2 changes: 1 addition & 1 deletion function/gen.go

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

2 changes: 1 addition & 1 deletion identity/gen.go

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

2 changes: 1 addition & 1 deletion internal/apply/gen.go

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

2 changes: 1 addition & 1 deletion option/gen.go

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

2 changes: 1 addition & 1 deletion reader/gen.go

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

2 changes: 1 addition & 1 deletion reader/generic/gen.go

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

2 changes: 1 addition & 1 deletion readerioeither/gen.go

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

2 changes: 1 addition & 1 deletion readerioeither/generic/gen.go

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

2 changes: 1 addition & 1 deletion readerioeither/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func MonadAp[R, E, A, B any](fab ReaderIOEither[R, E, func(A) B], fa ReaderIOEit
return G.MonadAp[ReaderIOEither[R, E, A], ReaderIOEither[R, E, B]](fab, fa)
}

func Ap[R, E, A, B any](fa ReaderIOEither[R, E, A]) func(fab ReaderIOEither[R, E, func(A) B]) ReaderIOEither[R, E, B] {
func Ap[B, R, E, A any](fa ReaderIOEither[R, E, A]) func(fab ReaderIOEither[R, E, func(A) B]) ReaderIOEither[R, E, B] {
return G.Ap[ReaderIOEither[R, E, A], ReaderIOEither[R, E, B], ReaderIOEither[R, E, func(A) B]](fa)
}

Expand Down
2 changes: 1 addition & 1 deletion readerioeither/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestOrLeft(t *testing.T) {
func TestAp(t *testing.T) {
g := F.Pipe1(
Right[context.Context, error](utils.Double),
Ap[context.Context, error, int, int](Right[context.Context, error](1)),
Ap[int](Right[context.Context, error](1)),
)

assert.Equal(t, E.Right[error](2), g(context.Background())())
Expand Down
File renamed without changes.
3 changes: 3 additions & 0 deletions samples/readfile/data/file1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"data": "file1"
}
3 changes: 3 additions & 0 deletions samples/readfile/data/file2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"data": "file2"
}
3 changes: 3 additions & 0 deletions samples/readfile/data/file3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"data": "file3"
}
27 changes: 26 additions & 1 deletion samples/readfile/readfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package readfile

import (
"context"
"fmt"
"testing"

A "github.com/IBM/fp-go/array"
R "github.com/IBM/fp-go/context/readerioeither"
"github.com/IBM/fp-go/context/readerioeither/file"
E "github.com/IBM/fp-go/either"
Expand All @@ -22,7 +24,7 @@ type RecordType struct {
func TestReadSingleFile(t *testing.T) {

data := F.Pipe2(
file.ReadFile("./file.json"),
file.ReadFile("./data/file.json"),
R.ChainEitherK(J.Unmarshal[RecordType]),
R.ChainFirstIOK(IO.Logf[RecordType]("Log: %v")),
)
Expand All @@ -31,3 +33,26 @@ func TestReadSingleFile(t *testing.T) {

assert.Equal(t, E.Of[error](RecordType{"Carsten"}), result())
}

func idxToFilename(idx int) string {
return fmt.Sprintf("./data/file%d.json", idx+1)
}

// TestReadMultipleFiles reads the content of a multiple from disk and parses them into
// structs
func TestReadMultipleFiles(t *testing.T) {

data := F.Pipe2(
A.MakeBy(3, idxToFilename),
R.TraverseArray(F.Flow3(
file.ReadFile,
R.ChainEitherK(J.Unmarshal[RecordType]),
R.ChainFirstIOK(IO.Logf[RecordType]("Log Single: %v")),
)),
R.ChainFirstIOK(IO.Logf[[]RecordType]("Log Result: %v")),
)

result := data(context.Background())

assert.Equal(t, E.Of[error](A.From(RecordType{"file1"}, RecordType{"file2"}, RecordType{"file3"})), result())
}
2 changes: 1 addition & 1 deletion tuple/gen.go

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

0 comments on commit 7983454

Please sign in to comment.