1
0
mirror of https://github.com/IBM/fp-go.git synced 2025-12-15 23:33:46 +02:00

Compare commits

..

1 Commits

Author SHA1 Message Date
Dr. Carsten Leue
5899c5d95f fix: refactor either type
Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
2024-02-07 10:57:58 +01:00
36 changed files with 439 additions and 1411 deletions

View File

@@ -26,7 +26,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
go-version: [ '1.20.x', '1.21.x', '1.22.x']
go-version: [ '1.20.x', '1.21.x']
steps:
# full checkout for semantic-release
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1

View File

@@ -15,10 +15,6 @@
package bytes
func Empty() []byte {
return Monoid.Empty()
}
func ToString(a []byte) string {
return string(a)
}

View File

@@ -405,6 +405,8 @@ 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"
)
@@ -455,7 +457,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 tupleMarshalJSON(")
fmt.Fprintf(f, " return json.Marshal([]any{")
// function prototypes
for j := 1; j <= i; j++ {
if j > 1 {
@@ -463,7 +465,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")
}
@@ -473,12 +475,19 @@ 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, " return tupleUnmarshalJSON(data")
// function prototypes
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
for j := 1; j <= i; j++ {
fmt.Fprintf(f, ", &t.F%d", 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, ")\n")
fmt.Fprintf(f, " ")
for j := 1; j <= i; j++ {
fmt.Fprintf(f, "}")
}
fmt.Fprintf(f, "\n return nil\n")
fmt.Fprintf(f, "}\n")
}
@@ -561,13 +570,30 @@ func generateTupleString(f *os.File, i int) {
writeTupleType(f, "T", i)
fmt.Fprintf(f, ") String() string {\n")
// convert to string
fmt.Fprint(f, " return tupleString(")
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, ")\", ")
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")
}

View File

@@ -26,7 +26,7 @@ import (
func onWriteAll[W io.Writer](data []byte) func(w W) RIOE.ReaderIOEither[[]byte] {
return func(w W) RIOE.ReaderIOEither[[]byte] {
return F.Pipe1(
RIOE.TryCatch(func(_ context.Context) func() ([]byte, error) {
RIOE.TryCatch(func(ctx context.Context) func() ([]byte, error) {
return func() ([]byte, error) {
_, err := w.Write(data)
return data, err

View File

@@ -32,7 +32,7 @@ import (
func TestBuilderWithQuery(t *testing.T) {
// add some query
withLimit := R.WithQueryArg("limit")("10")
withURL := R.WithURL("http://www.example.org?a=b")
withURL := R.WithUrl("http://www.example.org?a=b")
b := F.Pipe2(
R.Default,

View File

@@ -109,21 +109,17 @@ func ReadJson[A any](client Client) func(Requester) RIOE.ReaderIOEither[A] {
return ReadJSON[A](client)
}
func readJSON(client Client) func(Requester) RIOE.ReaderIOEither[[]byte] {
// ReadJSON sends a request, reads the response and parses the response as JSON
func ReadJSON[A any](client Client) func(Requester) RIOE.ReaderIOEither[A] {
return F.Flow3(
ReadFullResponse(client),
RIOE.ChainFirstEitherK(F.Flow2(
H.Response,
H.ValidateJSONResponse,
)),
RIOE.Map(H.Body),
)
}
// ReadJSON sends a request, reads the response and parses the response as JSON
func ReadJSON[A any](client Client) func(Requester) RIOE.ReaderIOEither[A] {
return F.Flow2(
readJSON(client),
RIOE.ChainEitherK(J.Unmarshal[A]),
RIOE.ChainEitherK(F.Flow2(
H.Body,
J.Unmarshal[A],
)),
)
}

View File

@@ -41,7 +41,7 @@ func eraseTuple[A, R any](f func(A) IOE.IOEither[error, R]) func(E.Either[error,
}
func eraseProviderFactory0[R any](f IOE.IOEither[error, R]) func(params ...any) IOE.IOEither[error, any] {
return func(_ ...any) IOE.IOEither[error, any] {
return func(params ...any) IOE.IOEither[error, any] {
return F.Pipe1(
f,
IOE.Map[error](F.ToAny[R]),

View File

@@ -20,19 +20,15 @@ import (
)
type (
either struct {
// Either defines a data structure that logically holds either an E or an A. The flag discriminates the cases
Either[E, A any] struct {
isLeft bool
value any
}
// Either defines a data structure that logically holds either an E or an A. The flag discriminates the cases
Either[E, A any] either
)
// String prints some debug info for the object
//
// go:noinline
func eitherString(s *either) string {
func (s Either[E, A]) String() string {
if s.isLeft {
return fmt.Sprintf("Left[%T](%v)", s.value, s.value)
}
@@ -40,27 +36,15 @@ func eitherString(s *either) string {
}
// Format prints some debug info for the object
//
// go:noinline
func eitherFormat(e *either, f fmt.State, c rune) {
func (s Either[E, A]) Format(f fmt.State, c rune) {
switch c {
case 's':
fmt.Fprint(f, eitherString(e))
fmt.Fprint(f, s.String())
default:
fmt.Fprint(f, eitherString(e))
fmt.Fprint(f, s.String())
}
}
// String prints some debug info for the object
func (s Either[E, A]) String() string {
return eitherString((*either)(&s))
}
// Format prints some debug info for the object
func (s Either[E, A]) Format(f fmt.State, c rune) {
eitherFormat((*either)(&s), f, c)
}
// IsLeft tests if the [Either] is a left value. Rather use [Fold] if you need to access the values. Inverse is [IsRight].
func IsLeft[E, A any](val Either[E, A]) bool {
return val.isLeft

View File

@@ -17,7 +17,6 @@ package either
import (
"errors"
"fmt"
"testing"
F "github.com/IBM/fp-go/function"
@@ -110,13 +109,3 @@ func TestFromOption(t *testing.T) {
assert.Equal(t, Left[int]("none"), FromOption[int](F.Constant("none"))(O.None[int]()))
assert.Equal(t, Right[string](1), FromOption[int](F.Constant("none"))(O.Some(1)))
}
func TestStringer(t *testing.T) {
e := Of[error]("foo")
exp := "Right[string](foo)"
assert.Equal(t, exp, e.String())
var s fmt.Stringer = e
assert.Equal(t, exp, s.String())
}

View File

@@ -25,13 +25,13 @@ func Memoize[K comparable, T any](f func(K) T) func(K) T {
}
// ContramapMemoize converts a unary function into a unary function that caches the value depending on the parameter
func ContramapMemoize[T, A any, K comparable](kf func(A) K) func(func(A) T) func(A) T {
func ContramapMemoize[A any, K comparable, T any](kf func(A) K) func(func(A) T) func(A) T {
return G.ContramapMemoize[func(A) T](kf)
}
// CacheCallback converts a unary function into a unary function that caches the value depending on the parameter
func CacheCallback[
T, A any, K comparable](kf func(A) K, getOrCreate func(K, func() func() T) func() T) func(func(A) T) func(A) T {
A any, K comparable, T any](kf func(A) K, getOrCreate func(K, func() func() T) func() T) func(func(A) T) func(A) T {
return G.CacheCallback[func(func(A) T) func(A) T](kf, getOrCreate)
}

View File

@@ -16,14 +16,9 @@
package builder
import (
"bytes"
"crypto/sha256"
"fmt"
"net/http"
"net/url"
A "github.com/IBM/fp-go/array"
B "github.com/IBM/fp-go/bytes"
E "github.com/IBM/fp-go/either"
ENDO "github.com/IBM/fp-go/endomorphism"
F "github.com/IBM/fp-go/function"
@@ -34,7 +29,6 @@ import (
LZ "github.com/IBM/fp-go/lazy"
L "github.com/IBM/fp-go/optics/lens"
O "github.com/IBM/fp-go/option"
R "github.com/IBM/fp-go/record"
S "github.com/IBM/fp-go/string"
T "github.com/IBM/fp-go/tuple"
)
@@ -144,9 +138,6 @@ var (
WithBytes,
ENDO.Chain(WithContentType(C.FormEncoded)),
)
// bodyAsBytes returns a []byte with a fallback to the empty array
bodyAsBytes = O.Fold(B.Empty, E.Fold(F.Ignore1of1[error](B.Empty), F.Identity[[]byte]))
)
func setRawQuery(u *url.URL, raw string) *url.URL {
@@ -281,11 +272,6 @@ func (builder *Builder) GetHeaderValues(name string) []string {
return builder.headers.Values(name)
}
// GetHash returns a hash value for the builder that can be used as a cache key
func (builder *Builder) GetHash() string {
return MakeHash(builder)
}
// Header returns a [L.Lens] for a single header
func Header(name string) L.Lens[*Builder, O.Option[string]] {
get := getHeader(name)
@@ -356,32 +342,3 @@ func WithQueryArg(name string) func(value string) Endomorphism {
func WithoutQueryArg(name string) Endomorphism {
return QueryArg(name).Set(noQueryArg)
}
func hashWriteValue(buf *bytes.Buffer, value string) *bytes.Buffer {
buf.WriteString(value)
return buf
}
func hashWriteQuery(name string, buf *bytes.Buffer, values []string) *bytes.Buffer {
buf.WriteString(name)
return A.Reduce(hashWriteValue, buf)(values)
}
func makeBytes(b *Builder) []byte {
var buf bytes.Buffer
buf.WriteString(b.GetMethod())
buf.WriteString(b.GetURL())
b.GetHeaders().Write(&buf) // #nosec: G104
R.ReduceOrdWithIndex[[]string, *bytes.Buffer](S.Ord)(hashWriteQuery, &buf)(b.GetQuery())
buf.Write(bodyAsBytes(b.GetBody()))
return buf.Bytes()
}
// MakeHash converts a [Builder] into a hash string, convenient to use as a cache key
func MakeHash(b *Builder) string {
return fmt.Sprintf("%x", sha256.Sum256(makeBytes(b)))
}

View File

@@ -16,7 +16,6 @@
package builder
import (
"fmt"
"testing"
F "github.com/IBM/fp-go/function"
@@ -67,27 +66,3 @@ func TestWithFormData(t *testing.T) {
assert.Equal(t, C.FormEncoded, Headers.Get(res).Get(H.ContentType))
}
func TestHash(t *testing.T) {
b1 := F.Pipe4(
Default,
WithContentType(C.JSON),
WithHeader(H.Accept)(C.JSON),
WithURL("http://www.example.com"),
WithJSON(map[string]string{"a": "b"}),
)
b2 := F.Pipe4(
Default,
WithURL("http://www.example.com"),
WithHeader(H.Accept)(C.JSON),
WithContentType(C.JSON),
WithJSON(map[string]string{"a": "b"}),
)
assert.Equal(t, MakeHash(b1), MakeHash(b2))
assert.NotEqual(t, MakeHash(Default), MakeHash(b2))
fmt.Println(MakeHash(b1))
}

View File

@@ -36,7 +36,7 @@ func Map[A, B any](f func(A) B) func(A) B {
return G.Map(f)
}
func MonadMapTo[A, B any](_ A, b B) B {
func MonadMapTo[A, B any](fa A, b B) B {
return b
}

View File

@@ -20,19 +20,13 @@ import (
E "github.com/IBM/fp-go/eq"
F "github.com/IBM/fp-go/function"
"github.com/IBM/fp-go/internal/applicative"
"github.com/IBM/fp-go/internal/apply"
L "github.com/IBM/fp-go/internal/apply/testing"
"github.com/IBM/fp-go/internal/functor"
"github.com/IBM/fp-go/internal/pointed"
"github.com/stretchr/testify/assert"
)
// Applicative identity law
//
// A.ap(A.of(a => a), fa) <-> fa
//
// Deprecated: use [ApplicativeAssertIdentity]
func AssertIdentity[HKTA, HKTAA, A any](t *testing.T,
eq E.Eq[HKTA],
@@ -52,33 +46,9 @@ func AssertIdentity[HKTA, HKTAA, A any](t *testing.T,
}
}
// Applicative identity law
//
// A.ap(A.of(a => a), fa) <-> fa
func ApplicativeAssertIdentity[HKTA, HKTFAA, A any](t *testing.T,
eq E.Eq[HKTA],
ap applicative.Applicative[A, A, HKTA, HKTA, HKTFAA],
paa pointed.Pointed[func(A) A, HKTFAA],
) func(fa HKTA) bool {
// mark as test helper
t.Helper()
return func(fa HKTA) bool {
left := ap.Ap(fa)(paa.Of(F.Identity[A]))
right := fa
return assert.True(t, eq.Equals(left, right), "Applicative identity")
}
}
// Applicative homomorphism law
//
// A.ap(A.of(ab), A.of(a)) <-> A.of(ab(a))
//
// Deprecated: use [ApplicativeAssertHomomorphism]
func AssertHomomorphism[HKTA, HKTB, HKTAB, A, B any](t *testing.T,
eq E.Eq[HKTB],
@@ -102,35 +72,9 @@ func AssertHomomorphism[HKTA, HKTB, HKTAB, A, B any](t *testing.T,
}
}
// Applicative homomorphism law
//
// A.ap(A.of(ab), A.of(a)) <-> A.of(ab(a))
func ApplicativeAssertHomomorphism[HKTA, HKTB, HKTFAB, A, B any](t *testing.T,
eq E.Eq[HKTB],
apab applicative.Applicative[A, B, HKTA, HKTB, HKTFAB],
pb pointed.Pointed[B, HKTB],
pfab pointed.Pointed[func(A) B, HKTFAB],
ab func(A) B,
) func(a A) bool {
// mark as test helper
t.Helper()
return func(a A) bool {
left := apab.Ap(apab.Of(a))(pfab.Of(ab))
right := pb.Of(ab(a))
return assert.True(t, eq.Equals(left, right), "Applicative homomorphism")
}
}
// Applicative interchange law
//
// A.ap(fab, A.of(a)) <-> A.ap(A.of(ab => ab(a)), fab)
//
// Deprecated: use [ApplicativeAssertInterchange]
func AssertInterchange[HKTA, HKTB, HKTAB, HKTABB, A, B any](t *testing.T,
eq E.Eq[HKTB],
@@ -159,38 +103,7 @@ func AssertInterchange[HKTA, HKTB, HKTAB, HKTABB, A, B any](t *testing.T,
}
}
// Applicative interchange law
//
// A.ap(fab, A.of(a)) <-> A.ap(A.of(ab => ab(a)), fab)
func ApplicativeAssertInterchange[HKTA, HKTB, HKTFAB, HKTABB, A, B any](t *testing.T,
eq E.Eq[HKTB],
apab applicative.Applicative[A, B, HKTA, HKTB, HKTFAB],
apabb applicative.Applicative[func(A) B, B, HKTFAB, HKTB, HKTABB],
pabb pointed.Pointed[func(func(A) B) B, HKTABB],
ab func(A) B,
) func(a A) bool {
// mark as test helper
t.Helper()
return func(a A) bool {
fab := apabb.Of(ab)
left := apab.Ap(apab.Of(a))(fab)
right := apabb.Ap(fab)(pabb.Of(func(ab func(A) B) B {
return ab(a)
}))
return assert.True(t, eq.Equals(left, right), "Applicative homomorphism")
}
}
// AssertLaws asserts the apply laws `identity`, `composition`, `associative composition`, 'applicative identity', 'homomorphism', 'interchange'
//
// Deprecated: use [ApplicativeAssertLaws] instead
func AssertLaws[HKTA, HKTB, HKTC, HKTAA, HKTAB, HKTBC, HKTAC, HKTABB, HKTABAC, A, B, C any](t *testing.T,
eqa E.Eq[HKTA],
eqb E.Eq[HKTB],
@@ -237,47 +150,3 @@ func AssertLaws[HKTA, HKTB, HKTC, HKTAA, HKTAB, HKTBC, HKTAC, HKTABB, HKTABAC, A
return apply(fa) && identity(fa) && homomorphism(a) && interchange(a)
}
}
// ApplicativeAssertLaws asserts the apply laws `identity`, `composition`, `associative composition`, 'applicative identity', 'homomorphism', 'interchange'
func ApplicativeAssertLaws[HKTA, HKTB, HKTC, HKTAA, HKTAB, HKTBC, HKTAC, HKTABB, HKTABAC, A, B, C any](t *testing.T,
eqa E.Eq[HKTA],
eqb E.Eq[HKTB],
eqc E.Eq[HKTC],
fofb pointed.Pointed[B, HKTB],
fofaa pointed.Pointed[func(A) A, HKTAA],
fofbc pointed.Pointed[func(B) C, HKTBC],
fofabb pointed.Pointed[func(func(A) B) B, HKTABB],
faa functor.Functor[A, A, HKTA, HKTA],
fmap functor.Functor[func(B) C, func(func(A) B) func(A) C, HKTBC, HKTABAC],
fapaa applicative.Applicative[A, A, HKTA, HKTA, HKTAA],
fapab applicative.Applicative[A, B, HKTA, HKTB, HKTAB],
fapbc apply.Apply[B, C, HKTB, HKTC, HKTBC],
fapac apply.Apply[A, C, HKTA, HKTC, HKTAC],
fapabb applicative.Applicative[func(A) B, B, HKTAB, HKTB, HKTABB],
fapabac applicative.Applicative[func(A) B, func(A) C, HKTAB, HKTAC, HKTABAC],
ab func(A) B,
bc func(B) C,
) func(a A) bool {
// mark as test helper
t.Helper()
// apply laws
apply := L.ApplyAssertLaws(t, eqa, eqc, applicative.ToPointed(fapabac), fofbc, faa, fmap, applicative.ToApply(fapab), fapbc, fapac, applicative.ToApply(fapabac), ab, bc)
// applicative laws
identity := ApplicativeAssertIdentity(t, eqa, fapaa, fofaa)
homomorphism := ApplicativeAssertHomomorphism(t, eqb, fapab, fofb, applicative.ToPointed(fapabb), ab)
interchange := ApplicativeAssertInterchange(t, eqb, fapab, fapabb, fofabb, ab)
return func(a A) bool {
fa := fapaa.Of(a)
return apply(fa) && identity(fa) && homomorphism(a) && interchange(a)
}
}

View File

@@ -17,7 +17,6 @@ package applicative
import (
"github.com/IBM/fp-go/internal/apply"
"github.com/IBM/fp-go/internal/functor"
"github.com/IBM/fp-go/internal/pointed"
)
@@ -25,18 +24,3 @@ type Applicative[A, B, HKTA, HKTB, HKTFAB any] interface {
apply.Apply[A, B, HKTA, HKTB, HKTFAB]
pointed.Pointed[A, HKTA]
}
// ToFunctor converts from [Applicative] to [functor.Functor]
func ToFunctor[A, B, HKTA, HKTB, HKTFAB any](ap Applicative[A, B, HKTA, HKTB, HKTFAB]) functor.Functor[A, B, HKTA, HKTB] {
return ap
}
// ToApply converts from [Applicative] to [apply.Apply]
func ToApply[A, B, HKTA, HKTB, HKTFAB any](ap Applicative[A, B, HKTA, HKTB, HKTFAB]) apply.Apply[A, B, HKTA, HKTB, HKTFAB] {
return ap
}
// ToPointed converts from [Applicative] to [pointed.Pointed]
func ToPointed[A, B, HKTA, HKTB, HKTFAB any](ap Applicative[A, B, HKTA, HKTB, HKTFAB]) pointed.Pointed[A, HKTA] {
return ap
}

View File

@@ -19,18 +19,13 @@ import (
"testing"
E "github.com/IBM/fp-go/eq"
"github.com/IBM/fp-go/internal/apply"
"github.com/IBM/fp-go/internal/functor"
FCT "github.com/IBM/fp-go/internal/functor/testing"
"github.com/IBM/fp-go/internal/pointed"
"github.com/stretchr/testify/assert"
)
// Apply associative composition law
//
// F.ap(F.ap(F.map(fbc, bc => ab => a => bc(ab(a))), fab), fa) <-> F.ap(fbc, F.ap(fab, fa))
//
// Deprecated: use [ApplyAssertAssociativeComposition] instead
func AssertAssociativeComposition[HKTA, HKTB, HKTC, HKTAB, HKTBC, HKTAC, HKTABAC, A, B, C any](t *testing.T,
eq E.Eq[HKTC],
@@ -68,49 +63,7 @@ func AssertAssociativeComposition[HKTA, HKTB, HKTC, HKTAB, HKTBC, HKTAC, HKTABAC
}
}
// Apply associative composition law
//
// F.ap(F.ap(F.map(fbc, bc => ab => a => bc(ab(a))), fab), fa) <-> F.ap(fbc, F.ap(fab, fa))
func ApplyAssertAssociativeComposition[HKTA, HKTB, HKTC, HKTAB, HKTBC, HKTAC, HKTABAC, A, B, C any](t *testing.T,
eq E.Eq[HKTC],
fofab pointed.Pointed[func(A) B, HKTAB],
fofbc pointed.Pointed[func(B) C, HKTBC],
fmap functor.Functor[func(B) C, func(func(A) B) func(A) C, HKTBC, HKTABAC],
fapab apply.Apply[A, B, HKTA, HKTB, HKTAB],
fapbc apply.Apply[B, C, HKTB, HKTC, HKTBC],
fapac apply.Apply[A, C, HKTA, HKTC, HKTAC],
fapabac apply.Apply[func(A) B, func(A) C, HKTAB, HKTAC, HKTABAC],
ab func(A) B,
bc func(B) C,
) func(fa HKTA) bool {
t.Helper()
return func(fa HKTA) bool {
fab := fofab.Of(ab)
fbc := fofbc.Of(bc)
left := fapac.Ap(fa)(fapabac.Ap(fab)(fmap.Map(func(bc func(B) C) func(func(A) B) func(A) C {
return func(ab func(A) B) func(A) C {
return func(a A) C {
return bc(ab(a))
}
}
})(fbc)))
right := fapbc.Ap(fapab.Ap(fa)(fab))(fbc)
return assert.True(t, eq.Equals(left, right), "Apply associative composition")
}
}
// AssertLaws asserts the apply laws `identity`, `composition` and `associative composition`
//
// Deprecated: use [ApplyAssertLaws] instead
func AssertLaws[HKTA, HKTB, HKTC, HKTAB, HKTBC, HKTAC, HKTABAC, A, B, C any](t *testing.T,
eqa E.Eq[HKTA],
eqc E.Eq[HKTC],
@@ -145,36 +98,3 @@ func AssertLaws[HKTA, HKTB, HKTC, HKTAB, HKTBC, HKTAC, HKTABAC, A, B, C any](t *
return functor(fa) && composition(fa)
}
}
// ApplyAssertLaws asserts the apply laws `identity`, `composition` and `associative composition`
func ApplyAssertLaws[HKTA, HKTB, HKTC, HKTAB, HKTBC, HKTAC, HKTABAC, A, B, C any](t *testing.T,
eqa E.Eq[HKTA],
eqc E.Eq[HKTC],
fofab pointed.Pointed[func(A) B, HKTAB],
fofbc pointed.Pointed[func(B) C, HKTBC],
faa functor.Functor[A, A, HKTA, HKTA],
fmap functor.Functor[func(B) C, func(func(A) B) func(A) C, HKTBC, HKTABAC],
fapab apply.Apply[A, B, HKTA, HKTB, HKTAB],
fapbc apply.Apply[B, C, HKTB, HKTC, HKTBC],
fapac apply.Apply[A, C, HKTA, HKTC, HKTAC],
fapabac apply.Apply[func(A) B, func(A) C, HKTAB, HKTAC, HKTABAC],
ab func(A) B,
bc func(B) C,
) func(fa HKTA) bool {
// mark as test helper
t.Helper()
// functor laws
functor := FCT.FunctorAssertLaws(t, eqa, eqc, faa, apply.ToFunctor(fapab), apply.ToFunctor(fapac), apply.ToFunctor(fapbc), ab, bc)
// associative composition laws
composition := ApplyAssertAssociativeComposition(t, eqc, fofab, fofbc, fmap, fapab, fapbc, fapac, fapabac, ab, bc)
return func(fa HKTA) bool {
return functor(fa) && composition(fa)
}
}

View File

@@ -23,8 +23,3 @@ type Apply[A, B, HKTA, HKTB, HKTFAB any] interface {
functor.Functor[A, B, HKTA, HKTB]
Ap(HKTA) func(HKTFAB) HKTB
}
// ToFunctor converts from [Apply] to [functor.Functor]
func ToFunctor[A, B, HKTA, HKTB, HKTFAB any](ap Apply[A, B, HKTA, HKTB, HKTFAB]) functor.Functor[A, B, HKTA, HKTB] {
return ap
}

View File

@@ -20,19 +20,13 @@ import (
E "github.com/IBM/fp-go/eq"
F "github.com/IBM/fp-go/function"
"github.com/IBM/fp-go/internal/apply"
L "github.com/IBM/fp-go/internal/apply/testing"
"github.com/IBM/fp-go/internal/chain"
"github.com/IBM/fp-go/internal/functor"
"github.com/IBM/fp-go/internal/pointed"
"github.com/stretchr/testify/assert"
)
// Chain associativity law
//
// F.chain(F.chain(fa, afb), bfc) <-> F.chain(fa, a => F.chain(afb(a), bfc))
//
// Deprecated: use [ChainAssertAssociativity] instead
func AssertAssociativity[HKTA, HKTB, HKTC, A, B, C any](t *testing.T,
eq E.Eq[HKTC],
@@ -61,40 +55,7 @@ func AssertAssociativity[HKTA, HKTB, HKTC, A, B, C any](t *testing.T,
}
}
// Chain associativity law
//
// F.chain(F.chain(fa, afb), bfc) <-> F.chain(fa, a => F.chain(afb(a), bfc))
func ChainAssertAssociativity[HKTA, HKTB, HKTC, HKTAB, HKTAC, HKTBC, A, B, C any](t *testing.T,
eq E.Eq[HKTC],
fofb pointed.Pointed[B, HKTB],
fofc pointed.Pointed[C, HKTC],
chainab chain.Chainable[A, B, HKTA, HKTB, HKTAB],
chainac chain.Chainable[A, C, HKTA, HKTC, HKTAC],
chainbc chain.Chainable[B, C, HKTB, HKTC, HKTBC],
ab func(A) B,
bc func(B) C,
) func(fa HKTA) bool {
return func(fa HKTA) bool {
afb := F.Flow2(ab, fofb.Of)
bfc := F.Flow2(bc, fofc.Of)
left := chainbc.Chain(bfc)(chainab.Chain(afb)(fa))
right := chainac.Chain(func(a A) HKTC {
return chainbc.Chain(bfc)(afb(a))
})(fa)
return assert.True(t, eq.Equals(left, right), "Chain associativity")
}
}
// AssertLaws asserts the apply laws `identity`, `composition`, `associative composition` and `associativity`
//
// Deprecated: use [ChainAssertLaws] instead
func AssertLaws[HKTA, HKTB, HKTC, HKTAB, HKTBC, HKTAC, HKTABAC, A, B, C any](t *testing.T,
eqa E.Eq[HKTA],
eqc E.Eq[HKTC],
@@ -134,37 +95,3 @@ func AssertLaws[HKTA, HKTB, HKTC, HKTAB, HKTBC, HKTAC, HKTABAC, A, B, C any](t *
return apply(fa) && associativity(fa)
}
}
// ChainAssertLaws asserts the apply laws `identity`, `composition`, `associative composition` and `associativity`
func ChainAssertLaws[HKTA, HKTB, HKTC, HKTAB, HKTBC, HKTAC, HKTABAC, A, B, C any](t *testing.T,
eqa E.Eq[HKTA],
eqc E.Eq[HKTC],
fofb pointed.Pointed[B, HKTB],
fofc pointed.Pointed[C, HKTC],
fofab pointed.Pointed[func(A) B, HKTAB],
fofbc pointed.Pointed[func(B) C, HKTBC],
faa functor.Functor[A, A, HKTA, HKTA],
fmap functor.Functor[func(B) C, func(func(A) B) func(A) C, HKTBC, HKTABAC],
chainab chain.Chainable[A, B, HKTA, HKTB, HKTAB],
chainac chain.Chainable[A, C, HKTA, HKTC, HKTAC],
chainbc chain.Chainable[B, C, HKTB, HKTC, HKTBC],
fapabac apply.Apply[func(A) B, func(A) C, HKTAB, HKTAC, HKTABAC],
ab func(A) B,
bc func(B) C,
) func(fa HKTA) bool {
// apply laws
apply := L.ApplyAssertLaws(t, eqa, eqc, fofab, fofbc, faa, fmap, chain.ToApply(chainab), chain.ToApply(chainbc), chain.ToApply(chainac), fapabac, ab, bc)
// chain laws
associativity := ChainAssertAssociativity(t, eqc, fofb, fofc, chainab, chainac, chainbc, ab, bc)
return func(fa HKTA) bool {
return apply(fa) && associativity(fa)
}
}

View File

@@ -17,20 +17,9 @@ package chain
import (
"github.com/IBM/fp-go/internal/apply"
"github.com/IBM/fp-go/internal/functor"
)
type Chainable[A, B, HKTA, HKTB, HKTFAB any] interface {
apply.Apply[A, B, HKTA, HKTB, HKTFAB]
Chain(func(A) HKTB) func(HKTA) HKTB
}
// ToFunctor converts from [Chainable] to [functor.Functor]
func ToFunctor[A, B, HKTA, HKTB, HKTFAB any](ap Chainable[A, B, HKTA, HKTB, HKTFAB]) functor.Functor[A, B, HKTA, HKTB] {
return ap
}
// ToApply converts from [Chainable] to [functor.Functor]
func ToApply[A, B, HKTA, HKTB, HKTFAB any](ap Chainable[A, B, HKTA, HKTB, HKTFAB]) apply.Apply[A, B, HKTA, HKTB, HKTFAB] {
return ap
}

View File

@@ -20,15 +20,12 @@ import (
E "github.com/IBM/fp-go/eq"
F "github.com/IBM/fp-go/function"
"github.com/IBM/fp-go/internal/functor"
"github.com/stretchr/testify/assert"
)
// Functor identity law
//
// F.map(fa, a => a) <-> fa
//
// Deprecated: use [FunctorAssertIdentity]
func AssertIdentity[HKTA, A any](t *testing.T, eq E.Eq[HKTA], fmap func(HKTA, func(A) A) HKTA) func(fa HKTA) bool {
t.Helper()
return func(fa HKTA) bool {
@@ -36,28 +33,9 @@ func AssertIdentity[HKTA, A any](t *testing.T, eq E.Eq[HKTA], fmap func(HKTA, fu
}
}
// Functor identity law
//
// F.map(fa, a => a) <-> fa
func FunctorAssertIdentity[HKTA, A any](
t *testing.T,
eq E.Eq[HKTA],
fca functor.Functor[A, A, HKTA, HKTA],
) func(fa HKTA) bool {
t.Helper()
return func(fa HKTA) bool {
return assert.True(t, eq.Equals(fa, fca.Map(F.Identity[A])(fa)), "Functor identity law")
}
}
// Functor composition law
//
// F.map(fa, a => bc(ab(a))) <-> F.map(F.map(fa, ab), bc)
//
// Deprecated: use [FunctorAssertComposition] instead
func AssertComposition[HKTA, HKTB, HKTC, A, B, C any](
t *testing.T,
@@ -75,30 +53,7 @@ func AssertComposition[HKTA, HKTB, HKTC, A, B, C any](
}
}
// Functor composition law
//
// F.map(fa, a => bc(ab(a))) <-> F.map(F.map(fa, ab), bc)
func FunctorAssertComposition[HKTA, HKTB, HKTC, A, B, C any](
t *testing.T,
eq E.Eq[HKTC],
fab functor.Functor[A, B, HKTA, HKTB],
fac functor.Functor[A, C, HKTA, HKTC],
fbc functor.Functor[B, C, HKTB, HKTC],
ab func(A) B,
bc func(B) C,
) func(fa HKTA) bool {
t.Helper()
return func(fa HKTA) bool {
return assert.True(t, eq.Equals(fac.Map(F.Flow2(ab, bc))(fa), fbc.Map(bc)(fab.Map(ab)(fa))), "Functor composition law")
}
}
// AssertLaws asserts the functor laws `identity` and `composition`
//
// Deprecated: use [FunctorAssertLaws] instead
func AssertLaws[HKTA, HKTB, HKTC, A, B, C any](t *testing.T,
eqa E.Eq[HKTA],
eqc E.Eq[HKTC],
@@ -107,7 +62,6 @@ func AssertLaws[HKTA, HKTB, HKTC, A, B, C any](t *testing.T,
fab func(HKTA, func(A) B) HKTB,
fac func(HKTA, func(A) C) HKTC,
fbc func(HKTB, func(B) C) HKTC,
ab func(A) B,
bc func(B) C,
) func(fa HKTA) bool {
@@ -119,25 +73,3 @@ func AssertLaws[HKTA, HKTB, HKTC, A, B, C any](t *testing.T,
return identity(fa) && composition(fa)
}
}
// FunctorAssertLaws asserts the functor laws `identity` and `composition`
func FunctorAssertLaws[HKTA, HKTB, HKTC, A, B, C any](t *testing.T,
eqa E.Eq[HKTA],
eqc E.Eq[HKTC],
faa functor.Functor[A, A, HKTA, HKTA],
fab functor.Functor[A, B, HKTA, HKTB],
fac functor.Functor[A, C, HKTA, HKTC],
fbc functor.Functor[B, C, HKTB, HKTC],
ab func(A) B,
bc func(B) C,
) func(fa HKTA) bool {
t.Helper()
identity := FunctorAssertIdentity(t, eqa, faa)
composition := FunctorAssertComposition(t, eqc, fab, fac, fbc, ab, bc)
return func(fa HKTA) bool {
return identity(fa) && composition(fa)
}
}

View File

@@ -17,38 +17,10 @@ package monad
import (
"github.com/IBM/fp-go/internal/applicative"
"github.com/IBM/fp-go/internal/apply"
"github.com/IBM/fp-go/internal/chain"
"github.com/IBM/fp-go/internal/functor"
"github.com/IBM/fp-go/internal/pointed"
)
type Monad[A, B, HKTA, HKTB, HKTFAB any] interface {
applicative.Applicative[A, B, HKTA, HKTB, HKTFAB]
chain.Chainable[A, B, HKTA, HKTB, HKTFAB]
}
// ToFunctor converts from [Monad] to [functor.Functor]
func ToFunctor[A, B, HKTA, HKTB, HKTFAB any](ap Monad[A, B, HKTA, HKTB, HKTFAB]) functor.Functor[A, B, HKTA, HKTB] {
return ap
}
// ToApply converts from [Monad] to [apply.Apply]
func ToApply[A, B, HKTA, HKTB, HKTFAB any](ap Monad[A, B, HKTA, HKTB, HKTFAB]) apply.Apply[A, B, HKTA, HKTB, HKTFAB] {
return ap
}
// ToPointed converts from [Monad] to [pointed.Pointed]
func ToPointed[A, B, HKTA, HKTB, HKTFAB any](ap Monad[A, B, HKTA, HKTB, HKTFAB]) pointed.Pointed[A, HKTA] {
return ap
}
// ToApplicative converts from [Monad] to [applicative.Applicative]
func ToApplicative[A, B, HKTA, HKTB, HKTFAB any](ap Monad[A, B, HKTA, HKTB, HKTFAB]) applicative.Applicative[A, B, HKTA, HKTB, HKTFAB] {
return ap
}
// ToChainable converts from [Monad] to [chain.Chainable]
func ToChainable[A, B, HKTA, HKTB, HKTFAB any](ap Monad[A, B, HKTA, HKTB, HKTFAB]) chain.Chainable[A, B, HKTA, HKTB, HKTFAB] {
return ap
}

View File

@@ -19,21 +19,14 @@ import (
"testing"
E "github.com/IBM/fp-go/eq"
"github.com/IBM/fp-go/internal/applicative"
LA "github.com/IBM/fp-go/internal/applicative/testing"
"github.com/IBM/fp-go/internal/chain"
LC "github.com/IBM/fp-go/internal/chain/testing"
"github.com/IBM/fp-go/internal/functor"
"github.com/IBM/fp-go/internal/monad"
"github.com/IBM/fp-go/internal/pointed"
"github.com/stretchr/testify/assert"
)
// Apply monad left identity law
//
// M.chain(M.of(a), f) <-> f(a)
//
// Deprecated: use [MonadAssertLeftIdentity] instead
func AssertLeftIdentity[HKTA, HKTB, A, B any](t *testing.T,
eq E.Eq[HKTB],
@@ -57,36 +50,9 @@ func AssertLeftIdentity[HKTA, HKTB, A, B any](t *testing.T,
}
}
// Apply monad left identity law
//
// M.chain(M.of(a), f) <-> f(a)
func MonadAssertLeftIdentity[HKTA, HKTB, HKTFAB, A, B any](t *testing.T,
eq E.Eq[HKTB],
fofb pointed.Pointed[B, HKTB],
ma monad.Monad[A, B, HKTA, HKTB, HKTFAB],
ab func(A) B,
) func(a A) bool {
return func(a A) bool {
f := func(a A) HKTB {
return fofb.Of(ab(a))
}
left := ma.Chain(f)(ma.Of(a))
right := f(a)
return assert.True(t, eq.Equals(left, right), "Monad left identity")
}
}
// Apply monad right identity law
//
// M.chain(fa, M.of) <-> fa
//
// Deprecated: use [MonadAssertRightIdentity] instead
func AssertRightIdentity[HKTA, A any](t *testing.T,
eq E.Eq[HKTA],
@@ -103,27 +69,7 @@ func AssertRightIdentity[HKTA, A any](t *testing.T,
}
}
// Apply monad right identity law
//
// M.chain(fa, M.of) <-> fa
func MonadAssertRightIdentity[HKTA, HKTAA, A any](t *testing.T,
eq E.Eq[HKTA],
ma monad.Monad[A, A, HKTA, HKTA, HKTAA],
) func(fa HKTA) bool {
return func(fa HKTA) bool {
left := ma.Chain(ma.Of)(fa)
right := fa
return assert.True(t, eq.Equals(left, right), "Monad right identity")
}
}
// AssertLaws asserts the apply laws `identity`, `composition`, `associative composition`, 'applicative identity', 'homomorphism', 'interchange', `associativity`, `left identity`, `right identity`
//
// Deprecated: use [MonadAssertLaws] instead
func AssertLaws[HKTA, HKTB, HKTC, HKTAA, HKTAB, HKTBC, HKTAC, HKTABB, HKTABAC, A, B, C any](t *testing.T,
eqa E.Eq[HKTA],
eqb E.Eq[HKTB],
@@ -174,55 +120,3 @@ func AssertLaws[HKTA, HKTB, HKTC, HKTAA, HKTAB, HKTBC, HKTAC, HKTABB, HKTABAC, A
return applicative(a) && chain(fa) && leftIdentity(a) && rightIdentity(fa)
}
}
// MonadAssertLaws asserts the apply laws `identity`, `composition`, `associative composition`, 'applicative identity', 'homomorphism', 'interchange', `associativity`, `left identity`, `right identity`
func MonadAssertLaws[HKTA, HKTB, HKTC, HKTAA, HKTAB, HKTBC, HKTAC, HKTABB, HKTABAC, A, B, C any](t *testing.T,
eqa E.Eq[HKTA],
eqb E.Eq[HKTB],
eqc E.Eq[HKTC],
fofc pointed.Pointed[C, HKTC],
fofaa pointed.Pointed[func(A) A, HKTAA],
fofbc pointed.Pointed[func(B) C, HKTBC],
fofabb pointed.Pointed[func(func(A) B) B, HKTABB],
fmap functor.Functor[func(B) C, func(func(A) B) func(A) C, HKTBC, HKTABAC],
fapabb applicative.Applicative[func(A) B, B, HKTAB, HKTB, HKTABB],
fapabac applicative.Applicative[func(A) B, func(A) C, HKTAB, HKTAC, HKTABAC],
maa monad.Monad[A, A, HKTA, HKTA, HKTAA],
mab monad.Monad[A, B, HKTA, HKTB, HKTAB],
mac monad.Monad[A, C, HKTA, HKTC, HKTAC],
mbc monad.Monad[B, C, HKTB, HKTC, HKTBC],
ab func(A) B,
bc func(B) C,
) func(a A) bool {
// derivations
fofa := monad.ToPointed(maa)
fofb := monad.ToPointed(mbc)
fofab := applicative.ToPointed(fapabb)
fapaa := monad.ToApplicative(maa)
fapab := monad.ToApplicative(mab)
chainab := monad.ToChainable(mab)
chainac := monad.ToChainable(mac)
chainbc := monad.ToChainable(mbc)
fapbc := chain.ToApply(chainbc)
fapac := chain.ToApply(chainac)
faa := monad.ToFunctor(maa)
// applicative laws
apLaw := LA.ApplicativeAssertLaws(t, eqa, eqb, eqc, fofb, fofaa, fofbc, fofabb, faa, fmap, fapaa, fapab, fapbc, fapac, fapabb, fapabac, ab, bc)
// chain laws
chainLaw := LC.ChainAssertLaws(t, eqa, eqc, fofb, fofc, fofab, fofbc, faa, fmap, chainab, chainac, chainbc, applicative.ToApply(fapabac), ab, bc)
// monad laws
leftIdentity := MonadAssertLeftIdentity(t, eqb, fofb, mab, ab)
rightIdentity := MonadAssertRightIdentity(t, eqa, maa)
return func(a A) bool {
fa := fofa.Of(a)
return apLaw(a) && chainLaw(fa) && leftIdentity(a) && rightIdentity(fa)
}
}

View File

@@ -124,22 +124,17 @@ func ReadJson[A any](client Client) func(Requester) IOE.IOEither[error, A] {
return ReadJSON[A](client)
}
// readJSON sends a request, reads the response and parses the response as a []byte
func readJSON(client Client) func(Requester) IOE.IOEither[error, []byte] {
// ReadJSON sends a request, reads the response and parses the response as JSON
func ReadJSON[A any](client Client) func(Requester) IOE.IOEither[error, A] {
return F.Flow3(
ReadFullResponse(client),
IOE.ChainFirstEitherK(F.Flow2(
H.Response,
H.ValidateJSONResponse,
)),
IOE.Map[error](H.Body),
)
}
// ReadJSON sends a request, reads the response and parses the response as JSON
func ReadJSON[A any](client Client) func(Requester) IOE.IOEither[error, A] {
return F.Flow2(
readJSON(client),
IOE.ChainEitherK[error](J.Unmarshal[A]),
IOE.ChainEitherK(F.Flow2(
H.Body,
J.Unmarshal[A],
)),
)
}

View File

@@ -113,7 +113,7 @@ func fromPredicate[S, A any](creator func(get func(S) O.Option[A], set func(S, A
return func(get func(S) A, set func(S, A) S) Optional[S, A] {
return creator(
F.Flow2(get, fromPred),
func(s S, _ A) S {
func(s S, a A) S {
return F.Pipe3(
s,
get,

View File

@@ -19,79 +19,52 @@ import (
"bytes"
"encoding/json"
"fmt"
"reflect"
)
var (
// jsonNull is the cached representation of the `null` serialization in JSON
jsonNull = []byte("null")
)
// Option defines a data structure that logically holds a value or not
type Option[A any] struct {
isSome bool
value A
}
// optString prints some debug info for the object
//
// go:noinline
func optString(isSome bool, value any) string {
if isSome {
return fmt.Sprintf("Some[%T](%v)", value, value)
}
return fmt.Sprintf("None[%T]", value)
}
// optFormat prints some debug info for the object
//
// go:noinline
func optFormat(isSome bool, value any, f fmt.State, c rune) {
switch c {
case 's':
fmt.Fprint(f, optString(isSome, value))
default:
fmt.Fprint(f, optString(isSome, value))
}
some A
}
// String prints some debug info for the object
func (s Option[A]) String() string {
return optString(s.isSome, s.value)
if s.isSome {
return fmt.Sprintf("Some[%T](%v)", s.some, s.some)
}
return fmt.Sprintf("None[%T]", s.some)
}
// Format prints some debug info for the object
func (s Option[A]) Format(f fmt.State, c rune) {
optFormat(s.isSome, s.value, f, c)
switch c {
case 's':
fmt.Fprint(f, s.String())
default:
fmt.Fprint(f, s.String())
}
}
func optMarshalJSON(isSome bool, value any) ([]byte, error) {
if isSome {
return json.Marshal(value)
func (s Option[A]) MarshalJSON() ([]byte, error) {
if IsSome(s) {
return json.Marshal(s.some)
}
return jsonNull, nil
}
func (s Option[A]) MarshalJSON() ([]byte, error) {
return optMarshalJSON(s.isSome, s.value)
}
// optUnmarshalJSON unmarshals the [Option] from a JSON string
//
// go:noinline
func optUnmarshalJSON(isSome *bool, value any, data []byte) error {
func (s *Option[A]) UnmarshalJSON(data []byte) error {
// decode the value
if bytes.Equal(data, jsonNull) {
*isSome = false
reflect.ValueOf(value).Elem().SetZero()
s.isSome = false
s.some = *new(A)
return nil
}
*isSome = true
return json.Unmarshal(data, value)
}
func (s *Option[A]) UnmarshalJSON(data []byte) error {
return optUnmarshalJSON(&s.isSome, &s.value, data)
s.isSome = true
return json.Unmarshal(data, &s.some)
}
func IsNone[T any](val Option[T]) bool {
@@ -99,7 +72,7 @@ func IsNone[T any](val Option[T]) bool {
}
func Some[T any](value T) Option[T] {
return Option[T]{isSome: true, value: value}
return Option[T]{isSome: true, some: value}
}
func Of[T any](value T) Option[T] {
@@ -116,11 +89,11 @@ func IsSome[T any](val Option[T]) bool {
func MonadFold[A, B any](ma Option[A], onNone func() B, onSome func(A) B) B {
if IsSome(ma) {
return onSome(ma.value)
return onSome(ma.some)
}
return onNone()
}
func Unwrap[A any](ma Option[A]) (A, bool) {
return ma.value, ma.isSome
return ma.some, ma.isSome
}

View File

@@ -1,33 +0,0 @@
// Copyright (c) 2024 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 pair
import (
EQ "github.com/IBM/fp-go/eq"
)
// Constructs an equal predicate for an `Either`
func Eq[A, B any](a EQ.Eq[A], b EQ.Eq[B]) EQ.Eq[Pair[A, B]] {
return EQ.FromEquals(func(l, r Pair[A, B]) bool {
return a.Equals(Head(l), Head(r)) && b.Equals(Tail(l), Tail(r))
})
}
// FromStrictEquals constructs an [EQ.Eq] from the canonical comparison function
func FromStrictEquals[A, B comparable]() EQ.Eq[Pair[A, B]] {
return Eq(EQ.FromStrictEquals[A](), EQ.FromStrictEquals[B]())
}

View File

@@ -1,193 +0,0 @@
// Copyright (c) 2024 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 pair
import (
"github.com/IBM/fp-go/internal/applicative"
"github.com/IBM/fp-go/internal/functor"
"github.com/IBM/fp-go/internal/monad"
"github.com/IBM/fp-go/internal/pointed"
M "github.com/IBM/fp-go/monoid"
Sg "github.com/IBM/fp-go/semigroup"
)
type (
pairPointedHead[A, B any] struct {
m M.Monoid[B]
}
pairFunctorHead[A, B, A1 any] struct {
}
pairApplicativeHead[A, B, A1 any] struct {
s Sg.Semigroup[B]
m M.Monoid[B]
}
pairMonadHead[A, B, A1 any] struct {
s Sg.Semigroup[B]
m M.Monoid[B]
}
pairPointedTail[A, B any] struct {
m M.Monoid[A]
}
pairFunctorTail[A, B, B1 any] struct {
}
pairApplicativeTail[A, B, B1 any] struct {
s Sg.Semigroup[A]
m M.Monoid[A]
}
pairMonadTail[A, B, B1 any] struct {
s Sg.Semigroup[A]
m M.Monoid[A]
}
)
func (o *pairMonadHead[A, B, A1]) Of(a A) Pair[A, B] {
return MakePair(a, o.m.Empty())
}
func (o *pairMonadHead[A, B, A1]) Map(f func(A) A1) func(Pair[A, B]) Pair[A1, B] {
return Map[B](f)
}
func (o *pairMonadHead[A, B, A1]) Chain(f func(A) Pair[A1, B]) func(Pair[A, B]) Pair[A1, B] {
return Chain[B, A, A1](o.s, f)
}
func (o *pairMonadHead[A, B, A1]) Ap(fa Pair[A, B]) func(Pair[func(A) A1, B]) Pair[A1, B] {
return Ap[B, A, A1](o.s, fa)
}
func (o *pairPointedHead[A, B]) Of(a A) Pair[A, B] {
return MakePair(a, o.m.Empty())
}
func (o *pairFunctorHead[A, B, A1]) Map(f func(A) A1) func(Pair[A, B]) Pair[A1, B] {
return Map[B, A, A1](f)
}
func (o *pairApplicativeHead[A, B, A1]) Map(f func(A) A1) func(Pair[A, B]) Pair[A1, B] {
return Map[B, A, A1](f)
}
func (o *pairApplicativeHead[A, B, A1]) Ap(fa Pair[A, B]) func(Pair[func(A) A1, B]) Pair[A1, B] {
return Ap[B, A, A1](o.s, fa)
}
func (o *pairApplicativeHead[A, B, A1]) Of(a A) Pair[A, B] {
return MakePair(a, o.m.Empty())
}
// Monad implements the monadic operations for [Pair]
func Monad[A, B, A1 any](m M.Monoid[B]) monad.Monad[A, A1, Pair[A, B], Pair[A1, B], Pair[func(A) A1, B]] {
return &pairMonadHead[A, B, A1]{s: M.ToSemigroup(m), m: m}
}
// Pointed implements the pointed operations for [Pair]
func Pointed[A, B any](m M.Monoid[B]) pointed.Pointed[A, Pair[A, B]] {
return &pairPointedHead[A, B]{m: m}
}
// Functor implements the functor operations for [Pair]
func Functor[A, B, A1 any]() functor.Functor[A, A1, Pair[A, B], Pair[A1, B]] {
return &pairFunctorHead[A, B, A1]{}
}
// Applicative implements the applicative operations for [Pair]
func Applicative[A, B, A1 any](m M.Monoid[B]) applicative.Applicative[A, A1, Pair[A, B], Pair[A1, B], Pair[func(A) A1, B]] {
return &pairApplicativeHead[A, B, A1]{s: M.ToSemigroup(m), m: m}
}
// MonadHead implements the monadic operations for [Pair]
func MonadHead[A, B, A1 any](m M.Monoid[B]) monad.Monad[A, A1, Pair[A, B], Pair[A1, B], Pair[func(A) A1, B]] {
return Monad[A, B, A1](m)
}
// PointedHead implements the pointed operations for [Pair]
func PointedHead[A, B any](m M.Monoid[B]) pointed.Pointed[A, Pair[A, B]] {
return PointedHead[A, B](m)
}
// FunctorHead implements the functor operations for [Pair]
func FunctorHead[A, B, A1 any]() functor.Functor[A, A1, Pair[A, B], Pair[A1, B]] {
return Functor[A, B, A1]()
}
// ApplicativeHead implements the applicative operations for [Pair]
func ApplicativeHead[A, B, A1 any](m M.Monoid[B]) applicative.Applicative[A, A1, Pair[A, B], Pair[A1, B], Pair[func(A) A1, B]] {
return Applicative[A, B, A1](m)
}
func (o *pairMonadTail[A, B, B1]) Of(b B) Pair[A, B] {
return MakePair(o.m.Empty(), b)
}
func (o *pairMonadTail[A, B, B1]) Map(f func(B) B1) func(Pair[A, B]) Pair[A, B1] {
return MapTail[A, B, B1](f)
}
func (o *pairMonadTail[A, B, B1]) Chain(f func(B) Pair[A, B1]) func(Pair[A, B]) Pair[A, B1] {
return ChainTail[A, B, B1](o.s, f)
}
func (o *pairMonadTail[A, B, B1]) Ap(fa Pair[A, B]) func(Pair[A, func(B) B1]) Pair[A, B1] {
return ApTail[A, B, B1](o.s, fa)
}
func (o *pairPointedTail[A, B]) Of(b B) Pair[A, B] {
return MakePair(o.m.Empty(), b)
}
func (o *pairFunctorTail[A, B, B1]) Map(f func(B) B1) func(Pair[A, B]) Pair[A, B1] {
return MapTail[A, B, B1](f)
}
func (o *pairApplicativeTail[A, B, B1]) Map(f func(B) B1) func(Pair[A, B]) Pair[A, B1] {
return MapTail[A, B, B1](f)
}
func (o *pairApplicativeTail[A, B, B1]) Ap(fa Pair[A, B]) func(Pair[A, func(B) B1]) Pair[A, B1] {
return ApTail[A, B, B1](o.s, fa)
}
func (o *pairApplicativeTail[A, B, B1]) Of(b B) Pair[A, B] {
return MakePair(o.m.Empty(), b)
}
// MonadTail implements the monadic operations for [Pair]
func MonadTail[B, A, B1 any](m M.Monoid[A]) monad.Monad[B, B1, Pair[A, B], Pair[A, B1], Pair[A, func(B) B1]] {
return &pairMonadTail[A, B, B1]{s: M.ToSemigroup(m), m: m}
}
// PointedTail implements the pointed operations for [Pair]
func PointedTail[B, A any](m M.Monoid[A]) pointed.Pointed[B, Pair[A, B]] {
return &pairPointedTail[A, B]{m: m}
}
// FunctorTail implements the functor operations for [Pair]
func FunctorTail[B, A, B1 any]() functor.Functor[B, B1, Pair[A, B], Pair[A, B1]] {
return &pairFunctorTail[A, B, B1]{}
}
// ApplicativeTail implements the applicative operations for [Pair]
func ApplicativeTail[B, A, B1 any](m M.Monoid[A]) applicative.Applicative[B, B1, Pair[A, B], Pair[A, B1], Pair[A, func(B) B1]] {
return &pairApplicativeTail[A, B, B1]{s: M.ToSemigroup(m), m: m}
}

View File

@@ -1,204 +0,0 @@
// Copyright (c) 2024 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 pair
import (
"fmt"
F "github.com/IBM/fp-go/function"
Sg "github.com/IBM/fp-go/semigroup"
T "github.com/IBM/fp-go/tuple"
)
type (
pair struct {
head, Tail any
}
// Pair defines a data structure that holds two strongly typed values
Pair[A, B any] pair
)
// String prints some debug info for the object
//
// go:noinline
func pairString(s *pair) string {
return fmt.Sprintf("Pair[%T, %t](%v, %v)", s.head, s.Tail, s.head, s.Tail)
}
// Format prints some debug info for the object
//
// go:noinline
func pairFormat(e *pair, f fmt.State, c rune) {
switch c {
case 's':
fmt.Fprint(f, pairString(e))
default:
fmt.Fprint(f, pairString(e))
}
}
// String prints some debug info for the object
func (s Pair[A, B]) String() string {
return pairString((*pair)(&s))
}
// Format prints some debug info for the object
func (s Pair[A, B]) Format(f fmt.State, c rune) {
pairFormat((*pair)(&s), f, c)
}
// Of creates a [Pair] with the same value to to both fields
func Of[A any](value A) Pair[A, A] {
return Pair[A, A]{head: value, Tail: value}
}
// FromTuple creates a [Pair] from a [T.Tuple2]
func FromTuple[A, B any](t T.Tuple2[A, B]) Pair[A, B] {
return Pair[A, B]{head: t.F1, Tail: t.F2}
}
// ToTuple creates a [T.Tuple2] from a [Pair]
func ToTuple[A, B any](t Pair[A, B]) T.Tuple2[A, B] {
return T.MakeTuple2(Head(t), Tail(t))
}
// MakePair creates a [Pair] from two values
func MakePair[A, B any](a A, b B) Pair[A, B] {
return Pair[A, B]{head: a, Tail: b}
}
// Head returns the head value of the pair
func Head[A, B any](fa Pair[A, B]) A {
return fa.head.(A)
}
// Tail returns the head value of the pair
func Tail[A, B any](fa Pair[A, B]) B {
return fa.Tail.(B)
}
// MonadMapHead maps the head value
func MonadMapHead[B, A, A1 any](fa Pair[A, B], f func(A) A1) Pair[A1, B] {
return Pair[A1, B]{f(Head(fa)), fa.Tail}
}
// MonadMap maps the head value
func MonadMap[B, A, A1 any](fa Pair[A, B], f func(A) A1) Pair[A1, B] {
return MonadMapHead(fa, f)
}
// MonadMapTail maps the Tail value
func MonadMapTail[A, B, B1 any](fa Pair[A, B], f func(B) B1) Pair[A, B1] {
return Pair[A, B1]{fa.head, f(Tail(fa))}
}
// MonadBiMap maps both values
func MonadBiMap[A, B, A1, B1 any](fa Pair[A, B], f func(A) A1, g func(B) B1) Pair[A1, B1] {
return Pair[A1, B1]{f(Head(fa)), g(Tail(fa))}
}
// Map maps the head value
func Map[B, A, A1 any](f func(A) A1) func(Pair[A, B]) Pair[A1, B] {
return MapHead[B, A, A1](f)
}
// MapHead maps the head value
func MapHead[B, A, A1 any](f func(A) A1) func(Pair[A, B]) Pair[A1, B] {
return F.Bind2nd(MonadMapHead[B, A, A1], f)
}
// MapTail maps the Tail value
func MapTail[A, B, B1 any](f func(B) B1) func(Pair[A, B]) Pair[A, B1] {
return F.Bind2nd(MonadMapTail[A, B, B1], f)
}
// BiMap maps both values
func BiMap[A, B, A1, B1 any](f func(A) A1, g func(B) B1) func(Pair[A, B]) Pair[A1, B1] {
return func(fa Pair[A, B]) Pair[A1, B1] {
return MonadBiMap(fa, f, g)
}
}
// MonadChainHead chains on the head value
func MonadChainHead[B, A, A1 any](sg Sg.Semigroup[B], fa Pair[A, B], f func(A) Pair[A1, B]) Pair[A1, B] {
fb := f(Head(fa))
return Pair[A1, B]{fb.head, sg.Concat(Tail(fa), Tail(fb))}
}
// MonadChainTail chains on the Tail value
func MonadChainTail[A, B, B1 any](sg Sg.Semigroup[A], fb Pair[A, B], f func(B) Pair[A, B1]) Pair[A, B1] {
fa := f(Tail(fb))
return Pair[A, B1]{sg.Concat(Head(fb), Head(fa)), fa.Tail}
}
// MonadChain chains on the head value
func MonadChain[B, A, A1 any](sg Sg.Semigroup[B], fa Pair[A, B], f func(A) Pair[A1, B]) Pair[A1, B] {
return MonadChainHead(sg, fa, f)
}
// ChainHead chains on the head value
func ChainHead[B, A, A1 any](sg Sg.Semigroup[B], f func(A) Pair[A1, B]) func(Pair[A, B]) Pair[A1, B] {
return func(fa Pair[A, B]) Pair[A1, B] {
return MonadChainHead(sg, fa, f)
}
}
// ChainTail chains on the Tail value
func ChainTail[A, B, B1 any](sg Sg.Semigroup[A], f func(B) Pair[A, B1]) func(Pair[A, B]) Pair[A, B1] {
return func(fa Pair[A, B]) Pair[A, B1] {
return MonadChainTail(sg, fa, f)
}
}
// Chain chains on the head value
func Chain[B, A, A1 any](sg Sg.Semigroup[B], f func(A) Pair[A1, B]) func(Pair[A, B]) Pair[A1, B] {
return ChainHead[B, A, A1](sg, f)
}
// MonadApHead applies on the head value
func MonadApHead[B, A, A1 any](sg Sg.Semigroup[B], faa Pair[func(A) A1, B], fa Pair[A, B]) Pair[A1, B] {
return Pair[A1, B]{Head(faa)(Head(fa)), sg.Concat(Tail(fa), Tail(faa))}
}
// MonadApTail applies on the Tail value
func MonadApTail[A, B, B1 any](sg Sg.Semigroup[A], fbb Pair[A, func(B) B1], fb Pair[A, B]) Pair[A, B1] {
return Pair[A, B1]{sg.Concat(Head(fb), Head(fbb)), Tail(fbb)(Tail(fb))}
}
// MonadAp applies on the head value
func MonadAp[B, A, A1 any](sg Sg.Semigroup[B], faa Pair[func(A) A1, B], fa Pair[A, B]) Pair[A1, B] {
return MonadApHead(sg, faa, fa)
}
// ApHead applies on the head value
func ApHead[B, A, A1 any](sg Sg.Semigroup[B], fa Pair[A, B]) func(Pair[func(A) A1, B]) Pair[A1, B] {
return func(faa Pair[func(A) A1, B]) Pair[A1, B] {
return MonadApHead(sg, faa, fa)
}
}
// ApTail applies on the Tail value
func ApTail[A, B, B1 any](sg Sg.Semigroup[A], fb Pair[A, B]) func(Pair[A, func(B) B1]) Pair[A, B1] {
return func(fbb Pair[A, func(B) B1]) Pair[A, B1] {
return MonadApTail(sg, fbb, fb)
}
}
// Ap applies on the head value
func Ap[B, A, A1 any](sg Sg.Semigroup[B], fa Pair[A, B]) func(Pair[func(A) A1, B]) Pair[A1, B] {
return ApHead[B, A, A1](sg, fa)
}

View File

@@ -1,155 +0,0 @@
// Copyright (c) 2024 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 testing
import (
"testing"
EQ "github.com/IBM/fp-go/eq"
L "github.com/IBM/fp-go/internal/monad/testing"
P "github.com/IBM/fp-go/pair"
M "github.com/IBM/fp-go/monoid"
)
// AssertLaws asserts the apply monad laws for the [P.Pair] monad
func assertLawsHead[E, A, B, C any](t *testing.T,
m M.Monoid[E],
eqe EQ.Eq[E],
eqa EQ.Eq[A],
eqb EQ.Eq[B],
eqc EQ.Eq[C],
ab func(A) B,
bc func(B) C,
) func(a A) bool {
fofc := P.Pointed[C](m)
fofaa := P.Pointed[func(A) A](m)
fofbc := P.Pointed[func(B) C](m)
fofabb := P.Pointed[func(func(A) B) B](m)
fmap := P.Functor[func(B) C, E, func(func(A) B) func(A) C]()
fapabb := P.Applicative[func(A) B, E, B](m)
fapabac := P.Applicative[func(A) B, E, func(A) C](m)
maa := P.Monad[A, E, A](m)
mab := P.Monad[A, E, B](m)
mac := P.Monad[A, E, C](m)
mbc := P.Monad[B, E, C](m)
return L.MonadAssertLaws(t,
P.Eq(eqa, eqe),
P.Eq(eqb, eqe),
P.Eq(eqc, eqe),
fofc,
fofaa,
fofbc,
fofabb,
fmap,
fapabb,
fapabac,
maa,
mab,
mac,
mbc,
ab,
bc,
)
}
// AssertLaws asserts the apply monad laws for the [P.Pair] monad
func assertLawsTail[E, A, B, C any](t *testing.T,
m M.Monoid[E],
eqe EQ.Eq[E],
eqa EQ.Eq[A],
eqb EQ.Eq[B],
eqc EQ.Eq[C],
ab func(A) B,
bc func(B) C,
) func(a A) bool {
fofc := P.PointedTail[C](m)
fofaa := P.PointedTail[func(A) A](m)
fofbc := P.PointedTail[func(B) C](m)
fofabb := P.PointedTail[func(func(A) B) B](m)
fmap := P.FunctorTail[func(B) C, E, func(func(A) B) func(A) C]()
fapabb := P.ApplicativeTail[func(A) B, E, B](m)
fapabac := P.ApplicativeTail[func(A) B, E, func(A) C](m)
maa := P.MonadTail[A, E, A](m)
mab := P.MonadTail[A, E, B](m)
mac := P.MonadTail[A, E, C](m)
mbc := P.MonadTail[B, E, C](m)
return L.MonadAssertLaws(t,
P.Eq(eqe, eqa),
P.Eq(eqe, eqb),
P.Eq(eqe, eqc),
fofc,
fofaa,
fofbc,
fofabb,
fmap,
fapabb,
fapabac,
maa,
mab,
mac,
mbc,
ab,
bc,
)
}
// AssertLaws asserts the apply monad laws for the [P.Pair] monad
func AssertLaws[E, A, B, C any](t *testing.T,
m M.Monoid[E],
eqe EQ.Eq[E],
eqa EQ.Eq[A],
eqb EQ.Eq[B],
eqc EQ.Eq[C],
ab func(A) B,
bc func(B) C,
) func(A) bool {
head := assertLawsHead(t, m, eqe, eqa, eqb, eqc, ab, bc)
tail := assertLawsHead(t, m, eqe, eqa, eqb, eqc, ab, bc)
return func(a A) bool {
return head(a) && tail(a)
}
}

View File

@@ -1,51 +0,0 @@
// 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 testing
import (
"fmt"
"testing"
EQ "github.com/IBM/fp-go/eq"
S "github.com/IBM/fp-go/string"
"github.com/stretchr/testify/assert"
)
func TestMonadLaws(t *testing.T) {
// some comparison
eqe := EQ.FromStrictEquals[string]()
eqa := EQ.FromStrictEquals[bool]()
eqb := EQ.FromStrictEquals[int]()
eqc := EQ.FromStrictEquals[string]()
m := S.Monoid
ab := func(a bool) int {
if a {
return 1
}
return 0
}
bc := func(b int) string {
return fmt.Sprintf("value %d", b)
}
laws := AssertLaws(t, m, eqe, eqa, eqb, eqc, ab, bc)
assert.True(t, laws(true))
assert.True(t, laws(false))
}

View File

@@ -1,10 +1,12 @@
// Code generated by go generate; DO NOT EDIT.
// This file was generated by robots at
// 2024-02-08 08:36:32.8883679 +0100 CET m=+0.008054801
// 2023-10-23 08:31:19.0449107 +0200 CEST m=+0.023307601
package tuple
import (
"encoding/json"
"fmt"
M "github.com/IBM/fp-go/monoid"
O "github.com/IBM/fp-go/ord"
)
@@ -160,17 +162,27 @@ func Replicate1[T any](t T) Tuple1[T] {
// String prints some debug info for the [Tuple1]
func (t Tuple1[T1]) String() string {
return tupleString(t.F1)
return fmt.Sprintf("Tuple1[%T](%v)", t.F1, t.F1)
}
// MarshalJSON marshals the [Tuple1] into a JSON array
func (t Tuple1[T1]) MarshalJSON() ([]byte, error) {
return tupleMarshalJSON(t.F1)
return json.Marshal([]any{t.F1})
}
// UnmarshalJSON unmarshals a JSON array into a [Tuple1]
func (t *Tuple1[T1]) UnmarshalJSON(data []byte) error {
return tupleUnmarshalJSON(data, &t.F1)
var tmp []json.RawMessage
if err := json.Unmarshal(data, &tmp); err != nil {
return err
}
l := len(tmp)
if l > 0 {
if err := json.Unmarshal(tmp[0], &t.F1); err != nil {
return err
}
}
return nil
}
// ToArray converts the [Tuple1] into an array of type [R] using 1 transformation functions from [T] to [R]
@@ -260,17 +272,32 @@ func Replicate2[T any](t T) Tuple2[T, T] {
// String prints some debug info for the [Tuple2]
func (t Tuple2[T1, T2]) String() string {
return tupleString(t.F1, t.F2)
return fmt.Sprintf("Tuple2[%T, %T](%v, %v)", t.F1, t.F2, t.F1, t.F2)
}
// MarshalJSON marshals the [Tuple2] into a JSON array
func (t Tuple2[T1, T2]) MarshalJSON() ([]byte, error) {
return tupleMarshalJSON(t.F1, t.F2)
return json.Marshal([]any{t.F1, t.F2})
}
// UnmarshalJSON unmarshals a JSON array into a [Tuple2]
func (t *Tuple2[T1, T2]) UnmarshalJSON(data []byte) error {
return tupleUnmarshalJSON(data, &t.F1, &t.F2)
var tmp []json.RawMessage
if err := json.Unmarshal(data, &tmp); err != nil {
return err
}
l := len(tmp)
if l > 0 {
if err := json.Unmarshal(tmp[0], &t.F1); err != nil {
return err
}
if l > 1 {
if err := json.Unmarshal(tmp[1], &t.F2); err != nil {
return err
}
}
}
return nil
}
// ToArray converts the [Tuple2] into an array of type [R] using 2 transformation functions from [T] to [R]
@@ -366,17 +393,37 @@ func Replicate3[T any](t T) Tuple3[T, T, T] {
// String prints some debug info for the [Tuple3]
func (t Tuple3[T1, T2, T3]) String() string {
return tupleString(t.F1, t.F2, t.F3)
return fmt.Sprintf("Tuple3[%T, %T, %T](%v, %v, %v)", t.F1, t.F2, t.F3, t.F1, t.F2, t.F3)
}
// MarshalJSON marshals the [Tuple3] into a JSON array
func (t Tuple3[T1, T2, T3]) MarshalJSON() ([]byte, error) {
return tupleMarshalJSON(t.F1, t.F2, t.F3)
return json.Marshal([]any{t.F1, t.F2, t.F3})
}
// UnmarshalJSON unmarshals a JSON array into a [Tuple3]
func (t *Tuple3[T1, T2, T3]) UnmarshalJSON(data []byte) error {
return tupleUnmarshalJSON(data, &t.F1, &t.F2, &t.F3)
var tmp []json.RawMessage
if err := json.Unmarshal(data, &tmp); err != nil {
return err
}
l := len(tmp)
if l > 0 {
if err := json.Unmarshal(tmp[0], &t.F1); err != nil {
return err
}
if l > 1 {
if err := json.Unmarshal(tmp[1], &t.F2); err != nil {
return err
}
if l > 2 {
if err := json.Unmarshal(tmp[2], &t.F3); err != nil {
return err
}
}
}
}
return nil
}
// ToArray converts the [Tuple3] into an array of type [R] using 3 transformation functions from [T] to [R]
@@ -478,17 +525,42 @@ func Replicate4[T any](t T) Tuple4[T, T, T, T] {
// String prints some debug info for the [Tuple4]
func (t Tuple4[T1, T2, T3, T4]) String() string {
return tupleString(t.F1, t.F2, t.F3, t.F4)
return fmt.Sprintf("Tuple4[%T, %T, %T, %T](%v, %v, %v, %v)", t.F1, t.F2, t.F3, t.F4, t.F1, t.F2, t.F3, t.F4)
}
// MarshalJSON marshals the [Tuple4] into a JSON array
func (t Tuple4[T1, T2, T3, T4]) MarshalJSON() ([]byte, error) {
return tupleMarshalJSON(t.F1, t.F2, t.F3, t.F4)
return json.Marshal([]any{t.F1, t.F2, t.F3, t.F4})
}
// UnmarshalJSON unmarshals a JSON array into a [Tuple4]
func (t *Tuple4[T1, T2, T3, T4]) UnmarshalJSON(data []byte) error {
return tupleUnmarshalJSON(data, &t.F1, &t.F2, &t.F3, &t.F4)
var tmp []json.RawMessage
if err := json.Unmarshal(data, &tmp); err != nil {
return err
}
l := len(tmp)
if l > 0 {
if err := json.Unmarshal(tmp[0], &t.F1); err != nil {
return err
}
if l > 1 {
if err := json.Unmarshal(tmp[1], &t.F2); err != nil {
return err
}
if l > 2 {
if err := json.Unmarshal(tmp[2], &t.F3); err != nil {
return err
}
if l > 3 {
if err := json.Unmarshal(tmp[3], &t.F4); err != nil {
return err
}
}
}
}
}
return nil
}
// ToArray converts the [Tuple4] into an array of type [R] using 4 transformation functions from [T] to [R]
@@ -596,17 +668,47 @@ func Replicate5[T any](t T) Tuple5[T, T, T, T, T] {
// String prints some debug info for the [Tuple5]
func (t Tuple5[T1, T2, T3, T4, T5]) String() string {
return tupleString(t.F1, t.F2, t.F3, t.F4, t.F5)
return fmt.Sprintf("Tuple5[%T, %T, %T, %T, %T](%v, %v, %v, %v, %v)", t.F1, t.F2, t.F3, t.F4, t.F5, t.F1, t.F2, t.F3, t.F4, t.F5)
}
// MarshalJSON marshals the [Tuple5] into a JSON array
func (t Tuple5[T1, T2, T3, T4, T5]) MarshalJSON() ([]byte, error) {
return tupleMarshalJSON(t.F1, t.F2, t.F3, t.F4, t.F5)
return json.Marshal([]any{t.F1, t.F2, t.F3, t.F4, t.F5})
}
// UnmarshalJSON unmarshals a JSON array into a [Tuple5]
func (t *Tuple5[T1, T2, T3, T4, T5]) UnmarshalJSON(data []byte) error {
return tupleUnmarshalJSON(data, &t.F1, &t.F2, &t.F3, &t.F4, &t.F5)
var tmp []json.RawMessage
if err := json.Unmarshal(data, &tmp); err != nil {
return err
}
l := len(tmp)
if l > 0 {
if err := json.Unmarshal(tmp[0], &t.F1); err != nil {
return err
}
if l > 1 {
if err := json.Unmarshal(tmp[1], &t.F2); err != nil {
return err
}
if l > 2 {
if err := json.Unmarshal(tmp[2], &t.F3); err != nil {
return err
}
if l > 3 {
if err := json.Unmarshal(tmp[3], &t.F4); err != nil {
return err
}
if l > 4 {
if err := json.Unmarshal(tmp[4], &t.F5); err != nil {
return err
}
}
}
}
}
}
return nil
}
// ToArray converts the [Tuple5] into an array of type [R] using 5 transformation functions from [T] to [R]
@@ -720,17 +822,52 @@ func Replicate6[T any](t T) Tuple6[T, T, T, T, T, T] {
// String prints some debug info for the [Tuple6]
func (t Tuple6[T1, T2, T3, T4, T5, T6]) String() string {
return tupleString(t.F1, t.F2, t.F3, t.F4, t.F5, t.F6)
return fmt.Sprintf("Tuple6[%T, %T, %T, %T, %T, %T](%v, %v, %v, %v, %v, %v)", t.F1, t.F2, t.F3, t.F4, t.F5, t.F6, t.F1, t.F2, t.F3, t.F4, t.F5, t.F6)
}
// MarshalJSON marshals the [Tuple6] into a JSON array
func (t Tuple6[T1, T2, T3, T4, T5, T6]) MarshalJSON() ([]byte, error) {
return tupleMarshalJSON(t.F1, t.F2, t.F3, t.F4, t.F5, t.F6)
return json.Marshal([]any{t.F1, t.F2, t.F3, t.F4, t.F5, t.F6})
}
// UnmarshalJSON unmarshals a JSON array into a [Tuple6]
func (t *Tuple6[T1, T2, T3, T4, T5, T6]) UnmarshalJSON(data []byte) error {
return tupleUnmarshalJSON(data, &t.F1, &t.F2, &t.F3, &t.F4, &t.F5, &t.F6)
var tmp []json.RawMessage
if err := json.Unmarshal(data, &tmp); err != nil {
return err
}
l := len(tmp)
if l > 0 {
if err := json.Unmarshal(tmp[0], &t.F1); err != nil {
return err
}
if l > 1 {
if err := json.Unmarshal(tmp[1], &t.F2); err != nil {
return err
}
if l > 2 {
if err := json.Unmarshal(tmp[2], &t.F3); err != nil {
return err
}
if l > 3 {
if err := json.Unmarshal(tmp[3], &t.F4); err != nil {
return err
}
if l > 4 {
if err := json.Unmarshal(tmp[4], &t.F5); err != nil {
return err
}
if l > 5 {
if err := json.Unmarshal(tmp[5], &t.F6); err != nil {
return err
}
}
}
}
}
}
}
return nil
}
// ToArray converts the [Tuple6] into an array of type [R] using 6 transformation functions from [T] to [R]
@@ -850,17 +987,57 @@ func Replicate7[T any](t T) Tuple7[T, T, T, T, T, T, T] {
// String prints some debug info for the [Tuple7]
func (t Tuple7[T1, T2, T3, T4, T5, T6, T7]) String() string {
return tupleString(t.F1, t.F2, t.F3, t.F4, t.F5, t.F6, t.F7)
return fmt.Sprintf("Tuple7[%T, %T, %T, %T, %T, %T, %T](%v, %v, %v, %v, %v, %v, %v)", t.F1, t.F2, t.F3, t.F4, t.F5, t.F6, t.F7, t.F1, t.F2, t.F3, t.F4, t.F5, t.F6, t.F7)
}
// MarshalJSON marshals the [Tuple7] into a JSON array
func (t Tuple7[T1, T2, T3, T4, T5, T6, T7]) MarshalJSON() ([]byte, error) {
return tupleMarshalJSON(t.F1, t.F2, t.F3, t.F4, t.F5, t.F6, t.F7)
return json.Marshal([]any{t.F1, t.F2, t.F3, t.F4, t.F5, t.F6, t.F7})
}
// UnmarshalJSON unmarshals a JSON array into a [Tuple7]
func (t *Tuple7[T1, T2, T3, T4, T5, T6, T7]) UnmarshalJSON(data []byte) error {
return tupleUnmarshalJSON(data, &t.F1, &t.F2, &t.F3, &t.F4, &t.F5, &t.F6, &t.F7)
var tmp []json.RawMessage
if err := json.Unmarshal(data, &tmp); err != nil {
return err
}
l := len(tmp)
if l > 0 {
if err := json.Unmarshal(tmp[0], &t.F1); err != nil {
return err
}
if l > 1 {
if err := json.Unmarshal(tmp[1], &t.F2); err != nil {
return err
}
if l > 2 {
if err := json.Unmarshal(tmp[2], &t.F3); err != nil {
return err
}
if l > 3 {
if err := json.Unmarshal(tmp[3], &t.F4); err != nil {
return err
}
if l > 4 {
if err := json.Unmarshal(tmp[4], &t.F5); err != nil {
return err
}
if l > 5 {
if err := json.Unmarshal(tmp[5], &t.F6); err != nil {
return err
}
if l > 6 {
if err := json.Unmarshal(tmp[6], &t.F7); err != nil {
return err
}
}
}
}
}
}
}
}
return nil
}
// ToArray converts the [Tuple7] into an array of type [R] using 7 transformation functions from [T] to [R]
@@ -986,17 +1163,62 @@ func Replicate8[T any](t T) Tuple8[T, T, T, T, T, T, T, T] {
// String prints some debug info for the [Tuple8]
func (t Tuple8[T1, T2, T3, T4, T5, T6, T7, T8]) String() string {
return tupleString(t.F1, t.F2, t.F3, t.F4, t.F5, t.F6, t.F7, t.F8)
return fmt.Sprintf("Tuple8[%T, %T, %T, %T, %T, %T, %T, %T](%v, %v, %v, %v, %v, %v, %v, %v)", t.F1, t.F2, t.F3, t.F4, t.F5, t.F6, t.F7, t.F8, t.F1, t.F2, t.F3, t.F4, t.F5, t.F6, t.F7, t.F8)
}
// MarshalJSON marshals the [Tuple8] into a JSON array
func (t Tuple8[T1, T2, T3, T4, T5, T6, T7, T8]) MarshalJSON() ([]byte, error) {
return tupleMarshalJSON(t.F1, t.F2, t.F3, t.F4, t.F5, t.F6, t.F7, t.F8)
return json.Marshal([]any{t.F1, t.F2, t.F3, t.F4, t.F5, t.F6, t.F7, t.F8})
}
// UnmarshalJSON unmarshals a JSON array into a [Tuple8]
func (t *Tuple8[T1, T2, T3, T4, T5, T6, T7, T8]) UnmarshalJSON(data []byte) error {
return tupleUnmarshalJSON(data, &t.F1, &t.F2, &t.F3, &t.F4, &t.F5, &t.F6, &t.F7, &t.F8)
var tmp []json.RawMessage
if err := json.Unmarshal(data, &tmp); err != nil {
return err
}
l := len(tmp)
if l > 0 {
if err := json.Unmarshal(tmp[0], &t.F1); err != nil {
return err
}
if l > 1 {
if err := json.Unmarshal(tmp[1], &t.F2); err != nil {
return err
}
if l > 2 {
if err := json.Unmarshal(tmp[2], &t.F3); err != nil {
return err
}
if l > 3 {
if err := json.Unmarshal(tmp[3], &t.F4); err != nil {
return err
}
if l > 4 {
if err := json.Unmarshal(tmp[4], &t.F5); err != nil {
return err
}
if l > 5 {
if err := json.Unmarshal(tmp[5], &t.F6); err != nil {
return err
}
if l > 6 {
if err := json.Unmarshal(tmp[6], &t.F7); err != nil {
return err
}
if l > 7 {
if err := json.Unmarshal(tmp[7], &t.F8); err != nil {
return err
}
}
}
}
}
}
}
}
}
return nil
}
// ToArray converts the [Tuple8] into an array of type [R] using 8 transformation functions from [T] to [R]
@@ -1128,17 +1350,67 @@ func Replicate9[T any](t T) Tuple9[T, T, T, T, T, T, T, T, T] {
// String prints some debug info for the [Tuple9]
func (t Tuple9[T1, T2, T3, T4, T5, T6, T7, T8, T9]) String() string {
return tupleString(t.F1, t.F2, t.F3, t.F4, t.F5, t.F6, t.F7, t.F8, t.F9)
return fmt.Sprintf("Tuple9[%T, %T, %T, %T, %T, %T, %T, %T, %T](%v, %v, %v, %v, %v, %v, %v, %v, %v)", t.F1, t.F2, t.F3, t.F4, t.F5, t.F6, t.F7, t.F8, t.F9, t.F1, t.F2, t.F3, t.F4, t.F5, t.F6, t.F7, t.F8, t.F9)
}
// MarshalJSON marshals the [Tuple9] into a JSON array
func (t Tuple9[T1, T2, T3, T4, T5, T6, T7, T8, T9]) MarshalJSON() ([]byte, error) {
return tupleMarshalJSON(t.F1, t.F2, t.F3, t.F4, t.F5, t.F6, t.F7, t.F8, t.F9)
return json.Marshal([]any{t.F1, t.F2, t.F3, t.F4, t.F5, t.F6, t.F7, t.F8, t.F9})
}
// UnmarshalJSON unmarshals a JSON array into a [Tuple9]
func (t *Tuple9[T1, T2, T3, T4, T5, T6, T7, T8, T9]) UnmarshalJSON(data []byte) error {
return tupleUnmarshalJSON(data, &t.F1, &t.F2, &t.F3, &t.F4, &t.F5, &t.F6, &t.F7, &t.F8, &t.F9)
var tmp []json.RawMessage
if err := json.Unmarshal(data, &tmp); err != nil {
return err
}
l := len(tmp)
if l > 0 {
if err := json.Unmarshal(tmp[0], &t.F1); err != nil {
return err
}
if l > 1 {
if err := json.Unmarshal(tmp[1], &t.F2); err != nil {
return err
}
if l > 2 {
if err := json.Unmarshal(tmp[2], &t.F3); err != nil {
return err
}
if l > 3 {
if err := json.Unmarshal(tmp[3], &t.F4); err != nil {
return err
}
if l > 4 {
if err := json.Unmarshal(tmp[4], &t.F5); err != nil {
return err
}
if l > 5 {
if err := json.Unmarshal(tmp[5], &t.F6); err != nil {
return err
}
if l > 6 {
if err := json.Unmarshal(tmp[6], &t.F7); err != nil {
return err
}
if l > 7 {
if err := json.Unmarshal(tmp[7], &t.F8); err != nil {
return err
}
if l > 8 {
if err := json.Unmarshal(tmp[8], &t.F9); err != nil {
return err
}
}
}
}
}
}
}
}
}
}
return nil
}
// ToArray converts the [Tuple9] into an array of type [R] using 9 transformation functions from [T] to [R]
@@ -1276,17 +1548,72 @@ func Replicate10[T any](t T) Tuple10[T, T, T, T, T, T, T, T, T, T] {
// String prints some debug info for the [Tuple10]
func (t Tuple10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]) String() string {
return tupleString(t.F1, t.F2, t.F3, t.F4, t.F5, t.F6, t.F7, t.F8, t.F9, t.F10)
return fmt.Sprintf("Tuple10[%T, %T, %T, %T, %T, %T, %T, %T, %T, %T](%v, %v, %v, %v, %v, %v, %v, %v, %v, %v)", t.F1, t.F2, t.F3, t.F4, t.F5, t.F6, t.F7, t.F8, t.F9, t.F10, t.F1, t.F2, t.F3, t.F4, t.F5, t.F6, t.F7, t.F8, t.F9, t.F10)
}
// MarshalJSON marshals the [Tuple10] into a JSON array
func (t Tuple10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]) MarshalJSON() ([]byte, error) {
return tupleMarshalJSON(t.F1, t.F2, t.F3, t.F4, t.F5, t.F6, t.F7, t.F8, t.F9, t.F10)
return json.Marshal([]any{t.F1, t.F2, t.F3, t.F4, t.F5, t.F6, t.F7, t.F8, t.F9, t.F10})
}
// UnmarshalJSON unmarshals a JSON array into a [Tuple10]
func (t *Tuple10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]) UnmarshalJSON(data []byte) error {
return tupleUnmarshalJSON(data, &t.F1, &t.F2, &t.F3, &t.F4, &t.F5, &t.F6, &t.F7, &t.F8, &t.F9, &t.F10)
var tmp []json.RawMessage
if err := json.Unmarshal(data, &tmp); err != nil {
return err
}
l := len(tmp)
if l > 0 {
if err := json.Unmarshal(tmp[0], &t.F1); err != nil {
return err
}
if l > 1 {
if err := json.Unmarshal(tmp[1], &t.F2); err != nil {
return err
}
if l > 2 {
if err := json.Unmarshal(tmp[2], &t.F3); err != nil {
return err
}
if l > 3 {
if err := json.Unmarshal(tmp[3], &t.F4); err != nil {
return err
}
if l > 4 {
if err := json.Unmarshal(tmp[4], &t.F5); err != nil {
return err
}
if l > 5 {
if err := json.Unmarshal(tmp[5], &t.F6); err != nil {
return err
}
if l > 6 {
if err := json.Unmarshal(tmp[6], &t.F7); err != nil {
return err
}
if l > 7 {
if err := json.Unmarshal(tmp[7], &t.F8); err != nil {
return err
}
if l > 8 {
if err := json.Unmarshal(tmp[8], &t.F9); err != nil {
return err
}
if l > 9 {
if err := json.Unmarshal(tmp[9], &t.F10); err != nil {
return err
}
}
}
}
}
}
}
}
}
}
}
return nil
}
// ToArray converts the [Tuple10] into an array of type [R] using 10 transformation functions from [T] to [R]

View File

@@ -17,14 +17,6 @@
// 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)
}
@@ -54,31 +46,3 @@ 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...))
}