mirror of
https://github.com/IBM/fp-go.git
synced 2025-08-10 22:31:32 +02:00
fix: optimize use of tuples
Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
This commit is contained in:
40
cli/tuple.go
40
cli/tuple.go
@@ -405,8 +405,6 @@ func generateTupleHelpers(filename string, count int) error {
|
|||||||
|
|
||||||
fmt.Fprintf(f, `
|
fmt.Fprintf(f, `
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"encoding/json"
|
|
||||||
M "github.com/IBM/fp-go/monoid"
|
M "github.com/IBM/fp-go/monoid"
|
||||||
O "github.com/IBM/fp-go/ord"
|
O "github.com/IBM/fp-go/ord"
|
||||||
)
|
)
|
||||||
@@ -457,7 +455,7 @@ func generateTupleMarshal(f *os.File, i int) {
|
|||||||
fmt.Fprintf(f, "func (t ")
|
fmt.Fprintf(f, "func (t ")
|
||||||
writeTupleType(f, "T", i)
|
writeTupleType(f, "T", i)
|
||||||
fmt.Fprintf(f, ") MarshalJSON() ([]byte, error) {\n")
|
fmt.Fprintf(f, ") MarshalJSON() ([]byte, error) {\n")
|
||||||
fmt.Fprintf(f, " return json.Marshal([]any{")
|
fmt.Fprintf(f, " return tupleMarshalJSON(")
|
||||||
// function prototypes
|
// function prototypes
|
||||||
for j := 1; j <= i; j++ {
|
for j := 1; j <= i; j++ {
|
||||||
if j > 1 {
|
if j > 1 {
|
||||||
@@ -465,7 +463,7 @@ func generateTupleMarshal(f *os.File, i int) {
|
|||||||
}
|
}
|
||||||
fmt.Fprintf(f, "t.F%d", j)
|
fmt.Fprintf(f, "t.F%d", j)
|
||||||
}
|
}
|
||||||
fmt.Fprintf(f, "})\n")
|
fmt.Fprintf(f, ")\n")
|
||||||
fmt.Fprintf(f, "}\n")
|
fmt.Fprintf(f, "}\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -475,19 +473,12 @@ func generateTupleUnmarshal(f *os.File, i int) {
|
|||||||
fmt.Fprintf(f, "func (t *")
|
fmt.Fprintf(f, "func (t *")
|
||||||
writeTupleType(f, "T", i)
|
writeTupleType(f, "T", i)
|
||||||
fmt.Fprintf(f, ") UnmarshalJSON(data []byte) error {\n")
|
fmt.Fprintf(f, ") UnmarshalJSON(data []byte) error {\n")
|
||||||
fmt.Fprintf(f, " var tmp []json.RawMessage\n")
|
fmt.Fprintf(f, " return tupleUnmarshalJSON(data")
|
||||||
fmt.Fprintf(f, " if err := json.Unmarshal(data, &tmp); err != nil {return err}\n")
|
// function prototypes
|
||||||
fmt.Fprintf(f, " l := len(tmp)\n")
|
|
||||||
// unmarshal fields
|
|
||||||
for j := 1; j <= i; j++ {
|
for j := 1; j <= i; j++ {
|
||||||
fmt.Fprintf(f, " if l > %d {\n", j-1)
|
fmt.Fprintf(f, ", &t.F%d", j)
|
||||||
fmt.Fprintf(f, " if err := json.Unmarshal(tmp[%d], &t.F%d); err != nil {return err}\n", j-1, j)
|
|
||||||
}
|
}
|
||||||
fmt.Fprintf(f, " ")
|
fmt.Fprintf(f, ")\n")
|
||||||
for j := 1; j <= i; j++ {
|
|
||||||
fmt.Fprintf(f, "}")
|
|
||||||
}
|
|
||||||
fmt.Fprintf(f, "\n return nil\n")
|
|
||||||
fmt.Fprintf(f, "}\n")
|
fmt.Fprintf(f, "}\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -570,30 +561,13 @@ func generateTupleString(f *os.File, i int) {
|
|||||||
writeTupleType(f, "T", i)
|
writeTupleType(f, "T", i)
|
||||||
fmt.Fprintf(f, ") String() string {\n")
|
fmt.Fprintf(f, ") String() string {\n")
|
||||||
// convert to string
|
// convert to string
|
||||||
fmt.Fprintf(f, " return fmt.Sprintf(\"Tuple%d[", i)
|
fmt.Fprint(f, " return tupleString(")
|
||||||
for j := 1; j <= i; j++ {
|
|
||||||
if j > 1 {
|
|
||||||
fmt.Fprintf(f, ", ")
|
|
||||||
}
|
|
||||||
fmt.Fprintf(f, "%s", "%T")
|
|
||||||
}
|
|
||||||
fmt.Fprintf(f, "](")
|
|
||||||
for j := 1; j <= i; j++ {
|
|
||||||
if j > 1 {
|
|
||||||
fmt.Fprintf(f, ", ")
|
|
||||||
}
|
|
||||||
fmt.Fprintf(f, "%s", "%v")
|
|
||||||
}
|
|
||||||
fmt.Fprintf(f, ")\", ")
|
|
||||||
for j := 1; j <= i; j++ {
|
for j := 1; j <= i; j++ {
|
||||||
if j > 1 {
|
if j > 1 {
|
||||||
fmt.Fprintf(f, ", ")
|
fmt.Fprintf(f, ", ")
|
||||||
}
|
}
|
||||||
fmt.Fprintf(f, "t.F%d", j)
|
fmt.Fprintf(f, "t.F%d", j)
|
||||||
}
|
}
|
||||||
for j := 1; j <= i; j++ {
|
|
||||||
fmt.Fprintf(f, ", t.F%d", j)
|
|
||||||
}
|
|
||||||
fmt.Fprintf(f, ")\n")
|
fmt.Fprintf(f, ")\n")
|
||||||
fmt.Fprintf(f, "}\n")
|
fmt.Fprintf(f, "}\n")
|
||||||
}
|
}
|
||||||
|
1666
tuple/gen.go
1666
tuple/gen.go
File diff suppressed because it is too large
Load Diff
@@ -17,6 +17,14 @@
|
|||||||
// consider to use arrays for simplicity
|
// consider to use arrays for simplicity
|
||||||
package tuple
|
package tuple
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
N "github.com/IBM/fp-go/number"
|
||||||
|
)
|
||||||
|
|
||||||
func Of[T1 any](t T1) Tuple1[T1] {
|
func Of[T1 any](t T1) Tuple1[T1] {
|
||||||
return MakeTuple1(t)
|
return MakeTuple1(t)
|
||||||
}
|
}
|
||||||
@@ -46,3 +54,31 @@ func BiMap[E, G, A, B any](mapSnd func(E) G, mapFst func(A) B) func(Tuple2[A, E]
|
|||||||
return MakeTuple2(mapFst(First(t)), mapSnd(Second(t)))
|
return MakeTuple2(mapFst(First(t)), mapSnd(Second(t)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// marshalJSON marshals the tuple into a JSON array
|
||||||
|
func tupleMarshalJSON(src ...any) ([]byte, error) {
|
||||||
|
return json.Marshal(src)
|
||||||
|
}
|
||||||
|
|
||||||
|
// tupleUnmarshalJSON unmarshals a JSON array into a tuple
|
||||||
|
func tupleUnmarshalJSON(data []byte, dst ...any) error {
|
||||||
|
var src []json.RawMessage
|
||||||
|
if err := json.Unmarshal(data, &src); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
l := N.Min(len(src), len(dst))
|
||||||
|
// unmarshal
|
||||||
|
for i := 0; i < l; i++ {
|
||||||
|
if err := json.Unmarshal(src[i], dst[i]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// successfully decoded the tuple
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// tupleString converts a tuple to a string
|
||||||
|
func tupleString(src ...any) string {
|
||||||
|
l := len(src)
|
||||||
|
return fmt.Sprintf("Tuple%d[%s](%s)", l, fmt.Sprintf(strings.Repeat(", %T", l)[2:], src...), fmt.Sprintf(strings.Repeat(", %v", l)[2:], src...))
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user