1
0
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:
Dr. Carsten Leue
2024-02-08 08:41:51 +01:00
parent 79652d8474
commit 9f6b6d4968
3 changed files with 658 additions and 1084 deletions

View File

@@ -405,8 +405,6 @@ func generateTupleHelpers(filename string, count int) error {
fmt.Fprintf(f, `
import (
"fmt"
"encoding/json"
M "github.com/IBM/fp-go/monoid"
O "github.com/IBM/fp-go/ord"
)
@@ -457,7 +455,7 @@ func generateTupleMarshal(f *os.File, i int) {
fmt.Fprintf(f, "func (t ")
writeTupleType(f, "T", i)
fmt.Fprintf(f, ") MarshalJSON() ([]byte, error) {\n")
fmt.Fprintf(f, " return json.Marshal([]any{")
fmt.Fprintf(f, " return tupleMarshalJSON(")
// function prototypes
for j := 1; j <= i; j++ {
if j > 1 {
@@ -465,7 +463,7 @@ func generateTupleMarshal(f *os.File, i int) {
}
fmt.Fprintf(f, "t.F%d", j)
}
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 *")
writeTupleType(f, "T", i)
fmt.Fprintf(f, ") UnmarshalJSON(data []byte) error {\n")
fmt.Fprintf(f, " var tmp []json.RawMessage\n")
fmt.Fprintf(f, " if err := json.Unmarshal(data, &tmp); err != nil {return err}\n")
fmt.Fprintf(f, " l := len(tmp)\n")
// unmarshal fields
fmt.Fprintf(f, " return tupleUnmarshalJSON(data")
// function prototypes
for j := 1; j <= i; j++ {
fmt.Fprintf(f, " if l > %d {\n", j-1)
fmt.Fprintf(f, " if err := json.Unmarshal(tmp[%d], &t.F%d); err != nil {return err}\n", j-1, j)
fmt.Fprintf(f, ", &t.F%d", j)
}
fmt.Fprintf(f, " ")
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)
fmt.Fprintf(f, ") String() string {\n")
// convert to string
fmt.Fprintf(f, " return fmt.Sprintf(\"Tuple%d[", i)
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, ")\", ")
fmt.Fprint(f, " return tupleString(")
for j := 1; j <= i; j++ {
if j > 1 {
fmt.Fprintf(f, ", ")
}
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")
}

File diff suppressed because it is too large Load Diff

View File

@@ -17,6 +17,14 @@
// consider to use arrays for simplicity
package tuple
import (
"encoding/json"
"fmt"
"strings"
N "github.com/IBM/fp-go/number"
)
func Of[T1 any](t T1) Tuple1[T1] {
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)))
}
}
// 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...))
}