Skip to content

Commit

Permalink
fix: auto generate TraverseTuple for ReaderIOEither
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 21, 2023
1 parent ff7d750 commit 91d7961
Show file tree
Hide file tree
Showing 29 changed files with 4,155 additions and 489 deletions.
395 changes: 257 additions & 138 deletions cli/contextreaderioeither.go

Large diffs are not rendered by default.

34 changes: 18 additions & 16 deletions cli/monad.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@ import (
"strings"
)

func tupleType(i int) string {
var buf strings.Builder
buf.WriteString(fmt.Sprintf("T.Tuple%d[", i))
for j := 0; j < i; j++ {
if j > 0 {
buf.WriteString(", ")
}
buf.WriteString(fmt.Sprintf("T%d", j+1))
}
buf.WriteString("]")
func tupleType(name string) func(i int) string {
return func(i int) string {
var buf strings.Builder
buf.WriteString(fmt.Sprintf("T.Tuple%d[", i))
for j := 0; j < i; j++ {
if j > 0 {
buf.WriteString(", ")
}
buf.WriteString(fmt.Sprintf("%s%d", name, j+1))
}
buf.WriteString("]")

return buf.String()
return buf.String()
}
}

func monadGenerateSequenceTNonGeneric(
Expand All @@ -27,7 +29,7 @@ func monadGenerateSequenceTNonGeneric(
) func(f *os.File, i int) {
return func(f *os.File, i int) {

tuple := tupleType(i)
tuple := tupleType("T")(i)

fmt.Fprintf(f, "SequenceT%d[", i)
for j := 0; j < i; j++ {
Expand Down Expand Up @@ -80,7 +82,7 @@ func monadGenerateSequenceTGeneric(
) func(f *os.File, i int) {
return func(f *os.File, i int) {

tuple := tupleType(i)
tuple := tupleType("T")(i)

fmt.Fprintf(f, "SequenceT%d[", i)
for j := 0; j < i; j++ {
Expand Down Expand Up @@ -131,7 +133,7 @@ func generateTraverseTuple1(
infix string) func(f *os.File, i int) {

return func(f *os.File, i int) {
tuple := tupleType(i)
tuple := tupleType("T")(i)

fmt.Fprintf(f, "\n// TraverseTuple%d converts a [Tuple%d] of [A] via transformation functions transforming [A] to [%s] into a [%s].\n", i, i, hkt("A"), hkt(fmt.Sprintf("Tuple%d", i)))
fmt.Fprintf(f, "func TraverseTuple%d[", i)
Expand Down Expand Up @@ -218,7 +220,7 @@ func generateSequenceTuple1(

return func(f *os.File, i int) {

tuple := tupleType(i)
tuple := tupleType("T")(i)

fmt.Fprintf(f, "\n// SequenceTuple%d converts a [Tuple%d] of [%s] into an [%s].\n", i, i, hkt("T"), hkt(fmt.Sprintf("Tuple%d", i)))
fmt.Fprintf(f, "func SequenceTuple%d[", i)
Expand Down Expand Up @@ -281,7 +283,7 @@ func generateSequenceT1(

return func(f *os.File, i int) {

tuple := tupleType(i)
tuple := tupleType("T")(i)

fmt.Fprintf(f, "\n// SequenceT%d converts %d parameters of [%s] into a [%s].\n", i, i, hkt("T"), hkt(fmt.Sprintf("Tuple%d", i)))
fmt.Fprintf(f, "func SequenceT%d[", i)
Expand Down
2 changes: 1 addition & 1 deletion context/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Package context contains versions of monads that work with a golang [context.Context]
// Package context contains versions of reader IO monads that work with a golang [context.Context]
package context
3 changes: 3 additions & 0 deletions context/readerioeither/file/data/file.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"data": "Carsten"
}
41 changes: 41 additions & 0 deletions context/readerioeither/file/file_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package file

import (
"context"
"fmt"

RIO "github.com/IBM/fp-go/context/readerio"
R "github.com/IBM/fp-go/context/readerioeither"
"github.com/IBM/fp-go/errors"
F "github.com/IBM/fp-go/function"
IO "github.com/IBM/fp-go/io"
J "github.com/IBM/fp-go/json"
)

type RecordType struct {
Data string `json:"data"`
}

func getData(r RecordType) string {
return r.Data
}

func ExampleReadFile() {

data := F.Pipe4(
ReadFile("./data/file.json"),
R.ChainEitherK(J.Unmarshal[RecordType]),
R.ChainFirstIOK(IO.Logf[RecordType]("Log: %v")),
R.Map(getData),
R.GetOrElse(F.Flow2(
errors.ToString,
RIO.Of[string],
)),
)

result := data(context.Background())

fmt.Println(result())

// Output: Carsten
}
352 changes: 351 additions & 1 deletion context/readerioeither/gen.go

Large diffs are not rendered by default.

Loading

0 comments on commit 91d7961

Please sign in to comment.