Skip to content

Commit

Permalink
fix: auto generate SequenceT 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 20, 2023
1 parent 680c103 commit 9e32acf
Show file tree
Hide file tree
Showing 21 changed files with 5,045 additions and 4,657 deletions.
27 changes: 8 additions & 19 deletions array/array.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,13 @@ func From[A any](data ...A) []A {
}

// MakeBy returns a `Array` of length `n` with element `i` initialized with `f(i)`.
func MakeBy[A any](n int, f func(int) A) []A {
// sanity check
if n <= 0 {
return Empty[A]()
}
// run the generator function across the input
as := make([]A, n)
for i := n - 1; i >= 0; i-- {
as[i] = f(i)
}
return as
func MakeBy[F ~func(int) A, A any](n int, f F) []A {
return G.MakeBy[[]A](n, f)
}

// Replicate creates a `Array` containing a value repeated the specified number of times.
func Replicate[A any](n int, a A) []A {
return MakeBy(n, F.Constant1[int](a))
return G.Replicate[[]A](n, a)
}

func MonadMap[A, B any](as []A, f func(a A) B) []B {
Expand Down Expand Up @@ -159,21 +150,19 @@ func Of[A any](a A) []A {
}

func MonadChain[A, B any](fa []A, f func(a A) []B) []B {
return array.Reduce(fa, func(bs []B, a A) []B {
return append(bs, f(a)...)
}, Zero[B]())
return G.MonadChain[[]A, []B](fa, f)
}

func Chain[A, B any](f func(a A) []B) func([]A) []B {
return F.Bind2nd(MonadChain[A, B], f)
func Chain[A, B any](f func(A) []B) func([]A) []B {
return G.Chain[[]A, []B](f)
}

func MonadAp[B, A any](fab []func(A) B, fa []A) []B {
return MonadChain(fab, F.Bind1st(MonadMap[A, B], fa))
return G.MonadAp[[]B](fab, fa)
}

func Ap[B, A any](fa []A) func([]func(A) B) []B {
return F.Bind2nd(MonadAp[B, A], fa)
return G.Ap[[]B, []func(A) B](fa)
}

func Match[A, B any](onEmpty func() B, onNonEmpty func([]A) B) func([]A) B {
Expand Down
36 changes: 36 additions & 0 deletions array/generic/array.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,24 @@ func From[GA ~[]A, A any](data ...A) GA {
return data
}

// MakeBy returns a `Array` of length `n` with element `i` initialized with `f(i)`.
func MakeBy[AS ~[]A, F ~func(int) A, A any](n int, f F) AS {
// sanity check
if n <= 0 {
return Empty[AS]()
}
// run the generator function across the input
as := make(AS, n)
for i := n - 1; i >= 0; i-- {
as[i] = f(i)
}
return as
}

func Replicate[AS ~[]A, A any](n int, a A) AS {
return MakeBy[AS](n, F.Constant1[int](a))
}

func Lookup[GA ~[]A, A any](idx int) func(GA) O.Option[A] {
none := O.None[A]()
if idx < 0 {
Expand Down Expand Up @@ -107,3 +125,21 @@ func MonadPartition[GA ~[]A, A any](as GA, pred func(A) bool) tuple.Tuple2[GA, G
func Partition[GA ~[]A, A any](pred func(A) bool) func(GA) tuple.Tuple2[GA, GA] {
return F.Bind2nd(MonadPartition[GA, A], pred)
}

func MonadChain[AS ~[]A, BS ~[]B, A, B any](fa AS, f func(a A) BS) BS {
return array.Reduce(fa, func(bs BS, a A) BS {
return append(bs, f(a)...)
}, Empty[BS]())
}

func Chain[AS ~[]A, BS ~[]B, A, B any](f func(A) BS) func(AS) BS {
return F.Bind2nd(MonadChain[AS, BS, A, B], f)
}

func MonadAp[BS ~[]B, ABS ~[]func(A) B, AS ~[]A, B, A any](fab ABS, fa AS) BS {
return MonadChain(fab, F.Bind1st(MonadMap[AS, BS, A, B], fa))
}

func Ap[BS ~[]B, ABS ~[]func(A) B, AS ~[]A, B, A any](fa AS) func(ABS) BS {
return F.Bind2nd(MonadAp[BS, ABS, AS], fa)
}
113 changes: 113 additions & 0 deletions cli/contextreaderioeither.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,118 @@ import (
"log"
"os"
"path/filepath"
"strings"

C "github.com/urfave/cli/v2"
)

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

func generateContextReaderIOEitherSequenceT(f, fg *os.File, i int) {
// tuple type
tuple := tupleType(i)

// non-generic version
// generic version
fmt.Fprintf(f, "\n// SequenceT%d converts %d [ReaderIOEither] into a [ReaderIOEither] of a [T.Tuple%d].\n", i, i, i)
fmt.Fprintf(f, "func SequenceT%d[", i)
for j := 0; j < i; j++ {
if j > 0 {
fmt.Fprintf(f, ", ")
}
fmt.Fprintf(f, "T%d", j+1)
}
fmt.Fprintf(f, " any](")
for j := 0; j < i; j++ {
if j > 0 {
fmt.Fprintf(f, ", ")
}
fmt.Fprintf(f, "t%d ReaderIOEither[T%d]", j+1, j+1)
}
fmt.Fprintf(f, ") ReaderIOEither[%s] {\n", tuple)
fmt.Fprintf(f, " return G.SequenceT%d[ReaderIOEither[%s]](", i, tuple)
for j := 0; j < i; j++ {
if j > 0 {
fmt.Fprintf(f, ", ")
}
fmt.Fprintf(f, "t%d", j+1)
}
fmt.Fprintf(f, ")\n")
fmt.Fprintf(f, "}\n")

// generic version
fmt.Fprintf(fg, "\n// SequenceT%d converts %d readers into a reader of a [T.Tuple%d].\n", i, i, i)
fmt.Fprintf(fg, "func SequenceT%d[\n", i)

fmt.Fprintf(fg, " GR_TUPLE%d ~func(context.Context) GIO_TUPLE%d,\n", i, i)
for j := 0; j < i; j++ {
fmt.Fprintf(fg, " GR_T%d ~func(context.Context) GIO_T%d,\n", j+1, j+1)
}

fmt.Fprintf(fg, " GIO_TUPLE%d ~func() E.Either[error, %s],\n", i, tuple)
for j := 0; j < i; j++ {
fmt.Fprintf(fg, " GIO_T%d ~func() E.Either[error, T%d],\n", j+1, j+1)
}

for j := 0; j < i; j++ {
fmt.Fprintf(fg, " T%d", j+1)
if j < i-1 {
fmt.Fprintf(fg, ",\n")
}
}
fmt.Fprintf(fg, " any](\n")
for j := 0; j < i; j++ {
fmt.Fprintf(fg, " t%d GR_T%d,\n", j+1, j+1)
}
fmt.Fprintf(fg, ") GR_TUPLE%d {\n", i)
fmt.Fprintf(fg, " return A.SequenceT%d(\n", i)
// map call
var cr string
if i > 1 {
cb := generateNestedCallbacks(1, i)
cio := fmt.Sprintf("func() E.Either[error, %s]", cb)
cr = fmt.Sprintf("func(context.Context) %s", cio)
} else {
cr = fmt.Sprintf("GR_TUPLE%d", i)
}
fmt.Fprintf(fg, " Map[GR_T%d, %s, GIO_T%d],\n", 1, cr, 1)
// the apply calls
for j := 1; j < i; j++ {
if j < i-1 {
cb := generateNestedCallbacks(j+1, i)
cio := fmt.Sprintf("func() E.Either[error, %s]", cb)
cr = fmt.Sprintf("func(context.Context) %s", cio)
} else {
cr = fmt.Sprintf("GR_TUPLE%d", i)
}
fmt.Fprintf(fg, " Ap[%s, func(context.Context) func() E.Either[error, %s], GR_T%d],\n", cr, generateNestedCallbacks(j, i), j+1)
}
// raw parameters
for j := 0; j < i; j++ {
fmt.Fprintf(fg, " t%d,\n", j+1)
}
// // map
// Ap[GB, func(E) func() ET.Either[L, func(C) T.Tuple3[A, B, C]], func(E) func() ET.Either[L, func(B) func(C) T.Tuple3[A, B, C]], GIOB, func() ET.Either[L, func(C) T.Tuple3[A, B, C]], func() ET.Either[L, func(B) func(C) T.Tuple3[A, B, C]], E, L, B, func(C) T.Tuple3[A, B, C]],
// Ap[GC, GTABC, func(E) func() ET.Either[L, func(C) T.Tuple3[A, B, C]], GIOC, GIOTABC, func() ET.Either[L, func(C) T.Tuple3[A, B, C]], E, L, C, T.Tuple3[A, B, C]],

// a, b, c,
fmt.Fprintf(fg, " )\n")
fmt.Fprintf(fg, "}\n")
}

func generateContextReaderIOEitherEitherize(f, fg *os.File, i int) {
// non generic version
fmt.Fprintf(f, "\n// Eitherize%d converts a function with %d parameters returning a tuple into a function with %d parameters returning a [ReaderIOEither[R]]\n// The inverse function is [Uneitherize%d]\n", i, i, i, i)
Expand Down Expand Up @@ -90,6 +198,7 @@ import (
"context"
G "github.com/IBM/fp-go/context/%s/generic"
T "github.com/IBM/fp-go/tuple"
)
`, pkg)

Expand All @@ -101,6 +210,8 @@ import (
E "github.com/IBM/fp-go/either"
RE "github.com/IBM/fp-go/readerioeither/generic"
A "github.com/IBM/fp-go/internal/apply"
T "github.com/IBM/fp-go/tuple"
)
`)

Expand All @@ -109,6 +220,8 @@ import (
for i := 1; i <= count; i++ {
// eitherize
generateContextReaderIOEitherEitherize(f, fg, i)
// sequenceT
generateContextReaderIOEitherSequenceT(f, fg, i)
}

return nil
Expand Down
17 changes: 16 additions & 1 deletion context/readerioeither/doc.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
// Package readerioeither contains a version of [ReaderIOEither] that takes a golang [context.Context] as its context
// Copyright (c) 2023 IBM Corp.
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package readerioeither contains a version of [ReaderIOEither] that takes a golang [context.Context] as its context and that assumes the standard go error
package readerioeither

//go:generate go run ../.. contextreaderioeither --count 10 --filename gen.go
Loading

0 comments on commit 9e32acf

Please sign in to comment.