mirror of
https://github.com/IBM/fp-go.git
synced 2025-08-10 22:31:32 +02:00
fix: initial release
Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
func Commands() []*C.Command {
|
||||
return []*C.Command{
|
||||
PipeCommand(),
|
||||
IdentityCommand(),
|
||||
OptionCommand(),
|
||||
EitherCommand(),
|
||||
TupleCommand(),
|
||||
|
@@ -25,48 +25,7 @@ func generateEitherSequenceTuple(f *os.File, i int) {
|
||||
}
|
||||
|
||||
func generateEitherSequenceT(f *os.File, i int) {
|
||||
|
||||
tuple := tupleType(i)
|
||||
|
||||
fmt.Fprintf(f, "\n// SequenceT%d converts %d parameters of [Either[E, T]] into an [Either] of a [Tuple%d].\n", i, i, i)
|
||||
fmt.Fprintf(f, "func SequenceT%d[E", i)
|
||||
for j := 0; j < i; j++ {
|
||||
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 Either[E, T%d]", j+1, j+1)
|
||||
}
|
||||
fmt.Fprintf(f, ") Either[E, %s] {\n", tuple)
|
||||
fmt.Fprintf(f, " return A.SequenceT%d(\n", i)
|
||||
// map
|
||||
fmt.Fprintf(f, " Map[E, T1,")
|
||||
for j := 1; j < i; j++ {
|
||||
fmt.Fprintf(f, " func(T%d)", j+1)
|
||||
}
|
||||
fmt.Fprintf(f, " %s],\n", tuple)
|
||||
// applicatives
|
||||
for j := 1; j < i; j++ {
|
||||
fmt.Fprintf(f, " Ap[")
|
||||
for k := j + 1; k < i; k++ {
|
||||
if k > j+1 {
|
||||
fmt.Fprintf(f, " ")
|
||||
}
|
||||
fmt.Fprintf(f, "func(T%d)", k+1)
|
||||
}
|
||||
if j < i-1 {
|
||||
fmt.Fprintf(f, " ")
|
||||
}
|
||||
fmt.Fprintf(f, "%s, E, T%d],\n", tuple, j+1)
|
||||
}
|
||||
for j := 0; j < i; j++ {
|
||||
fmt.Fprintf(f, " t%d,\n", j+1)
|
||||
}
|
||||
fmt.Fprintf(f, " )\n")
|
||||
fmt.Fprintf(f, "}\n")
|
||||
generateSequenceT1(eitherHKT("E"), "E")(f, i)
|
||||
}
|
||||
|
||||
func generateUneitherize(f *os.File, i int) {
|
||||
|
88
cli/identity.go
Normal file
88
cli/identity.go
Normal file
@@ -0,0 +1,88 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
C "github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
func identityHKT(typeA string) string {
|
||||
return typeA
|
||||
}
|
||||
|
||||
func generateIdentityTraverseTuple(f *os.File, i int) {
|
||||
generateTraverseTuple1(identityHKT, "")(f, i)
|
||||
}
|
||||
|
||||
func generateIdentitySequenceTuple(f *os.File, i int) {
|
||||
generateSequenceTuple1(identityHKT, "")(f, i)
|
||||
}
|
||||
|
||||
func generateIdentitySequenceT(f *os.File, i int) {
|
||||
generateSequenceT1(identityHKT, "")(f, i)
|
||||
}
|
||||
|
||||
func generateIdentityHelpers(filename string, count int) error {
|
||||
dir, err := os.Getwd()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
absDir, err := filepath.Abs(dir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pkg := filepath.Base(absDir)
|
||||
f, err := os.Create(filepath.Clean(filename))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
// log
|
||||
log.Printf("Generating code in [%s] for package [%s] with [%d] repetitions ...", filename, pkg, count)
|
||||
|
||||
// some header
|
||||
fmt.Fprintln(f, "// Code generated by go generate; DO NOT EDIT.")
|
||||
fmt.Fprintln(f, "// This file was generated by robots at")
|
||||
fmt.Fprintf(f, "// %s\n", time.Now())
|
||||
|
||||
fmt.Fprintf(f, "package %s\n\n", pkg)
|
||||
|
||||
fmt.Fprintf(f, `
|
||||
import (
|
||||
A "github.com/ibm/fp-go/internal/apply"
|
||||
T "github.com/ibm/fp-go/tuple"
|
||||
)
|
||||
`)
|
||||
|
||||
for i := 1; i <= count; i++ {
|
||||
// sequenceT
|
||||
generateIdentitySequenceT(f, i)
|
||||
// sequenceTuple
|
||||
generateIdentitySequenceTuple(f, i)
|
||||
// traverseTuple
|
||||
generateIdentityTraverseTuple(f, i)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func IdentityCommand() *C.Command {
|
||||
return &C.Command{
|
||||
Name: "identity",
|
||||
Usage: "generate code for Identity",
|
||||
Flags: []C.Flag{
|
||||
flagCount,
|
||||
flagFilename,
|
||||
},
|
||||
Action: func(ctx *C.Context) error {
|
||||
return generateIdentityHelpers(
|
||||
ctx.String(keyFilename),
|
||||
ctx.Int(keyCount),
|
||||
)
|
||||
},
|
||||
}
|
||||
}
|
66
cli/monad.go
66
cli/monad.go
@@ -274,3 +274,69 @@ func generateSequenceTuple1(
|
||||
fmt.Fprintf(f, "}\n")
|
||||
}
|
||||
}
|
||||
|
||||
func generateSequenceT1(
|
||||
hkt func(string) string,
|
||||
infix string) func(f *os.File, i int) {
|
||||
|
||||
return func(f *os.File, i int) {
|
||||
|
||||
tuple := tupleType(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)
|
||||
if infix != "" {
|
||||
fmt.Fprintf(f, "%s", infix)
|
||||
}
|
||||
for j := 0; j < i; j++ {
|
||||
if infix != "" || 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 %s", j+1, hkt(fmt.Sprintf("T%d", j+1)))
|
||||
}
|
||||
fmt.Fprintf(f, ") %s {\n", hkt(tuple))
|
||||
fmt.Fprintf(f, " return A.SequenceT%d(\n", i)
|
||||
// map
|
||||
fmt.Fprintf(f, " Map[")
|
||||
if infix != "" {
|
||||
fmt.Fprintf(f, "%s, T1,", infix)
|
||||
} else {
|
||||
fmt.Fprintf(f, "T1,")
|
||||
}
|
||||
for j := 1; j < i; j++ {
|
||||
fmt.Fprintf(f, " func(T%d)", j+1)
|
||||
}
|
||||
fmt.Fprintf(f, " %s],\n", tuple)
|
||||
// applicatives
|
||||
for j := 1; j < i; j++ {
|
||||
fmt.Fprintf(f, " Ap[")
|
||||
for k := j + 1; k < i; k++ {
|
||||
if k > j+1 {
|
||||
fmt.Fprintf(f, " ")
|
||||
}
|
||||
fmt.Fprintf(f, "func(T%d)", k+1)
|
||||
}
|
||||
if j < i-1 {
|
||||
fmt.Fprintf(f, " ")
|
||||
}
|
||||
fmt.Fprintf(f, "%s", tuple)
|
||||
if infix != "" {
|
||||
fmt.Fprintf(f, ", %s", infix)
|
||||
}
|
||||
fmt.Fprintf(f, ", T%d],\n", j+1)
|
||||
}
|
||||
for j := 0; j < i; j++ {
|
||||
fmt.Fprintf(f, " t%d,\n", j+1)
|
||||
}
|
||||
fmt.Fprintf(f, " )\n")
|
||||
fmt.Fprintf(f, "}\n")
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -22,6 +22,10 @@ func generateOptionSequenceTuple(f *os.File, i int) {
|
||||
generateSequenceTuple1(optionHKT, "")(f, i)
|
||||
}
|
||||
|
||||
func generateOptionSequenceT(f *os.File, i int) {
|
||||
generateSequenceT1(optionHKT, "")(f, i)
|
||||
}
|
||||
|
||||
func generateOptionize(f *os.File, i int) {
|
||||
// Create the optionize version
|
||||
fmt.Fprintf(f, "\n// Optionize%d converts a function with %d parameters returning a tuple of a return value R and a boolean into a function with %d parameters returning an Option[R]\n", i, i, i)
|
||||
@@ -162,6 +166,8 @@ func optionize[R any](f func() (R, bool)) Option[R] {
|
||||
generateOptionize(f, i)
|
||||
// unoptionize
|
||||
generateUnoptionize(f, i)
|
||||
// sequenceT
|
||||
generateOptionSequenceT(f, i)
|
||||
// sequenceTuple
|
||||
generateOptionSequenceTuple(f, i)
|
||||
// traverseTuple
|
||||
|
Reference in New Issue
Block a user