mirror of
https://github.com/IBM/fp-go.git
synced 2026-01-15 00:53:10 +02:00
Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
380ba2853c | ||
|
|
c18e5e2107 | ||
|
|
89766bdb26 | ||
|
|
21d116d325 | ||
|
|
7f2e76dd94 | ||
|
|
77965a12ff | ||
|
|
ed77bd7971 |
@@ -519,6 +519,8 @@ func RunAll(testcases map[string]Reader) Reader {
|
||||
// by providing a function that converts R2 to R1. This allows you to focus a test on a
|
||||
// specific property or subset of a larger data structure.
|
||||
//
|
||||
// See: https://github.com/fantasyland/fantasy-land?tab=readme-ov-file#profunctor
|
||||
//
|
||||
// This is particularly useful when you have an assertion that operates on a specific field
|
||||
// or property, and you want to apply it to a complete object. Instead of extracting the
|
||||
// property and then asserting on it, you can transform the assertion to work directly
|
||||
|
||||
@@ -19,11 +19,13 @@ package consumer
|
||||
// This is the contravariant map operation for Consumers, analogous to reader.Local
|
||||
// but operating on the input side rather than the output side.
|
||||
//
|
||||
// See: https://github.com/fantasyland/fantasy-land?tab=readme-ov-file#profunctor
|
||||
//
|
||||
// Given a Consumer[R1] that consumes values of type R1, and a function f that
|
||||
// converts R2 to R1, Local creates a new Consumer[R2] that:
|
||||
// 1. Takes a value of type R2
|
||||
// 2. Applies f to convert it to R1
|
||||
// 3. Passes the result to the original Consumer[R1]
|
||||
// 1. Takes a value of type R2
|
||||
// 2. Applies f to convert it to R1
|
||||
// 3. Passes the result to the original Consumer[R1]
|
||||
//
|
||||
// This is particularly useful for adapting consumers to work with different input types,
|
||||
// similar to how reader.Local adapts readers to work with different environment types.
|
||||
@@ -168,7 +170,7 @@ package consumer
|
||||
// - reader.Local transforms the environment before reading
|
||||
// - consumer.Local transforms the input before consuming
|
||||
// - Both are contravariant functors on their input type
|
||||
func Local[R2, R1 any](f func(R2) R1) Operator[R1, R2] {
|
||||
func Local[R1, R2 any](f func(R2) R1) Operator[R1, R2] {
|
||||
return func(c Consumer[R1]) Consumer[R2] {
|
||||
return func(r2 R2) {
|
||||
c(f(r2))
|
||||
|
||||
74
v2/context/readerio/profunctor.go
Normal file
74
v2/context/readerio/profunctor.go
Normal file
@@ -0,0 +1,74 @@
|
||||
// Copyright (c) 2025 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 readerio
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/IBM/fp-go/v2/function"
|
||||
)
|
||||
|
||||
// Promap is the profunctor map operation that transforms both the input and output of a context-based ReaderIO.
|
||||
// It applies f to the input context (contravariantly) and g to the output value (covariantly).
|
||||
//
|
||||
// See: https://github.com/fantasyland/fantasy-land?tab=readme-ov-file#profunctor
|
||||
//
|
||||
// This operation allows you to:
|
||||
// - Modify the context before passing it to the ReaderIO (via f)
|
||||
// - Transform the result value after the IO effect completes (via g)
|
||||
//
|
||||
// The function f returns both a new context and a CancelFunc that should be called to release resources.
|
||||
//
|
||||
// Type Parameters:
|
||||
// - A: The original result type produced by the ReaderIO
|
||||
// - B: The new output result type
|
||||
//
|
||||
// Parameters:
|
||||
// - f: Function to transform the input context (contravariant)
|
||||
// - g: Function to transform the output value from A to B (covariant)
|
||||
//
|
||||
// Returns:
|
||||
// - An Operator that takes a ReaderIO[A] and returns a ReaderIO[B]
|
||||
//
|
||||
//go:inline
|
||||
func Promap[A, B any](f func(context.Context) (context.Context, context.CancelFunc), g func(A) B) Operator[A, B] {
|
||||
return function.Flow2(
|
||||
Local[A](f),
|
||||
Map(g),
|
||||
)
|
||||
}
|
||||
|
||||
// Contramap changes the context during the execution of a ReaderIO.
|
||||
// This is the contravariant functor operation that transforms the input context.
|
||||
//
|
||||
// See: https://github.com/fantasyland/fantasy-land?tab=readme-ov-file#profunctor
|
||||
//
|
||||
// Contramap is an alias for Local and is useful for adapting a ReaderIO to work with
|
||||
// a modified context by providing a function that transforms the context.
|
||||
//
|
||||
// Type Parameters:
|
||||
// - A: The result type (unchanged)
|
||||
//
|
||||
// Parameters:
|
||||
// - f: Function to transform the context, returning a new context and CancelFunc
|
||||
//
|
||||
// Returns:
|
||||
// - An Operator that takes a ReaderIO[A] and returns a ReaderIO[A]
|
||||
//
|
||||
//go:inline
|
||||
func Contramap[A any](f func(context.Context) (context.Context, context.CancelFunc)) Operator[A, A] {
|
||||
return Local[A](f)
|
||||
}
|
||||
97
v2/context/readerio/profunctor_test.go
Normal file
97
v2/context/readerio/profunctor_test.go
Normal file
@@ -0,0 +1,97 @@
|
||||
// Copyright (c) 2025 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 readerio
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// TestPromapBasic tests basic Promap functionality
|
||||
func TestPromapBasic(t *testing.T) {
|
||||
t.Run("transform both context and output", func(t *testing.T) {
|
||||
// ReaderIO that reads a value from context
|
||||
getValue := func(ctx context.Context) IO[int] {
|
||||
return func() int {
|
||||
if v := ctx.Value("key"); v != nil {
|
||||
return v.(int)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
// Transform context and result
|
||||
addKey := func(ctx context.Context) (context.Context, context.CancelFunc) {
|
||||
newCtx := context.WithValue(ctx, "key", 42)
|
||||
return newCtx, func() {}
|
||||
}
|
||||
toString := strconv.Itoa
|
||||
|
||||
adapted := Promap(addKey, toString)(getValue)
|
||||
result := adapted(context.Background())()
|
||||
|
||||
assert.Equal(t, "42", result)
|
||||
})
|
||||
}
|
||||
|
||||
// TestContramapBasic tests basic Contramap functionality
|
||||
func TestContramapBasic(t *testing.T) {
|
||||
t.Run("context transformation", func(t *testing.T) {
|
||||
getValue := func(ctx context.Context) IO[int] {
|
||||
return func() int {
|
||||
if v := ctx.Value("key"); v != nil {
|
||||
return v.(int)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
addKey := func(ctx context.Context) (context.Context, context.CancelFunc) {
|
||||
newCtx := context.WithValue(ctx, "key", 100)
|
||||
return newCtx, func() {}
|
||||
}
|
||||
|
||||
adapted := Contramap[int](addKey)(getValue)
|
||||
result := adapted(context.Background())()
|
||||
|
||||
assert.Equal(t, 100, result)
|
||||
})
|
||||
}
|
||||
|
||||
// TestLocalBasic tests basic Local functionality
|
||||
func TestLocalBasic(t *testing.T) {
|
||||
t.Run("adds timeout to context", func(t *testing.T) {
|
||||
getValue := func(ctx context.Context) IO[bool] {
|
||||
return func() bool {
|
||||
_, hasDeadline := ctx.Deadline()
|
||||
return hasDeadline
|
||||
}
|
||||
}
|
||||
|
||||
addTimeout := func(ctx context.Context) (context.Context, context.CancelFunc) {
|
||||
return context.WithTimeout(ctx, time.Second)
|
||||
}
|
||||
|
||||
adapted := Local[bool](addTimeout)(getValue)
|
||||
result := adapted(context.Background())()
|
||||
|
||||
assert.True(t, result)
|
||||
})
|
||||
}
|
||||
@@ -608,7 +608,7 @@ func TestCircuitBreaker_ErrorMessageFormat(t *testing.T) {
|
||||
protectedOp := pair.Tail(resultEnv)
|
||||
outcome := protectedOp(ctx)()
|
||||
|
||||
assert.True(t, result.IsLeft[string](outcome))
|
||||
assert.True(t, result.IsLeft(outcome))
|
||||
|
||||
// Error message should indicate circuit breaker is open
|
||||
_, err := result.Unwrap(outcome)
|
||||
|
||||
75
v2/context/readerioresult/profunctor.go
Normal file
75
v2/context/readerioresult/profunctor.go
Normal file
@@ -0,0 +1,75 @@
|
||||
// Copyright (c) 2025 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 readerioresult
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/IBM/fp-go/v2/function"
|
||||
)
|
||||
|
||||
// Promap is the profunctor map operation that transforms both the input and output of a context-based ReaderIOResult.
|
||||
// It applies f to the input context (contravariantly) and g to the output value (covariantly).
|
||||
//
|
||||
// See: https://github.com/fantasyland/fantasy-land?tab=readme-ov-file#profunctor
|
||||
//
|
||||
// This operation allows you to:
|
||||
// - Modify the context before passing it to the ReaderIOResult (via f)
|
||||
// - Transform the success value after the IO effect completes (via g)
|
||||
//
|
||||
// The function f returns both a new context and a CancelFunc that should be called to release resources.
|
||||
// The error type is fixed as error and remains unchanged through the transformation.
|
||||
//
|
||||
// Type Parameters:
|
||||
// - A: The original success type produced by the ReaderIOResult
|
||||
// - B: The new output success type
|
||||
//
|
||||
// Parameters:
|
||||
// - f: Function to transform the input context (contravariant)
|
||||
// - g: Function to transform the output success value from A to B (covariant)
|
||||
//
|
||||
// Returns:
|
||||
// - An Operator that takes a ReaderIOResult[A] and returns a ReaderIOResult[B]
|
||||
//
|
||||
//go:inline
|
||||
func Promap[A, B any](f func(context.Context) (context.Context, context.CancelFunc), g func(A) B) Operator[A, B] {
|
||||
return function.Flow2(
|
||||
Local[A](f),
|
||||
Map(g),
|
||||
)
|
||||
}
|
||||
|
||||
// Contramap changes the context during the execution of a ReaderIOResult.
|
||||
// This is the contravariant functor operation that transforms the input context.
|
||||
//
|
||||
// See: https://github.com/fantasyland/fantasy-land?tab=readme-ov-file#profunctor
|
||||
//
|
||||
// Contramap is an alias for Local and is useful for adapting a ReaderIOResult to work with
|
||||
// a modified context by providing a function that transforms the context.
|
||||
//
|
||||
// Type Parameters:
|
||||
// - A: The success type (unchanged)
|
||||
//
|
||||
// Parameters:
|
||||
// - f: Function to transform the context, returning a new context and CancelFunc
|
||||
//
|
||||
// Returns:
|
||||
// - An Operator that takes a ReaderIOResult[A] and returns a ReaderIOResult[A]
|
||||
//
|
||||
//go:inline
|
||||
func Contramap[A any](f func(context.Context) (context.Context, context.CancelFunc)) Operator[A, A] {
|
||||
return Local[A](f)
|
||||
}
|
||||
98
v2/context/readerioresult/profunctor_test.go
Normal file
98
v2/context/readerioresult/profunctor_test.go
Normal file
@@ -0,0 +1,98 @@
|
||||
// Copyright (c) 2025 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 readerioresult
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
R "github.com/IBM/fp-go/v2/result"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// TestPromapBasic tests basic Promap functionality
|
||||
func TestPromapBasic(t *testing.T) {
|
||||
t.Run("transform both context and output", func(t *testing.T) {
|
||||
getValue := func(ctx context.Context) IOResult[int] {
|
||||
return func() R.Result[int] {
|
||||
if v := ctx.Value("key"); v != nil {
|
||||
return R.Of(v.(int))
|
||||
}
|
||||
return R.Of(0)
|
||||
}
|
||||
}
|
||||
|
||||
addKey := func(ctx context.Context) (context.Context, context.CancelFunc) {
|
||||
newCtx := context.WithValue(ctx, "key", 42)
|
||||
return newCtx, func() {}
|
||||
}
|
||||
toString := strconv.Itoa
|
||||
|
||||
adapted := Promap(addKey, toString)(getValue)
|
||||
result := adapted(context.Background())()
|
||||
|
||||
assert.Equal(t, R.Of("42"), result)
|
||||
})
|
||||
}
|
||||
|
||||
// TestContramapBasic tests basic Contramap functionality
|
||||
func TestContramapBasic(t *testing.T) {
|
||||
t.Run("context transformation", func(t *testing.T) {
|
||||
getValue := func(ctx context.Context) IOResult[int] {
|
||||
return func() R.Result[int] {
|
||||
if v := ctx.Value("key"); v != nil {
|
||||
return R.Of(v.(int))
|
||||
}
|
||||
return R.Of(0)
|
||||
}
|
||||
}
|
||||
|
||||
addKey := func(ctx context.Context) (context.Context, context.CancelFunc) {
|
||||
newCtx := context.WithValue(ctx, "key", 100)
|
||||
return newCtx, func() {}
|
||||
}
|
||||
|
||||
adapted := Contramap[int](addKey)(getValue)
|
||||
result := adapted(context.Background())()
|
||||
|
||||
assert.Equal(t, R.Of(100), result)
|
||||
})
|
||||
}
|
||||
|
||||
// TestLocalBasic tests basic Local functionality
|
||||
func TestLocalBasic(t *testing.T) {
|
||||
t.Run("adds value to context", func(t *testing.T) {
|
||||
getValue := func(ctx context.Context) IOResult[string] {
|
||||
return func() R.Result[string] {
|
||||
if v := ctx.Value("user"); v != nil {
|
||||
return R.Of(v.(string))
|
||||
}
|
||||
return R.Of("unknown")
|
||||
}
|
||||
}
|
||||
|
||||
addUser := func(ctx context.Context) (context.Context, context.CancelFunc) {
|
||||
newCtx := context.WithValue(ctx, "user", "Alice")
|
||||
return newCtx, func() {}
|
||||
}
|
||||
|
||||
adapted := Local[string](addUser)(getValue)
|
||||
result := adapted(context.Background())()
|
||||
|
||||
assert.Equal(t, R.Of("Alice"), result)
|
||||
})
|
||||
}
|
||||
106
v2/context/readerresult/profunctor.go
Normal file
106
v2/context/readerresult/profunctor.go
Normal file
@@ -0,0 +1,106 @@
|
||||
// Copyright (c) 2025 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 readerresult
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/IBM/fp-go/v2/function"
|
||||
)
|
||||
|
||||
// Promap is the profunctor map operation that transforms both the input and output of a context-based ReaderResult.
|
||||
// It applies f to the input context (contravariantly) and g to the output value (covariantly).
|
||||
//
|
||||
// See: https://github.com/fantasyland/fantasy-land?tab=readme-ov-file#profunctor
|
||||
//
|
||||
// This operation allows you to:
|
||||
// - Modify the context before passing it to the ReaderResult (via f)
|
||||
// - Transform the success value after the computation completes (via g)
|
||||
//
|
||||
// The function f returns both a new context and a CancelFunc that should be called to release resources.
|
||||
// The error type is fixed as error and remains unchanged through the transformation.
|
||||
//
|
||||
// Type Parameters:
|
||||
// - A: The original success type produced by the ReaderResult
|
||||
// - B: The new output success type
|
||||
//
|
||||
// Parameters:
|
||||
// - f: Function to transform the input context (contravariant)
|
||||
// - g: Function to transform the output success value from A to B (covariant)
|
||||
//
|
||||
// Returns:
|
||||
// - An Operator that takes a ReaderResult[A] and returns a ReaderResult[B]
|
||||
//
|
||||
//go:inline
|
||||
func Promap[A, B any](f func(context.Context) (context.Context, context.CancelFunc), g func(A) B) Operator[A, B] {
|
||||
return function.Flow2(
|
||||
Local[A](f),
|
||||
Map(g),
|
||||
)
|
||||
}
|
||||
|
||||
// Contramap changes the context during the execution of a ReaderResult.
|
||||
// This is the contravariant functor operation that transforms the input context.
|
||||
//
|
||||
// See: https://github.com/fantasyland/fantasy-land?tab=readme-ov-file#profunctor
|
||||
//
|
||||
// Contramap is an alias for Local and is useful for adapting a ReaderResult to work with
|
||||
// a modified context by providing a function that transforms the context.
|
||||
//
|
||||
// Type Parameters:
|
||||
// - A: The success type (unchanged)
|
||||
//
|
||||
// Parameters:
|
||||
// - f: Function to transform the context, returning a new context and CancelFunc
|
||||
//
|
||||
// Returns:
|
||||
// - An Operator that takes a ReaderResult[A] and returns a ReaderResult[A]
|
||||
//
|
||||
//go:inline
|
||||
func Contramap[A any](f func(context.Context) (context.Context, context.CancelFunc)) Operator[A, A] {
|
||||
return Local[A](f)
|
||||
}
|
||||
|
||||
// Local changes the context during the execution of a ReaderResult.
|
||||
// This allows you to modify the context before passing it to a ReaderResult computation.
|
||||
//
|
||||
// See: https://github.com/fantasyland/fantasy-land?tab=readme-ov-file#profunctor
|
||||
//
|
||||
// Local is particularly useful for:
|
||||
// - Adding values to the context
|
||||
// - Setting timeouts or deadlines
|
||||
// - Modifying context metadata
|
||||
//
|
||||
// The function f returns both a new context and a CancelFunc. The CancelFunc is automatically
|
||||
// called (via defer) after the ReaderResult computation completes to ensure proper cleanup.
|
||||
//
|
||||
// Type Parameters:
|
||||
// - A: The result type (unchanged)
|
||||
//
|
||||
// Parameters:
|
||||
// - f: Function to transform the context, returning a new context and CancelFunc
|
||||
//
|
||||
// Returns:
|
||||
// - An Operator that takes a ReaderResult[A] and returns a ReaderResult[A]
|
||||
func Local[A any](f func(context.Context) (context.Context, context.CancelFunc)) Operator[A, A] {
|
||||
return func(rr ReaderResult[A]) ReaderResult[A] {
|
||||
return func(ctx context.Context) Result[A] {
|
||||
otherCtx, otherCancel := f(ctx)
|
||||
defer otherCancel()
|
||||
return rr(otherCtx)
|
||||
}
|
||||
}
|
||||
}
|
||||
92
v2/context/readerresult/profunctor_test.go
Normal file
92
v2/context/readerresult/profunctor_test.go
Normal file
@@ -0,0 +1,92 @@
|
||||
// Copyright (c) 2025 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 readerresult
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
R "github.com/IBM/fp-go/v2/result"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// TestPromapBasic tests basic Promap functionality
|
||||
func TestPromapBasic(t *testing.T) {
|
||||
t.Run("transform both context and output", func(t *testing.T) {
|
||||
getValue := func(ctx context.Context) Result[int] {
|
||||
if v := ctx.Value("key"); v != nil {
|
||||
return R.Of(v.(int))
|
||||
}
|
||||
return R.Of(0)
|
||||
}
|
||||
|
||||
addKey := func(ctx context.Context) (context.Context, context.CancelFunc) {
|
||||
newCtx := context.WithValue(ctx, "key", 42)
|
||||
return newCtx, func() {}
|
||||
}
|
||||
toString := strconv.Itoa
|
||||
|
||||
adapted := Promap(addKey, toString)(getValue)
|
||||
result := adapted(context.Background())
|
||||
|
||||
assert.Equal(t, R.Of("42"), result)
|
||||
})
|
||||
}
|
||||
|
||||
// TestContramapBasic tests basic Contramap functionality
|
||||
func TestContramapBasic(t *testing.T) {
|
||||
t.Run("context transformation", func(t *testing.T) {
|
||||
getValue := func(ctx context.Context) Result[int] {
|
||||
if v := ctx.Value("key"); v != nil {
|
||||
return R.Of(v.(int))
|
||||
}
|
||||
return R.Of(0)
|
||||
}
|
||||
|
||||
addKey := func(ctx context.Context) (context.Context, context.CancelFunc) {
|
||||
newCtx := context.WithValue(ctx, "key", 100)
|
||||
return newCtx, func() {}
|
||||
}
|
||||
|
||||
adapted := Contramap[int](addKey)(getValue)
|
||||
result := adapted(context.Background())
|
||||
|
||||
assert.Equal(t, R.Of(100), result)
|
||||
})
|
||||
}
|
||||
|
||||
// TestLocalBasic tests basic Local functionality
|
||||
func TestLocalBasic(t *testing.T) {
|
||||
t.Run("adds value to context", func(t *testing.T) {
|
||||
getValue := func(ctx context.Context) Result[string] {
|
||||
if v := ctx.Value("user"); v != nil {
|
||||
return R.Of(v.(string))
|
||||
}
|
||||
return R.Of("unknown")
|
||||
}
|
||||
|
||||
addUser := func(ctx context.Context) (context.Context, context.CancelFunc) {
|
||||
newCtx := context.WithValue(ctx, "user", "Alice")
|
||||
return newCtx, func() {}
|
||||
}
|
||||
|
||||
adapted := Local[string](addUser)(getValue)
|
||||
result := adapted(context.Background())
|
||||
|
||||
assert.Equal(t, R.Of("Alice"), result)
|
||||
})
|
||||
}
|
||||
@@ -56,3 +56,77 @@ func AltMonoid[E, A any](zero Lazy[Either[E, A]]) Monoid[E, A] {
|
||||
MonadAlt[E, A],
|
||||
)
|
||||
}
|
||||
|
||||
// takeFirst is a helper function that returns the first Right value, or the second if the first is Left.
|
||||
func takeFirst[E, A any](l, r Either[E, A]) Either[E, A] {
|
||||
if IsRight(l) {
|
||||
return l
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
// FirstMonoid creates a Monoid for Either[E, A] that returns the first Right value.
|
||||
// This monoid prefers the left operand when it is Right, otherwise returns the right operand.
|
||||
// The empty value is provided as a lazy computation.
|
||||
//
|
||||
// This is equivalent to AltMonoid but implemented more directly.
|
||||
//
|
||||
// Truth table:
|
||||
//
|
||||
// | x | y | concat(x, y) |
|
||||
// | --------- | --------- | ------------ |
|
||||
// | left(e1) | left(e2) | left(e2) |
|
||||
// | right(a) | left(e) | right(a) |
|
||||
// | left(e) | right(b) | right(b) |
|
||||
// | right(a) | right(b) | right(a) |
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// import "errors"
|
||||
// zero := func() either.Either[error, int] { return either.Left[int](errors.New("empty")) }
|
||||
// m := either.FirstMonoid[error, int](zero)
|
||||
// m.Concat(either.Right[error](2), either.Right[error](3)) // Right(2) - returns first Right
|
||||
// m.Concat(either.Left[int](errors.New("err")), either.Right[error](3)) // Right(3)
|
||||
// m.Concat(either.Right[error](2), either.Left[int](errors.New("err"))) // Right(2)
|
||||
// m.Empty() // Left(error("empty"))
|
||||
//
|
||||
//go:inline
|
||||
func FirstMonoid[E, A any](zero Lazy[Either[E, A]]) M.Monoid[Either[E, A]] {
|
||||
return M.MakeMonoid(takeFirst[E, A], zero())
|
||||
}
|
||||
|
||||
// takeLast is a helper function that returns the last Right value, or the first if the last is Left.
|
||||
func takeLast[E, A any](l, r Either[E, A]) Either[E, A] {
|
||||
if IsRight(r) {
|
||||
return r
|
||||
}
|
||||
return l
|
||||
}
|
||||
|
||||
// LastMonoid creates a Monoid for Either[E, A] that returns the last Right value.
|
||||
// This monoid prefers the right operand when it is Right, otherwise returns the left operand.
|
||||
// The empty value is provided as a lazy computation.
|
||||
//
|
||||
// Truth table:
|
||||
//
|
||||
// | x | y | concat(x, y) |
|
||||
// | --------- | --------- | ------------ |
|
||||
// | left(e1) | left(e2) | left(e1) |
|
||||
// | right(a) | left(e) | right(a) |
|
||||
// | left(e) | right(b) | right(b) |
|
||||
// | right(a) | right(b) | right(b) |
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// import "errors"
|
||||
// zero := func() either.Either[error, int] { return either.Left[int](errors.New("empty")) }
|
||||
// m := either.LastMonoid[error, int](zero)
|
||||
// m.Concat(either.Right[error](2), either.Right[error](3)) // Right(3) - returns last Right
|
||||
// m.Concat(either.Left[int](errors.New("err")), either.Right[error](3)) // Right(3)
|
||||
// m.Concat(either.Right[error](2), either.Left[int](errors.New("err"))) // Right(2)
|
||||
// m.Empty() // Left(error("empty"))
|
||||
//
|
||||
//go:inline
|
||||
func LastMonoid[E, A any](zero Lazy[Either[E, A]]) M.Monoid[Either[E, A]] {
|
||||
return M.MakeMonoid(takeLast[E, A], zero())
|
||||
}
|
||||
|
||||
402
v2/either/monoid_test.go
Normal file
402
v2/either/monoid_test.go
Normal file
@@ -0,0 +1,402 @@
|
||||
// Copyright (c) 2025 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 either
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// TestFirstMonoid tests the FirstMonoid implementation
|
||||
func TestFirstMonoid(t *testing.T) {
|
||||
zero := func() Either[error, int] { return Left[int](errors.New("empty")) }
|
||||
m := FirstMonoid(zero)
|
||||
|
||||
t.Run("both Right values - returns first", func(t *testing.T) {
|
||||
result := m.Concat(Right[error](2), Right[error](3))
|
||||
assert.Equal(t, Right[error](2), result)
|
||||
})
|
||||
|
||||
t.Run("left Right, right Left", func(t *testing.T) {
|
||||
result := m.Concat(Right[error](2), Left[int](errors.New("err")))
|
||||
assert.Equal(t, Right[error](2), result)
|
||||
})
|
||||
|
||||
t.Run("left Left, right Right", func(t *testing.T) {
|
||||
result := m.Concat(Left[int](errors.New("err")), Right[error](3))
|
||||
assert.Equal(t, Right[error](3), result)
|
||||
})
|
||||
|
||||
t.Run("both Left", func(t *testing.T) {
|
||||
err1 := errors.New("err1")
|
||||
err2 := errors.New("err2")
|
||||
result := m.Concat(Left[int](err1), Left[int](err2))
|
||||
// Should return the second Left
|
||||
assert.True(t, IsLeft(result))
|
||||
_, leftErr := Unwrap(result)
|
||||
assert.Equal(t, err2, leftErr)
|
||||
})
|
||||
|
||||
t.Run("empty value", func(t *testing.T) {
|
||||
empty := m.Empty()
|
||||
assert.True(t, IsLeft(empty))
|
||||
_, leftErr := Unwrap(empty)
|
||||
assert.Equal(t, "empty", leftErr.Error())
|
||||
})
|
||||
|
||||
t.Run("left identity", func(t *testing.T) {
|
||||
x := Right[error](5)
|
||||
result := m.Concat(m.Empty(), x)
|
||||
assert.Equal(t, x, result)
|
||||
})
|
||||
|
||||
t.Run("right identity", func(t *testing.T) {
|
||||
x := Right[error](5)
|
||||
result := m.Concat(x, m.Empty())
|
||||
assert.Equal(t, x, result)
|
||||
})
|
||||
|
||||
t.Run("associativity", func(t *testing.T) {
|
||||
a := Right[error](1)
|
||||
b := Right[error](2)
|
||||
c := Right[error](3)
|
||||
|
||||
left := m.Concat(m.Concat(a, b), c)
|
||||
right := m.Concat(a, m.Concat(b, c))
|
||||
|
||||
assert.Equal(t, left, right)
|
||||
assert.Equal(t, Right[error](1), left)
|
||||
})
|
||||
|
||||
t.Run("multiple concatenations", func(t *testing.T) {
|
||||
// Should return the first Right value encountered
|
||||
result := m.Concat(
|
||||
m.Concat(Left[int](errors.New("err1")), Right[error](1)),
|
||||
m.Concat(Right[error](2), Right[error](3)),
|
||||
)
|
||||
assert.Equal(t, Right[error](1), result)
|
||||
})
|
||||
|
||||
t.Run("with strings", func(t *testing.T) {
|
||||
zeroStr := func() Either[error, string] { return Left[string](errors.New("empty")) }
|
||||
strMonoid := FirstMonoid(zeroStr)
|
||||
|
||||
result := strMonoid.Concat(Right[error]("first"), Right[error]("second"))
|
||||
assert.Equal(t, Right[error]("first"), result)
|
||||
|
||||
result = strMonoid.Concat(Left[string](errors.New("err")), Right[error]("second"))
|
||||
assert.Equal(t, Right[error]("second"), result)
|
||||
})
|
||||
}
|
||||
|
||||
// TestLastMonoid tests the LastMonoid implementation
|
||||
func TestLastMonoid(t *testing.T) {
|
||||
zero := func() Either[error, int] { return Left[int](errors.New("empty")) }
|
||||
m := LastMonoid(zero)
|
||||
|
||||
t.Run("both Right values - returns last", func(t *testing.T) {
|
||||
result := m.Concat(Right[error](2), Right[error](3))
|
||||
assert.Equal(t, Right[error](3), result)
|
||||
})
|
||||
|
||||
t.Run("left Right, right Left", func(t *testing.T) {
|
||||
result := m.Concat(Right[error](2), Left[int](errors.New("err")))
|
||||
assert.Equal(t, Right[error](2), result)
|
||||
})
|
||||
|
||||
t.Run("left Left, right Right", func(t *testing.T) {
|
||||
result := m.Concat(Left[int](errors.New("err")), Right[error](3))
|
||||
assert.Equal(t, Right[error](3), result)
|
||||
})
|
||||
|
||||
t.Run("both Left", func(t *testing.T) {
|
||||
err1 := errors.New("err1")
|
||||
err2 := errors.New("err2")
|
||||
result := m.Concat(Left[int](err1), Left[int](err2))
|
||||
// Should return the first Left
|
||||
assert.True(t, IsLeft(result))
|
||||
_, leftErr := Unwrap(result)
|
||||
assert.Equal(t, err1, leftErr)
|
||||
})
|
||||
|
||||
t.Run("empty value", func(t *testing.T) {
|
||||
empty := m.Empty()
|
||||
assert.True(t, IsLeft(empty))
|
||||
_, leftErr := Unwrap(empty)
|
||||
assert.Equal(t, "empty", leftErr.Error())
|
||||
})
|
||||
|
||||
t.Run("left identity", func(t *testing.T) {
|
||||
x := Right[error](5)
|
||||
result := m.Concat(m.Empty(), x)
|
||||
assert.Equal(t, x, result)
|
||||
})
|
||||
|
||||
t.Run("right identity", func(t *testing.T) {
|
||||
x := Right[error](5)
|
||||
result := m.Concat(x, m.Empty())
|
||||
assert.Equal(t, x, result)
|
||||
})
|
||||
|
||||
t.Run("associativity", func(t *testing.T) {
|
||||
a := Right[error](1)
|
||||
b := Right[error](2)
|
||||
c := Right[error](3)
|
||||
|
||||
left := m.Concat(m.Concat(a, b), c)
|
||||
right := m.Concat(a, m.Concat(b, c))
|
||||
|
||||
assert.Equal(t, left, right)
|
||||
assert.Equal(t, Right[error](3), left)
|
||||
})
|
||||
|
||||
t.Run("multiple concatenations", func(t *testing.T) {
|
||||
// Should return the last Right value encountered
|
||||
result := m.Concat(
|
||||
m.Concat(Right[error](1), Right[error](2)),
|
||||
m.Concat(Right[error](3), Left[int](errors.New("err"))),
|
||||
)
|
||||
assert.Equal(t, Right[error](3), result)
|
||||
})
|
||||
|
||||
t.Run("with strings", func(t *testing.T) {
|
||||
zeroStr := func() Either[error, string] { return Left[string](errors.New("empty")) }
|
||||
strMonoid := LastMonoid(zeroStr)
|
||||
|
||||
result := strMonoid.Concat(Right[error]("first"), Right[error]("second"))
|
||||
assert.Equal(t, Right[error]("second"), result)
|
||||
|
||||
result = strMonoid.Concat(Right[error]("first"), Left[string](errors.New("err")))
|
||||
assert.Equal(t, Right[error]("first"), result)
|
||||
})
|
||||
}
|
||||
|
||||
// TestFirstMonoidVsAltMonoid verifies FirstMonoid and AltMonoid have the same behavior
|
||||
func TestFirstMonoidVsAltMonoid(t *testing.T) {
|
||||
zero := func() Either[error, int] { return Left[int](errors.New("empty")) }
|
||||
firstMonoid := FirstMonoid(zero)
|
||||
altMonoid := AltMonoid(zero)
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
left Either[error, int]
|
||||
right Either[error, int]
|
||||
}{
|
||||
{"both Right", Right[error](1), Right[error](2)},
|
||||
{"left Right, right Left", Right[error](1), Left[int](errors.New("err"))},
|
||||
{"left Left, right Right", Left[int](errors.New("err")), Right[error](2)},
|
||||
{"both Left", Left[int](errors.New("err1")), Left[int](errors.New("err2"))},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
firstResult := firstMonoid.Concat(tc.left, tc.right)
|
||||
altResult := altMonoid.Concat(tc.left, tc.right)
|
||||
|
||||
// Both should have the same Right/Left status
|
||||
assert.Equal(t, IsRight(firstResult), IsRight(altResult), "FirstMonoid and AltMonoid should have same Right/Left status")
|
||||
|
||||
if IsRight(firstResult) {
|
||||
rightVal1, _ := Unwrap(firstResult)
|
||||
rightVal2, _ := Unwrap(altResult)
|
||||
assert.Equal(t, rightVal1, rightVal2, "FirstMonoid and AltMonoid should have same Right value")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestFirstMonoidVsLastMonoid verifies the difference between FirstMonoid and LastMonoid
|
||||
func TestFirstMonoidVsLastMonoid(t *testing.T) {
|
||||
zero := func() Either[error, int] { return Left[int](errors.New("empty")) }
|
||||
firstMonoid := FirstMonoid(zero)
|
||||
lastMonoid := LastMonoid(zero)
|
||||
|
||||
t.Run("both Right - different results", func(t *testing.T) {
|
||||
firstResult := firstMonoid.Concat(Right[error](1), Right[error](2))
|
||||
lastResult := lastMonoid.Concat(Right[error](1), Right[error](2))
|
||||
|
||||
assert.Equal(t, Right[error](1), firstResult)
|
||||
assert.Equal(t, Right[error](2), lastResult)
|
||||
assert.NotEqual(t, firstResult, lastResult)
|
||||
})
|
||||
|
||||
t.Run("with Left values - different behavior", func(t *testing.T) {
|
||||
err1 := errors.New("err1")
|
||||
err2 := errors.New("err2")
|
||||
|
||||
// Both Left: FirstMonoid returns second, LastMonoid returns first
|
||||
firstResult := firstMonoid.Concat(Left[int](err1), Left[int](err2))
|
||||
lastResult := lastMonoid.Concat(Left[int](err1), Left[int](err2))
|
||||
|
||||
assert.True(t, IsLeft(firstResult))
|
||||
assert.True(t, IsLeft(lastResult))
|
||||
_, leftErr1 := Unwrap(firstResult)
|
||||
_, leftErr2 := Unwrap(lastResult)
|
||||
assert.Equal(t, err2, leftErr1)
|
||||
assert.Equal(t, err1, leftErr2)
|
||||
})
|
||||
|
||||
t.Run("mixed values - same results", func(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
left Either[error, int]
|
||||
right Either[error, int]
|
||||
expected Either[error, int]
|
||||
}{
|
||||
{"left Right, right Left", Right[error](1), Left[int](errors.New("err")), Right[error](1)},
|
||||
{"left Left, right Right", Left[int](errors.New("err")), Right[error](2), Right[error](2)},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
firstResult := firstMonoid.Concat(tc.left, tc.right)
|
||||
lastResult := lastMonoid.Concat(tc.left, tc.right)
|
||||
|
||||
assert.Equal(t, tc.expected, firstResult)
|
||||
assert.Equal(t, tc.expected, lastResult)
|
||||
assert.Equal(t, firstResult, lastResult)
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// TestMonoidLaws verifies monoid laws for FirstMonoid and LastMonoid
|
||||
func TestMonoidLaws(t *testing.T) {
|
||||
t.Run("FirstMonoid laws", func(t *testing.T) {
|
||||
zero := func() Either[error, int] { return Left[int](errors.New("empty")) }
|
||||
m := FirstMonoid(zero)
|
||||
|
||||
a := Right[error](1)
|
||||
b := Right[error](2)
|
||||
c := Right[error](3)
|
||||
|
||||
// Associativity: (a • b) • c = a • (b • c)
|
||||
left := m.Concat(m.Concat(a, b), c)
|
||||
right := m.Concat(a, m.Concat(b, c))
|
||||
assert.Equal(t, left, right)
|
||||
|
||||
// Left identity: Empty() • a = a
|
||||
leftId := m.Concat(m.Empty(), a)
|
||||
assert.Equal(t, a, leftId)
|
||||
|
||||
// Right identity: a • Empty() = a
|
||||
rightId := m.Concat(a, m.Empty())
|
||||
assert.Equal(t, a, rightId)
|
||||
})
|
||||
|
||||
t.Run("LastMonoid laws", func(t *testing.T) {
|
||||
zero := func() Either[error, int] { return Left[int](errors.New("empty")) }
|
||||
m := LastMonoid(zero)
|
||||
|
||||
a := Right[error](1)
|
||||
b := Right[error](2)
|
||||
c := Right[error](3)
|
||||
|
||||
// Associativity: (a • b) • c = a • (b • c)
|
||||
left := m.Concat(m.Concat(a, b), c)
|
||||
right := m.Concat(a, m.Concat(b, c))
|
||||
assert.Equal(t, left, right)
|
||||
|
||||
// Left identity: Empty() • a = a
|
||||
leftId := m.Concat(m.Empty(), a)
|
||||
assert.Equal(t, a, leftId)
|
||||
|
||||
// Right identity: a • Empty() = a
|
||||
rightId := m.Concat(a, m.Empty())
|
||||
assert.Equal(t, a, rightId)
|
||||
})
|
||||
|
||||
t.Run("FirstMonoid laws with Left values", func(t *testing.T) {
|
||||
zero := func() Either[error, int] { return Left[int](errors.New("empty")) }
|
||||
m := FirstMonoid(zero)
|
||||
|
||||
a := Left[int](errors.New("err1"))
|
||||
b := Left[int](errors.New("err2"))
|
||||
c := Left[int](errors.New("err3"))
|
||||
|
||||
// Associativity with Left values
|
||||
left := m.Concat(m.Concat(a, b), c)
|
||||
right := m.Concat(a, m.Concat(b, c))
|
||||
assert.Equal(t, left, right)
|
||||
})
|
||||
|
||||
t.Run("LastMonoid laws with Left values", func(t *testing.T) {
|
||||
zero := func() Either[error, int] { return Left[int](errors.New("empty")) }
|
||||
m := LastMonoid(zero)
|
||||
|
||||
a := Left[int](errors.New("err1"))
|
||||
b := Left[int](errors.New("err2"))
|
||||
c := Left[int](errors.New("err3"))
|
||||
|
||||
// Associativity with Left values
|
||||
left := m.Concat(m.Concat(a, b), c)
|
||||
right := m.Concat(a, m.Concat(b, c))
|
||||
assert.Equal(t, left, right)
|
||||
})
|
||||
}
|
||||
|
||||
// TestMonoidEdgeCases tests edge cases for monoid operations
|
||||
func TestMonoidEdgeCases(t *testing.T) {
|
||||
t.Run("FirstMonoid with empty concatenations", func(t *testing.T) {
|
||||
zero := func() Either[error, int] { return Left[int](errors.New("empty")) }
|
||||
m := FirstMonoid(zero)
|
||||
|
||||
// Empty with empty
|
||||
result := m.Concat(m.Empty(), m.Empty())
|
||||
assert.True(t, IsLeft(result))
|
||||
})
|
||||
|
||||
t.Run("LastMonoid with empty concatenations", func(t *testing.T) {
|
||||
zero := func() Either[error, int] { return Left[int](errors.New("empty")) }
|
||||
m := LastMonoid(zero)
|
||||
|
||||
// Empty with empty
|
||||
result := m.Concat(m.Empty(), m.Empty())
|
||||
assert.True(t, IsLeft(result))
|
||||
})
|
||||
|
||||
t.Run("FirstMonoid chain of operations", func(t *testing.T) {
|
||||
zero := func() Either[error, int] { return Left[int](errors.New("empty")) }
|
||||
m := FirstMonoid(zero)
|
||||
|
||||
// Chain multiple operations
|
||||
result := m.Concat(
|
||||
m.Concat(
|
||||
m.Concat(Left[int](errors.New("err1")), Left[int](errors.New("err2"))),
|
||||
Right[error](1),
|
||||
),
|
||||
m.Concat(Right[error](2), Right[error](3)),
|
||||
)
|
||||
assert.Equal(t, Right[error](1), result)
|
||||
})
|
||||
|
||||
t.Run("LastMonoid chain of operations", func(t *testing.T) {
|
||||
zero := func() Either[error, int] { return Left[int](errors.New("empty")) }
|
||||
m := LastMonoid(zero)
|
||||
|
||||
// Chain multiple operations
|
||||
result := m.Concat(
|
||||
m.Concat(Right[error](1), Right[error](2)),
|
||||
m.Concat(
|
||||
Right[error](3),
|
||||
m.Concat(Right[error](4), Left[int](errors.New("err"))),
|
||||
),
|
||||
)
|
||||
assert.Equal(t, Right[error](4), result)
|
||||
})
|
||||
}
|
||||
@@ -20,6 +20,8 @@ package eq
|
||||
// by mapping the input type. It's particularly useful for comparing complex types by
|
||||
// extracting comparable fields.
|
||||
//
|
||||
// See: https://github.com/fantasyland/fantasy-land?tab=readme-ov-file#profunctor
|
||||
//
|
||||
// The name "contramap" comes from category theory, where it represents a contravariant
|
||||
// functor. Unlike regular map (covariant), which transforms the output, contramap
|
||||
// transforms the input in the opposite direction.
|
||||
|
||||
@@ -21,54 +21,261 @@ import (
|
||||
"github.com/IBM/fp-go/v2/internal/functor"
|
||||
)
|
||||
|
||||
// MonadAp applies a function to a value in the Identity monad context.
|
||||
// Since Identity has no computational context, this is just function application.
|
||||
//
|
||||
// This is the uncurried version of Ap.
|
||||
//
|
||||
// Implements the Fantasy Land Apply specification:
|
||||
// https://github.com/fantasyland/fantasy-land?tab=readme-ov-file#apply
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// result := identity.MonadAp(func(n int) int { return n * 2 }, 21)
|
||||
// // result is 42
|
||||
func MonadAp[B, A any](fab func(A) B, fa A) B {
|
||||
return fab(fa)
|
||||
}
|
||||
|
||||
// Ap applies a wrapped function to a wrapped value.
|
||||
// Returns a function that takes a function and applies the value to it.
|
||||
//
|
||||
// This is the curried version of MonadAp, useful for composition with Pipe.
|
||||
//
|
||||
// Implements the Fantasy Land Apply specification:
|
||||
// https://github.com/fantasyland/fantasy-land?tab=readme-ov-file#apply
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// import F "github.com/IBM/fp-go/v2/function"
|
||||
//
|
||||
// double := func(n int) int { return n * 2 }
|
||||
// result := F.Pipe1(double, identity.Ap[int](21))
|
||||
// // result is 42
|
||||
func Ap[B, A any](fa A) Operator[func(A) B, B] {
|
||||
return function.Bind2nd(MonadAp[B, A], fa)
|
||||
}
|
||||
|
||||
// MonadMap transforms a value using a function in the Identity monad context.
|
||||
// Since Identity has no computational context, this is just function application.
|
||||
//
|
||||
// This is the uncurried version of Map.
|
||||
//
|
||||
// Implements the Fantasy Land Functor specification:
|
||||
// https://github.com/fantasyland/fantasy-land?tab=readme-ov-file#functor
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// result := identity.MonadMap(21, func(n int) int { return n * 2 })
|
||||
// // result is 42
|
||||
func MonadMap[A, B any](fa A, f func(A) B) B {
|
||||
return f(fa)
|
||||
}
|
||||
|
||||
// Map transforms a value using a function.
|
||||
// Returns the function itself since Identity adds no context.
|
||||
//
|
||||
// This is the curried version of MonadMap, useful for composition with Pipe.
|
||||
//
|
||||
// Implements the Fantasy Land Functor specification:
|
||||
// https://github.com/fantasyland/fantasy-land?tab=readme-ov-file#functor
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// import F "github.com/IBM/fp-go/v2/function"
|
||||
//
|
||||
// result := F.Pipe1(21, identity.Map(func(n int) int { return n * 2 }))
|
||||
// // result is 42
|
||||
func Map[A, B any](f func(A) B) Operator[A, B] {
|
||||
return f
|
||||
}
|
||||
|
||||
// MonadMapTo replaces a value with a constant, ignoring the input.
|
||||
//
|
||||
// This is the uncurried version of MapTo.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// result := identity.MonadMapTo("ignored", 42)
|
||||
// // result is 42
|
||||
func MonadMapTo[A, B any](_ A, b B) B {
|
||||
return b
|
||||
}
|
||||
|
||||
// MapTo replaces any value with a constant value.
|
||||
// Returns a function that ignores its input and returns the constant.
|
||||
//
|
||||
// This is the curried version of MonadMapTo, useful for composition with Pipe.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// import F "github.com/IBM/fp-go/v2/function"
|
||||
//
|
||||
// result := F.Pipe1("ignored", identity.MapTo[string](42))
|
||||
// // result is 42
|
||||
func MapTo[A, B any](b B) func(A) B {
|
||||
return function.Constant1[A](b)
|
||||
}
|
||||
|
||||
// Of wraps a value in the Identity monad.
|
||||
// Since Identity has no computational context, this is just the identity function.
|
||||
//
|
||||
// This is the Pointed/Applicative "pure" operation.
|
||||
//
|
||||
// Implements the Fantasy Land Applicative specification:
|
||||
// https://github.com/fantasyland/fantasy-land?tab=readme-ov-file#applicative
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// value := identity.Of(42)
|
||||
// // value is 42
|
||||
//
|
||||
//go:inline
|
||||
func Of[A any](a A) A {
|
||||
return a
|
||||
}
|
||||
|
||||
// MonadChain applies a Kleisli arrow to a value in the Identity monad context.
|
||||
// Since Identity has no computational context, this is just function application.
|
||||
//
|
||||
// This is the uncurried version of Chain, also known as "bind" or "flatMap".
|
||||
//
|
||||
// Implements the Fantasy Land Chain specification:
|
||||
// https://github.com/fantasyland/fantasy-land?tab=readme-ov-file#chain
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// result := identity.MonadChain(21, func(n int) int { return n * 2 })
|
||||
// // result is 42
|
||||
func MonadChain[A, B any](ma A, f Kleisli[A, B]) B {
|
||||
return f(ma)
|
||||
}
|
||||
|
||||
// Chain applies a Kleisli arrow to a value.
|
||||
// Returns the function itself since Identity adds no context.
|
||||
//
|
||||
// This is the curried version of MonadChain, also known as "bind" or "flatMap".
|
||||
// Useful for composition with Pipe.
|
||||
//
|
||||
// Implements the Fantasy Land Chain specification:
|
||||
// https://github.com/fantasyland/fantasy-land?tab=readme-ov-file#chain
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// import F "github.com/IBM/fp-go/v2/function"
|
||||
//
|
||||
// result := F.Pipe1(21, identity.Chain(func(n int) int { return n * 2 }))
|
||||
// // result is 42
|
||||
//
|
||||
//go:inline
|
||||
func Chain[A, B any](f Kleisli[A, B]) Operator[A, B] {
|
||||
return f
|
||||
}
|
||||
|
||||
// MonadChainFirst executes a computation for its effect but returns the original value.
|
||||
// Useful for side effects like logging while preserving the original value.
|
||||
//
|
||||
// This is the uncurried version of ChainFirst.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// result := identity.MonadChainFirst(42, func(n int) string {
|
||||
// fmt.Printf("Value: %d\n", n)
|
||||
// return "logged"
|
||||
// })
|
||||
// // result is 42 (original value preserved)
|
||||
func MonadChainFirst[A, B any](fa A, f Kleisli[A, B]) A {
|
||||
return chain.MonadChainFirst(MonadChain[A, A], MonadMap[B, A], fa, f)
|
||||
}
|
||||
|
||||
// ChainFirst executes a computation for its effect but returns the original value.
|
||||
// Useful for side effects like logging while preserving the original value.
|
||||
//
|
||||
// This is the curried version of MonadChainFirst, useful for composition with Pipe.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// import F "github.com/IBM/fp-go/v2/function"
|
||||
//
|
||||
// result := F.Pipe1(
|
||||
// 42,
|
||||
// identity.ChainFirst(func(n int) string {
|
||||
// fmt.Printf("Value: %d\n", n)
|
||||
// return "logged"
|
||||
// }),
|
||||
// )
|
||||
// // result is 42 (original value preserved)
|
||||
func ChainFirst[A, B any](f Kleisli[A, B]) Operator[A, A] {
|
||||
return chain.ChainFirst(Chain[A, A], Map[B, A], f)
|
||||
}
|
||||
|
||||
// MonadFlap applies a value to a function, flipping the normal application order.
|
||||
// Instead of applying a function to a value, it applies a value to a function.
|
||||
//
|
||||
// This is the uncurried version of Flap.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// double := func(n int) int { return n * 2 }
|
||||
// result := identity.MonadFlap(double, 21)
|
||||
// // result is 42
|
||||
func MonadFlap[B, A any](fab func(A) B, a A) B {
|
||||
return functor.MonadFlap(MonadMap[func(A) B, B], fab, a)
|
||||
}
|
||||
|
||||
// Flap applies a value to a function, flipping the normal application order.
|
||||
// Returns a function that takes a function and applies the value to it.
|
||||
//
|
||||
// This is the curried version of MonadFlap, useful for composition with Pipe.
|
||||
// Useful when you have a value and want to apply it to multiple functions.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// import F "github.com/IBM/fp-go/v2/function"
|
||||
//
|
||||
// double := func(n int) int { return n * 2 }
|
||||
// result := F.Pipe1(double, identity.Flap[int](21))
|
||||
// // result is 42
|
||||
//
|
||||
//go:inline
|
||||
func Flap[B, A any](a A) Operator[func(A) B, B] {
|
||||
return functor.Flap(Map[func(A) B, B], a)
|
||||
}
|
||||
|
||||
// Extract extracts the value from the Identity monad.
|
||||
// Since Identity has no computational context, this is just the identity function.
|
||||
//
|
||||
// This is the Comonad "extract" operation.
|
||||
//
|
||||
// Implements the Fantasy Land Comonad specification:
|
||||
// https://github.com/fantasyland/fantasy-land?tab=readme-ov-file#comonad
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// value := identity.Extract(42)
|
||||
// // value is 42
|
||||
//
|
||||
//go:inline
|
||||
func Extract[A any](a A) A {
|
||||
return a
|
||||
}
|
||||
|
||||
// Extend extends a computation over the Identity monad.
|
||||
// Since Identity has no computational context, this is just function application.
|
||||
//
|
||||
// This is the Comonad "extend" operation, also known as "cobind".
|
||||
//
|
||||
// Implements the Fantasy Land Extend specification:
|
||||
// https://github.com/fantasyland/fantasy-land?tab=readme-ov-file#extend
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// import F "github.com/IBM/fp-go/v2/function"
|
||||
//
|
||||
// result := F.Pipe1(21, identity.Extend(func(n int) int { return n * 2 }))
|
||||
// // result is 42
|
||||
//
|
||||
//go:inline
|
||||
func Extend[A, B any](f func(A) B) Operator[A, B] {
|
||||
return f
|
||||
}
|
||||
|
||||
@@ -723,3 +723,99 @@ func TestTraverseTuple10(t *testing.T) {
|
||||
assert.Equal(t, T.MakeTuple10(2, 4, 6, 8, 10, 12, 14, 16, 18, 20), result)
|
||||
})
|
||||
}
|
||||
|
||||
func TestExtract(t *testing.T) {
|
||||
t.Run("extracts int value", func(t *testing.T) {
|
||||
result := Extract(42)
|
||||
assert.Equal(t, 42, result)
|
||||
})
|
||||
|
||||
t.Run("extracts string value", func(t *testing.T) {
|
||||
result := Extract("hello")
|
||||
assert.Equal(t, "hello", result)
|
||||
})
|
||||
|
||||
t.Run("extracts struct value", func(t *testing.T) {
|
||||
type Person struct{ Name string }
|
||||
p := Person{Name: "Alice"}
|
||||
result := Extract(p)
|
||||
assert.Equal(t, p, result)
|
||||
})
|
||||
|
||||
t.Run("extracts pointer value", func(t *testing.T) {
|
||||
value := 100
|
||||
ptr := &value
|
||||
result := Extract(ptr)
|
||||
assert.Equal(t, ptr, result)
|
||||
assert.Equal(t, 100, *result)
|
||||
})
|
||||
}
|
||||
|
||||
func TestExtend(t *testing.T) {
|
||||
t.Run("extends with transformation", func(t *testing.T) {
|
||||
result := F.Pipe1(21, Extend(utils.Double))
|
||||
assert.Equal(t, 42, result)
|
||||
})
|
||||
|
||||
t.Run("extends with type change", func(t *testing.T) {
|
||||
result := F.Pipe1(42, Extend(S.Format[int]("Number: %d")))
|
||||
assert.Equal(t, "Number: 42", result)
|
||||
})
|
||||
|
||||
t.Run("chains multiple extends", func(t *testing.T) {
|
||||
result := F.Pipe2(
|
||||
5,
|
||||
Extend(N.Mul(2)),
|
||||
Extend(N.Add(10)),
|
||||
)
|
||||
assert.Equal(t, 20, result)
|
||||
})
|
||||
|
||||
t.Run("extends with complex computation", func(t *testing.T) {
|
||||
result := F.Pipe1(
|
||||
10,
|
||||
Extend(func(n int) string {
|
||||
doubled := n * 2
|
||||
return fmt.Sprintf("Result: %d", doubled)
|
||||
}),
|
||||
)
|
||||
assert.Equal(t, "Result: 20", result)
|
||||
})
|
||||
}
|
||||
|
||||
// Test Comonad laws
|
||||
func TestComonadLaws(t *testing.T) {
|
||||
t.Run("left identity", func(t *testing.T) {
|
||||
// Extract(Extend(f)(w)) === f(w)
|
||||
w := 42
|
||||
f := N.Mul(2)
|
||||
|
||||
left := Extract(F.Pipe1(w, Extend(f)))
|
||||
right := f(w)
|
||||
|
||||
assert.Equal(t, right, left)
|
||||
})
|
||||
|
||||
t.Run("right identity", func(t *testing.T) {
|
||||
// Extend(Extract)(w) === w
|
||||
w := 42
|
||||
|
||||
result := F.Pipe1(w, Extend(Extract[int]))
|
||||
|
||||
assert.Equal(t, w, result)
|
||||
})
|
||||
|
||||
t.Run("associativity", func(t *testing.T) {
|
||||
// Extend(f)(Extend(g)(w)) === Extend(x => f(Extend(g)(x)))(w)
|
||||
w := 5
|
||||
f := N.Mul(2)
|
||||
g := N.Add(10)
|
||||
|
||||
left := F.Pipe2(w, Extend(g), Extend(f))
|
||||
right := F.Pipe1(w, Extend(func(x int) int {
|
||||
return f(F.Pipe1(x, Extend(g)))
|
||||
}))
|
||||
|
||||
assert.Equal(t, right, left)
|
||||
})
|
||||
}
|
||||
|
||||
59
v2/idiomatic/context/readerresult/profunctor.go
Normal file
59
v2/idiomatic/context/readerresult/profunctor.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package readerresult
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/IBM/fp-go/v2/function"
|
||||
)
|
||||
|
||||
// Promap is the profunctor map operation that transforms both the input and output of a ReaderResult.
|
||||
// It applies f to the input context (contravariantly) and g to the output value (covariantly).
|
||||
//
|
||||
// See: https://github.com/fantasyland/fantasy-land?tab=readme-ov-file#profunctor
|
||||
//
|
||||
// This operation allows you to:
|
||||
// - Adapt the context before passing it to the ReaderResult (via f)
|
||||
// - Transform the success value after the computation completes (via g)
|
||||
//
|
||||
// The error type is fixed as error and remains unchanged through the transformation.
|
||||
//
|
||||
// Type Parameters:
|
||||
// - A: The original success type produced by the ReaderResult
|
||||
// - B: The new output success type
|
||||
//
|
||||
// Parameters:
|
||||
// - f: Function to transform the input context, returning a new context and cancel function (contravariant)
|
||||
// - g: Function to transform the output success value from A to B (covariant)
|
||||
//
|
||||
// Returns:
|
||||
// - An Operator that takes a ReaderResult[A] and returns a ReaderResult[B]
|
||||
//
|
||||
//go:inline
|
||||
func Promap[A, B any](f func(context.Context) (context.Context, context.CancelFunc), g func(A) B) Operator[A, B] {
|
||||
return function.Flow2(
|
||||
Local[A](f),
|
||||
Map(g),
|
||||
)
|
||||
}
|
||||
|
||||
// Contramap changes the value of the local context during the execution of a ReaderResult.
|
||||
// This is the contravariant functor operation that transforms the input context.
|
||||
//
|
||||
// See: https://github.com/fantasyland/fantasy-land?tab=readme-ov-file#profunctor
|
||||
//
|
||||
// Contramap is useful for adapting a ReaderResult to work with a modified context
|
||||
// by providing a function that creates a new context (and optional cancel function) from the current one.
|
||||
//
|
||||
// Type Parameters:
|
||||
// - A: The success type (unchanged)
|
||||
//
|
||||
// Parameters:
|
||||
// - f: Function to transform the context, returning a new context and cancel function
|
||||
//
|
||||
// Returns:
|
||||
// - A Kleisli arrow that takes a ReaderResult[A] and returns a ReaderResult[A]
|
||||
//
|
||||
//go:inline
|
||||
func Contramap[A any](f func(context.Context) (context.Context, context.CancelFunc)) Kleisli[ReaderResult[A], A] {
|
||||
return Local[A](f)
|
||||
}
|
||||
187
v2/idiomatic/context/readerresult/profunctor_test.go
Normal file
187
v2/idiomatic/context/readerresult/profunctor_test.go
Normal file
@@ -0,0 +1,187 @@
|
||||
// Copyright (c) 2025 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 readerresult
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
N "github.com/IBM/fp-go/v2/number"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// TestPromapBasic tests basic Promap functionality
|
||||
func TestPromapBasic(t *testing.T) {
|
||||
t.Run("transform both context and output", func(t *testing.T) {
|
||||
// ReaderResult that reads a value from context
|
||||
getValue := func(ctx context.Context) (int, error) {
|
||||
if val := ctx.Value("port"); val != nil {
|
||||
return val.(int), nil
|
||||
}
|
||||
return 0, fmt.Errorf("port not found")
|
||||
}
|
||||
|
||||
// Transform context to add a value and int to string
|
||||
addPort := func(ctx context.Context) (context.Context, context.CancelFunc) {
|
||||
return context.WithValue(ctx, "port", 8080), func() {}
|
||||
}
|
||||
toString := strconv.Itoa
|
||||
|
||||
adapted := Promap(addPort, toString)(getValue)
|
||||
result, err := adapted(context.Background())
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "8080", result)
|
||||
})
|
||||
|
||||
t.Run("handles error case", func(t *testing.T) {
|
||||
// ReaderResult that returns an error
|
||||
getError := func(ctx context.Context) (int, error) {
|
||||
return 0, fmt.Errorf("error occurred")
|
||||
}
|
||||
|
||||
addPort := func(ctx context.Context) (context.Context, context.CancelFunc) {
|
||||
return context.WithValue(ctx, "port", 8080), func() {}
|
||||
}
|
||||
toString := strconv.Itoa
|
||||
|
||||
adapted := Promap(addPort, toString)(getError)
|
||||
_, err := adapted(context.Background())
|
||||
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, "error occurred", err.Error())
|
||||
})
|
||||
|
||||
t.Run("context transformation with cancellation", func(t *testing.T) {
|
||||
getValue := func(ctx context.Context) (string, error) {
|
||||
if val := ctx.Value("key"); val != nil {
|
||||
return val.(string), nil
|
||||
}
|
||||
return "", fmt.Errorf("key not found")
|
||||
}
|
||||
|
||||
addValue := func(ctx context.Context) (context.Context, context.CancelFunc) {
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
return context.WithValue(ctx, "key", "value"), cancel
|
||||
}
|
||||
toUpper := func(s string) string {
|
||||
return "UPPER_" + s
|
||||
}
|
||||
|
||||
adapted := Promap(addValue, toUpper)(getValue)
|
||||
result, err := adapted(context.Background())
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "UPPER_value", result)
|
||||
})
|
||||
}
|
||||
|
||||
// TestContramapBasic tests basic Contramap functionality
|
||||
func TestContramapBasic(t *testing.T) {
|
||||
t.Run("context adaptation", func(t *testing.T) {
|
||||
// ReaderResult that reads from context
|
||||
getPort := func(ctx context.Context) (int, error) {
|
||||
if val := ctx.Value("port"); val != nil {
|
||||
return val.(int), nil
|
||||
}
|
||||
return 0, fmt.Errorf("port not found")
|
||||
}
|
||||
|
||||
// Adapt context to add port value
|
||||
addPort := func(ctx context.Context) (context.Context, context.CancelFunc) {
|
||||
return context.WithValue(ctx, "port", 9000), func() {}
|
||||
}
|
||||
|
||||
adapted := Contramap[int](addPort)(getPort)
|
||||
result, err := adapted(context.Background())
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 9000, result)
|
||||
})
|
||||
|
||||
t.Run("preserves error", func(t *testing.T) {
|
||||
getError := func(ctx context.Context) (int, error) {
|
||||
return 0, fmt.Errorf("config error")
|
||||
}
|
||||
|
||||
addPort := func(ctx context.Context) (context.Context, context.CancelFunc) {
|
||||
return context.WithValue(ctx, "port", 9000), func() {}
|
||||
}
|
||||
|
||||
adapted := Contramap[int](addPort)(getError)
|
||||
_, err := adapted(context.Background())
|
||||
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, "config error", err.Error())
|
||||
})
|
||||
|
||||
t.Run("multiple context values", func(t *testing.T) {
|
||||
getValues := func(ctx context.Context) (string, error) {
|
||||
host := ctx.Value("host")
|
||||
port := ctx.Value("port")
|
||||
if host != nil && port != nil {
|
||||
return fmt.Sprintf("%s:%d", host, port), nil
|
||||
}
|
||||
return "", fmt.Errorf("missing values")
|
||||
}
|
||||
|
||||
addValues := func(ctx context.Context) (context.Context, context.CancelFunc) {
|
||||
ctx = context.WithValue(ctx, "host", "localhost")
|
||||
ctx = context.WithValue(ctx, "port", 8080)
|
||||
return ctx, func() {}
|
||||
}
|
||||
|
||||
adapted := Contramap[string](addValues)(getValues)
|
||||
result, err := adapted(context.Background())
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "localhost:8080", result)
|
||||
})
|
||||
}
|
||||
|
||||
// TestPromapComposition tests that Promap can be composed
|
||||
func TestPromapComposition(t *testing.T) {
|
||||
t.Run("compose two Promap transformations", func(t *testing.T) {
|
||||
reader := func(ctx context.Context) (int, error) {
|
||||
if val := ctx.Value("value"); val != nil {
|
||||
return val.(int), nil
|
||||
}
|
||||
return 0, fmt.Errorf("value not found")
|
||||
}
|
||||
|
||||
f1 := func(ctx context.Context) (context.Context, context.CancelFunc) {
|
||||
return context.WithValue(ctx, "value", 5), func() {}
|
||||
}
|
||||
g1 := N.Mul(2)
|
||||
|
||||
f2 := func(ctx context.Context) (context.Context, context.CancelFunc) {
|
||||
return ctx, func() {}
|
||||
}
|
||||
g2 := N.Add(10)
|
||||
|
||||
// Apply two Promap transformations
|
||||
step1 := Promap(f1, g1)(reader)
|
||||
step2 := Promap(f2, g2)(step1)
|
||||
|
||||
result, err := step2(context.Background())
|
||||
|
||||
// (5 * 2) + 10 = 20
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 20, result)
|
||||
})
|
||||
}
|
||||
74
v2/idiomatic/readerioresult/profunctor.go
Normal file
74
v2/idiomatic/readerioresult/profunctor.go
Normal file
@@ -0,0 +1,74 @@
|
||||
// Copyright (c) 2025 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 readerioresult
|
||||
|
||||
import (
|
||||
"github.com/IBM/fp-go/v2/idiomatic/ioresult"
|
||||
"github.com/IBM/fp-go/v2/reader"
|
||||
)
|
||||
|
||||
// Promap is the profunctor map operation that transforms both the input and output of a ReaderIOResult.
|
||||
// It applies f to the input environment (contravariantly) and g to the output value (covariantly).
|
||||
//
|
||||
// See: https://github.com/fantasyland/fantasy-land?tab=readme-ov-file#profunctor
|
||||
//
|
||||
// This operation allows you to:
|
||||
// - Adapt the environment type before passing it to the ReaderIOResult (via f)
|
||||
// - Transform the success value after the IO effect completes (via g)
|
||||
//
|
||||
// The error type is fixed as error and remains unchanged through the transformation.
|
||||
//
|
||||
// Type Parameters:
|
||||
// - E: The original environment type expected by the ReaderIOResult
|
||||
// - A: The original success type produced by the ReaderIOResult
|
||||
// - D: The new input environment type
|
||||
// - B: The new output success type
|
||||
//
|
||||
// Parameters:
|
||||
// - f: Function to transform the input environment from D to E (contravariant)
|
||||
// - g: Function to transform the output success value from A to B (covariant)
|
||||
//
|
||||
// Returns:
|
||||
// - A Kleisli arrow that takes a ReaderIOResult[E, A] and returns a ReaderIOResult[D, B]
|
||||
//
|
||||
//go:inline
|
||||
func Promap[E, A, D, B any](f func(D) E, g func(A) B) Kleisli[D, ReaderIOResult[E, A], B] {
|
||||
return reader.Promap(f, ioresult.Map(g))
|
||||
}
|
||||
|
||||
// Contramap changes the value of the local environment during the execution of a ReaderIOResult.
|
||||
// This is the contravariant functor operation that transforms the input environment.
|
||||
//
|
||||
// See: https://github.com/fantasyland/fantasy-land?tab=readme-ov-file#profunctor
|
||||
//
|
||||
// Contramap is useful for adapting a ReaderIOResult to work with a different environment type
|
||||
// by providing a function that converts the new environment to the expected one.
|
||||
//
|
||||
// Type Parameters:
|
||||
// - A: The success type (unchanged)
|
||||
// - R2: The new input environment type
|
||||
// - R1: The original environment type expected by the ReaderIOResult
|
||||
//
|
||||
// Parameters:
|
||||
// - f: Function to transform the environment from R2 to R1
|
||||
//
|
||||
// Returns:
|
||||
// - A Kleisli arrow that takes a ReaderIOResult[R1, A] and returns a ReaderIOResult[R2, A]
|
||||
//
|
||||
//go:inline
|
||||
func Contramap[A, R1, R2 any](f func(R2) R1) Kleisli[R2, ReaderIOResult[R1, A], A] {
|
||||
return Local[A](f)
|
||||
}
|
||||
199
v2/idiomatic/readerioresult/profunctor_test.go
Normal file
199
v2/idiomatic/readerioresult/profunctor_test.go
Normal file
@@ -0,0 +1,199 @@
|
||||
// Copyright (c) 2025 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 readerioresult
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
N "github.com/IBM/fp-go/v2/number"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type SimpleConfig struct {
|
||||
Port int
|
||||
}
|
||||
|
||||
type DetailedConfig struct {
|
||||
Host string
|
||||
Port int
|
||||
}
|
||||
|
||||
// TestPromapBasic tests basic Promap functionality
|
||||
func TestPromapBasic(t *testing.T) {
|
||||
t.Run("transform both input and output", func(t *testing.T) {
|
||||
// ReaderIOResult that reads port from SimpleConfig
|
||||
getPort := func(c SimpleConfig) func() (int, error) {
|
||||
return func() (int, error) {
|
||||
return c.Port, nil
|
||||
}
|
||||
}
|
||||
|
||||
// Transform DetailedConfig to SimpleConfig and int to string
|
||||
simplify := func(d DetailedConfig) SimpleConfig {
|
||||
return SimpleConfig{Port: d.Port}
|
||||
}
|
||||
toString := strconv.Itoa
|
||||
|
||||
adapted := Promap(simplify, toString)(getPort)
|
||||
result, err := adapted(DetailedConfig{Host: "localhost", Port: 8080})()
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "8080", result)
|
||||
})
|
||||
|
||||
t.Run("handles error case", func(t *testing.T) {
|
||||
// ReaderIOResult that returns an error
|
||||
getError := func(c SimpleConfig) func() (int, error) {
|
||||
return func() (int, error) {
|
||||
return 0, fmt.Errorf("error occurred")
|
||||
}
|
||||
}
|
||||
|
||||
simplify := func(d DetailedConfig) SimpleConfig {
|
||||
return SimpleConfig{Port: d.Port}
|
||||
}
|
||||
toString := strconv.Itoa
|
||||
|
||||
adapted := Promap(simplify, toString)(getError)
|
||||
_, err := adapted(DetailedConfig{Host: "localhost", Port: 8080})()
|
||||
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, "error occurred", err.Error())
|
||||
})
|
||||
}
|
||||
|
||||
// TestContramapBasic tests basic Contramap functionality
|
||||
func TestContramapBasic(t *testing.T) {
|
||||
t.Run("environment adaptation", func(t *testing.T) {
|
||||
// ReaderIOResult that reads from SimpleConfig
|
||||
getPort := func(c SimpleConfig) func() (int, error) {
|
||||
return func() (int, error) {
|
||||
return c.Port, nil
|
||||
}
|
||||
}
|
||||
|
||||
// Adapt to work with DetailedConfig
|
||||
simplify := func(d DetailedConfig) SimpleConfig {
|
||||
return SimpleConfig{Port: d.Port}
|
||||
}
|
||||
|
||||
adapted := Contramap[int](simplify)(getPort)
|
||||
result, err := adapted(DetailedConfig{Host: "localhost", Port: 9000})()
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 9000, result)
|
||||
})
|
||||
|
||||
t.Run("preserves error", func(t *testing.T) {
|
||||
getError := func(c SimpleConfig) func() (int, error) {
|
||||
return func() (int, error) {
|
||||
return 0, fmt.Errorf("config error")
|
||||
}
|
||||
}
|
||||
|
||||
simplify := func(d DetailedConfig) SimpleConfig {
|
||||
return SimpleConfig{Port: d.Port}
|
||||
}
|
||||
|
||||
adapted := Contramap[int](simplify)(getError)
|
||||
_, err := adapted(DetailedConfig{Host: "localhost", Port: 9000})()
|
||||
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, "config error", err.Error())
|
||||
})
|
||||
}
|
||||
|
||||
// TestPromapWithIO tests Promap with actual IO effects
|
||||
func TestPromapWithIO(t *testing.T) {
|
||||
t.Run("transform IO result", func(t *testing.T) {
|
||||
counter := 0
|
||||
|
||||
// ReaderIOResult with side effect
|
||||
getPortWithEffect := func(c SimpleConfig) func() (int, error) {
|
||||
return func() (int, error) {
|
||||
counter++
|
||||
return c.Port, nil
|
||||
}
|
||||
}
|
||||
|
||||
simplify := func(d DetailedConfig) SimpleConfig {
|
||||
return SimpleConfig{Port: d.Port}
|
||||
}
|
||||
toString := strconv.Itoa
|
||||
|
||||
adapted := Promap(simplify, toString)(getPortWithEffect)
|
||||
result, err := adapted(DetailedConfig{Host: "localhost", Port: 8080})()
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "8080", result)
|
||||
assert.Equal(t, 1, counter) // Side effect occurred
|
||||
})
|
||||
|
||||
t.Run("side effect occurs even on error", func(t *testing.T) {
|
||||
counter := 0
|
||||
|
||||
getErrorWithEffect := func(c SimpleConfig) func() (int, error) {
|
||||
return func() (int, error) {
|
||||
counter++
|
||||
return 0, fmt.Errorf("io error")
|
||||
}
|
||||
}
|
||||
|
||||
simplify := func(d DetailedConfig) SimpleConfig {
|
||||
return SimpleConfig{Port: d.Port}
|
||||
}
|
||||
toString := strconv.Itoa
|
||||
|
||||
adapted := Promap(simplify, toString)(getErrorWithEffect)
|
||||
_, err := adapted(DetailedConfig{Host: "localhost", Port: 8080})()
|
||||
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, 1, counter) // Side effect occurred before error
|
||||
})
|
||||
}
|
||||
|
||||
// TestPromapComposition tests that Promap can be composed
|
||||
func TestPromapComposition(t *testing.T) {
|
||||
t.Run("compose two Promap transformations", func(t *testing.T) {
|
||||
type Config1 struct{ Value int }
|
||||
type Config2 struct{ Value int }
|
||||
type Config3 struct{ Value int }
|
||||
|
||||
reader := func(c Config1) func() (int, error) {
|
||||
return func() (int, error) {
|
||||
return c.Value, nil
|
||||
}
|
||||
}
|
||||
|
||||
f1 := func(c2 Config2) Config1 { return Config1{Value: c2.Value} }
|
||||
g1 := N.Mul(2)
|
||||
|
||||
f2 := func(c3 Config3) Config2 { return Config2{Value: c3.Value} }
|
||||
g2 := N.Add(10)
|
||||
|
||||
// Apply two Promap transformations
|
||||
step1 := Promap(f1, g1)(reader)
|
||||
step2 := Promap(f2, g2)(step1)
|
||||
|
||||
result, err := step2(Config3{Value: 5})()
|
||||
|
||||
// (5 * 2) + 10 = 20
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 20, result)
|
||||
})
|
||||
}
|
||||
76
v2/idiomatic/readerresult/profunctor.go
Normal file
76
v2/idiomatic/readerresult/profunctor.go
Normal file
@@ -0,0 +1,76 @@
|
||||
// Copyright (c) 2025 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 readerresult
|
||||
|
||||
import "github.com/IBM/fp-go/v2/idiomatic/result"
|
||||
|
||||
// Promap is the profunctor map operation that transforms both the input and output of a ReaderResult.
|
||||
// It applies f to the input environment (contravariantly) and g to the output value (covariantly).
|
||||
//
|
||||
// See: https://github.com/fantasyland/fantasy-land?tab=readme-ov-file#profunctor
|
||||
//
|
||||
// This operation allows you to:
|
||||
// - Adapt the environment type before passing it to the ReaderResult (via f)
|
||||
// - Transform the success value after the computation completes (via g)
|
||||
//
|
||||
// The error type is fixed as error and remains unchanged through the transformation.
|
||||
//
|
||||
// Type Parameters:
|
||||
// - E: The original environment type expected by the ReaderResult
|
||||
// - A: The original success type produced by the ReaderResult
|
||||
// - D: The new input environment type
|
||||
// - B: The new output success type
|
||||
//
|
||||
// Parameters:
|
||||
// - f: Function to transform the input environment from D to E (contravariant)
|
||||
// - g: Function to transform the output success value from A to B (covariant)
|
||||
//
|
||||
// Returns:
|
||||
// - A Kleisli arrow that takes a ReaderResult[E, A] and returns a ReaderResult[D, B]
|
||||
//
|
||||
//go:inline
|
||||
func Promap[E, A, D, B any](f func(D) E, g func(A) B) Kleisli[D, ReaderResult[E, A], B] {
|
||||
mp := result.Map(g)
|
||||
return func(rr ReaderResult[E, A]) ReaderResult[D, B] {
|
||||
return func(d D) (B, error) {
|
||||
return mp(rr(f(d)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Contramap changes the value of the local environment during the execution of a ReaderResult.
|
||||
// This is the contravariant functor operation that transforms the input environment.
|
||||
//
|
||||
// See: https://github.com/fantasyland/fantasy-land?tab=readme-ov-file#profunctor
|
||||
//
|
||||
// Contramap is useful for adapting a ReaderResult to work with a different environment type
|
||||
// by providing a function that converts the new environment to the expected one.
|
||||
//
|
||||
// Type Parameters:
|
||||
// - A: The success type (unchanged)
|
||||
// - R2: The new input environment type
|
||||
// - R1: The original environment type expected by the ReaderResult
|
||||
//
|
||||
// Parameters:
|
||||
// - f: Function to transform the environment from R2 to R1
|
||||
//
|
||||
// Returns:
|
||||
// - A Kleisli arrow that takes a ReaderResult[R1, A] and returns a ReaderResult[R2, A]
|
||||
//
|
||||
//go:inline
|
||||
func Contramap[A, R1, R2 any](f func(R2) R1) Kleisli[R2, ReaderResult[R1, A], A] {
|
||||
return Local[A](f)
|
||||
}
|
||||
238
v2/idiomatic/readerresult/profunctor_test.go
Normal file
238
v2/idiomatic/readerresult/profunctor_test.go
Normal file
@@ -0,0 +1,238 @@
|
||||
// Copyright (c) 2025 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 readerresult
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
N "github.com/IBM/fp-go/v2/number"
|
||||
R "github.com/IBM/fp-go/v2/reader"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type SimpleConfig struct {
|
||||
Port int
|
||||
}
|
||||
|
||||
type DetailedConfig struct {
|
||||
Host string
|
||||
Port int
|
||||
}
|
||||
|
||||
// TestPromapBasic tests basic Promap functionality
|
||||
func TestPromapBasic(t *testing.T) {
|
||||
t.Run("transform both input and output", func(t *testing.T) {
|
||||
// ReaderResult that reads port from SimpleConfig
|
||||
getPort := func(c SimpleConfig) (int, error) {
|
||||
return c.Port, nil
|
||||
}
|
||||
|
||||
// Transform DetailedConfig to SimpleConfig and int to string
|
||||
simplify := func(d DetailedConfig) SimpleConfig {
|
||||
return SimpleConfig{Port: d.Port}
|
||||
}
|
||||
toString := strconv.Itoa
|
||||
|
||||
adapted := Promap(simplify, toString)(getPort)
|
||||
result, err := adapted(DetailedConfig{Host: "localhost", Port: 8080})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "8080", result)
|
||||
})
|
||||
|
||||
t.Run("handles error case", func(t *testing.T) {
|
||||
// ReaderResult that returns an error
|
||||
getError := func(c SimpleConfig) (int, error) {
|
||||
return 0, fmt.Errorf("error occurred")
|
||||
}
|
||||
|
||||
simplify := func(d DetailedConfig) SimpleConfig {
|
||||
return SimpleConfig{Port: d.Port}
|
||||
}
|
||||
toString := strconv.Itoa
|
||||
|
||||
adapted := Promap(simplify, toString)(getError)
|
||||
_, err := adapted(DetailedConfig{Host: "localhost", Port: 8080})
|
||||
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, "error occurred", err.Error())
|
||||
})
|
||||
|
||||
t.Run("environment transformation with complex types", func(t *testing.T) {
|
||||
type Database struct {
|
||||
ConnectionString string
|
||||
}
|
||||
type AppConfig struct {
|
||||
DB Database
|
||||
}
|
||||
|
||||
getConnection := func(db Database) (string, error) {
|
||||
if db.ConnectionString == "" {
|
||||
return "", fmt.Errorf("empty connection string")
|
||||
}
|
||||
return db.ConnectionString, nil
|
||||
}
|
||||
|
||||
extractDB := func(cfg AppConfig) Database {
|
||||
return cfg.DB
|
||||
}
|
||||
addPrefix := func(s string) string {
|
||||
return "postgres://" + s
|
||||
}
|
||||
|
||||
adapted := Promap(extractDB, addPrefix)(getConnection)
|
||||
result, err := adapted(AppConfig{DB: Database{ConnectionString: "localhost:5432"}})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "postgres://localhost:5432", result)
|
||||
})
|
||||
}
|
||||
|
||||
// TestContramapBasic tests basic Contramap functionality
|
||||
func TestContramapBasic(t *testing.T) {
|
||||
t.Run("environment adaptation", func(t *testing.T) {
|
||||
// ReaderResult that reads from SimpleConfig
|
||||
getPort := func(c SimpleConfig) (int, error) {
|
||||
return c.Port, nil
|
||||
}
|
||||
|
||||
// Adapt to work with DetailedConfig
|
||||
simplify := func(d DetailedConfig) SimpleConfig {
|
||||
return SimpleConfig{Port: d.Port}
|
||||
}
|
||||
|
||||
adapted := Contramap[int](simplify)(getPort)
|
||||
result, err := adapted(DetailedConfig{Host: "localhost", Port: 9000})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 9000, result)
|
||||
})
|
||||
|
||||
t.Run("preserves error", func(t *testing.T) {
|
||||
getError := func(c SimpleConfig) (int, error) {
|
||||
return 0, fmt.Errorf("config error")
|
||||
}
|
||||
|
||||
simplify := func(d DetailedConfig) SimpleConfig {
|
||||
return SimpleConfig{Port: d.Port}
|
||||
}
|
||||
|
||||
adapted := Contramap[int](simplify)(getError)
|
||||
_, err := adapted(DetailedConfig{Host: "localhost", Port: 9000})
|
||||
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, "config error", err.Error())
|
||||
})
|
||||
|
||||
t.Run("multiple field extraction", func(t *testing.T) {
|
||||
type FullConfig struct {
|
||||
Host string
|
||||
Port int
|
||||
Protocol string
|
||||
}
|
||||
|
||||
getURL := func(c DetailedConfig) (string, error) {
|
||||
return fmt.Sprintf("%s:%d", c.Host, c.Port), nil
|
||||
}
|
||||
|
||||
extractHostPort := func(fc FullConfig) DetailedConfig {
|
||||
return DetailedConfig{Host: fc.Host, Port: fc.Port}
|
||||
}
|
||||
|
||||
adapted := Contramap[string](extractHostPort)(getURL)
|
||||
result, err := adapted(FullConfig{Host: "example.com", Port: 443, Protocol: "https"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "example.com:443", result)
|
||||
})
|
||||
}
|
||||
|
||||
// TestPromapComposition tests that Promap can be composed
|
||||
func TestPromapComposition(t *testing.T) {
|
||||
t.Run("compose two Promap transformations", func(t *testing.T) {
|
||||
type Config1 struct{ Value int }
|
||||
type Config2 struct{ Value int }
|
||||
type Config3 struct{ Value int }
|
||||
|
||||
reader := func(c Config1) (int, error) {
|
||||
return c.Value, nil
|
||||
}
|
||||
|
||||
f1 := func(c2 Config2) Config1 { return Config1{Value: c2.Value} }
|
||||
g1 := N.Mul(2)
|
||||
|
||||
f2 := func(c3 Config3) Config2 { return Config2{Value: c3.Value} }
|
||||
g2 := N.Add(10)
|
||||
|
||||
// Apply two Promap transformations
|
||||
step1 := Promap(f1, g1)(reader)
|
||||
step2 := Promap(f2, g2)(step1)
|
||||
|
||||
result, err := step2(Config3{Value: 5})
|
||||
|
||||
// (5 * 2) + 10 = 20
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 20, result)
|
||||
})
|
||||
|
||||
t.Run("compose Promap and Contramap", func(t *testing.T) {
|
||||
type Config1 struct{ Value int }
|
||||
type Config2 struct{ Value int }
|
||||
|
||||
reader := func(c Config1) (int, error) {
|
||||
return c.Value * 3, nil
|
||||
}
|
||||
|
||||
// First apply Contramap
|
||||
f1 := func(c2 Config2) Config1 { return Config1{Value: c2.Value} }
|
||||
step1 := Contramap[int](f1)(reader)
|
||||
|
||||
// Then apply Promap
|
||||
f2 := func(c2 Config2) Config2 { return c2 }
|
||||
g2 := func(n int) string { return fmt.Sprintf("result: %d", n) }
|
||||
step2 := Promap(f2, g2)(step1)
|
||||
|
||||
result, err := step2(Config2{Value: 7})
|
||||
|
||||
// 7 * 3 = 21
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "result: 21", result)
|
||||
})
|
||||
}
|
||||
|
||||
// TestPromapIdentityLaws tests profunctor identity laws
|
||||
func TestPromapIdentityLaws(t *testing.T) {
|
||||
t.Run("identity law", func(t *testing.T) {
|
||||
// Promap with identity functions should be identity
|
||||
reader := func(c SimpleConfig) (int, error) {
|
||||
return c.Port, nil
|
||||
}
|
||||
|
||||
identity := R.Ask[SimpleConfig]()
|
||||
identityInt := R.Ask[int]()
|
||||
|
||||
adapted := Promap(identity, identityInt)(reader)
|
||||
|
||||
config := SimpleConfig{Port: 8080}
|
||||
result1, err1 := reader(config)
|
||||
result2, err2 := adapted(config)
|
||||
|
||||
assert.Equal(t, err1, err2)
|
||||
assert.Equal(t, result1, result2)
|
||||
})
|
||||
}
|
||||
@@ -501,7 +501,7 @@ func BiMap[R, A, B any](f Endomorphism[error], g func(A) B) Operator[R, A, B] {
|
||||
// rr := readerresult.Of[Config](42)
|
||||
// adapted := readerresult.Local[int](toConfig)(rr)
|
||||
// // adapted now accepts DB instead of Config
|
||||
func Local[A, R2, R1 any](f func(R2) R1) func(ReaderResult[R1, A]) ReaderResult[R2, A] {
|
||||
func Local[A, R1, R2 any](f func(R2) R1) func(ReaderResult[R1, A]) ReaderResult[R2, A] {
|
||||
return func(rr ReaderResult[R1, A]) ReaderResult[R2, A] {
|
||||
return func(r R2) (A, error) {
|
||||
return rr(f(r))
|
||||
|
||||
@@ -22,6 +22,7 @@ import (
|
||||
"testing"
|
||||
|
||||
N "github.com/IBM/fp-go/v2/number"
|
||||
"github.com/IBM/fp-go/v2/reader"
|
||||
S "github.com/IBM/fp-go/v2/semigroup"
|
||||
STR "github.com/IBM/fp-go/v2/string"
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -267,7 +268,7 @@ func TestApV_ZeroValues(t *testing.T) {
|
||||
sg := makeErrorConcatSemigroup()
|
||||
apv := ApV[int, int](sg)
|
||||
|
||||
identity := func(x int) int { return x }
|
||||
identity := reader.Ask[int]()
|
||||
|
||||
value, verr := Right(0)
|
||||
fn, ferr := Right(identity)
|
||||
|
||||
@@ -240,7 +240,7 @@ func TestCopyFileChaining(t *testing.T) {
|
||||
// Chain two copy operations
|
||||
result := F.Pipe1(
|
||||
CopyFile(srcPath)(dst1Path),
|
||||
IOE.Chain[error](func(string) IOEither[error, string] {
|
||||
IOE.Chain(func(string) IOEither[error, string] {
|
||||
return CopyFile(dst1Path)(dst2Path)
|
||||
}),
|
||||
)()
|
||||
|
||||
@@ -141,7 +141,7 @@ func TestFilterOrElse_WithMap(t *testing.T) {
|
||||
onNegative := func(n int) string { return "negative number" }
|
||||
|
||||
filter := FilterOrElse(isPositive, onNegative)
|
||||
double := Map[string](func(n int) int { return n * 2 })
|
||||
double := Map[string](N.Mul(2))
|
||||
|
||||
// Compose: filter then double
|
||||
result1 := double(filter(Right[string](5)))()
|
||||
|
||||
@@ -47,14 +47,14 @@ import (
|
||||
// Example - Remove duplicate integers:
|
||||
//
|
||||
// seq := From(1, 2, 3, 2, 4, 1, 5)
|
||||
// unique := Uniq(func(x int) int { return x })
|
||||
// unique := Uniq(reader.Ask[int]())
|
||||
// result := unique(seq)
|
||||
// // yields: 1, 2, 3, 4, 5
|
||||
//
|
||||
// Example - Unique by string length:
|
||||
//
|
||||
// seq := From("a", "bb", "c", "dd", "eee")
|
||||
// uniqueByLength := Uniq(func(s string) int { return len(s) })
|
||||
// uniqueByLength := Uniq(S.Size)
|
||||
// result := uniqueByLength(seq)
|
||||
// // yields: "a", "bb", "eee" (first occurrence of each length)
|
||||
//
|
||||
@@ -82,14 +82,14 @@ import (
|
||||
// Example - Empty sequence:
|
||||
//
|
||||
// seq := Empty[int]()
|
||||
// unique := Uniq(func(x int) int { return x })
|
||||
// unique := Uniq(reader.Ask[int]())
|
||||
// result := unique(seq)
|
||||
// // yields: nothing (empty sequence)
|
||||
//
|
||||
// Example - All duplicates:
|
||||
//
|
||||
// seq := From(1, 1, 1, 1)
|
||||
// unique := Uniq(func(x int) int { return x })
|
||||
// unique := Uniq(reader.Ask[int]())
|
||||
// result := unique(seq)
|
||||
// // yields: 1 (only first occurrence)
|
||||
func Uniq[A any, K comparable](f func(A) K) Operator[A, A] {
|
||||
|
||||
@@ -377,7 +377,7 @@ func ExampleUniq() {
|
||||
|
||||
func ExampleUniq_byLength() {
|
||||
seq := From("a", "bb", "c", "dd", "eee")
|
||||
uniqueByLength := Uniq(func(s string) int { return len(s) })
|
||||
uniqueByLength := Uniq(S.Size)
|
||||
result := uniqueByLength(seq)
|
||||
|
||||
for v := range result {
|
||||
|
||||
@@ -25,6 +25,7 @@ import (
|
||||
M "github.com/IBM/fp-go/v2/monoid"
|
||||
N "github.com/IBM/fp-go/v2/number"
|
||||
L "github.com/IBM/fp-go/v2/optics/lens"
|
||||
"github.com/IBM/fp-go/v2/reader"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
@@ -497,7 +498,7 @@ func TestMapComposition(t *testing.T) {
|
||||
Of(5),
|
||||
Map(N.Mul(2)),
|
||||
Map(N.Add(10)),
|
||||
Map(func(x int) int { return x }),
|
||||
Map(reader.Ask[int]()),
|
||||
)
|
||||
|
||||
assert.Equal(t, 20, result())
|
||||
|
||||
@@ -154,7 +154,7 @@ FunctionMonoid - Creates a monoid for functions when the codomain has a monoid:
|
||||
|
||||
funcMonoid := monoid.FunctionMonoid[string, int](intAddMonoid)
|
||||
|
||||
f1 := func(s string) int { return len(s) }
|
||||
f1 := S.Size
|
||||
f2 := func(s string) int { return len(s) * 2 }
|
||||
|
||||
// Combine functions: result(x) = f1(x) + f2(x)
|
||||
|
||||
@@ -49,7 +49,7 @@ import (
|
||||
// funcMonoid := FunctionMonoid[string, int](intAddMonoid)
|
||||
//
|
||||
// // Define some functions
|
||||
// f1 := func(s string) int { return len(s) }
|
||||
// f1 := S.Size
|
||||
// f2 := func(s string) int { return len(s) * 2 }
|
||||
//
|
||||
// // Combine functions: result(x) = f1(x) + f2(x)
|
||||
|
||||
@@ -262,7 +262,7 @@ func imap[S any, AB ~func(A) B, BA ~func(B) A, A, B any](sa Prism[S, A], ab AB,
|
||||
//
|
||||
// intPrism := MakePrism(...) // Prism[Result, int]
|
||||
// stringPrism := IMap[Result](
|
||||
// func(n int) string { return strconv.Itoa(n) },
|
||||
// strconv.Itoa,
|
||||
// func(s string) int { n, _ := strconv.Atoi(s); return n },
|
||||
// )(intPrism) // Prism[Result, string]
|
||||
func IMap[S any, AB ~func(A) B, BA ~func(B) A, A, B any](ab AB, ba BA) Operator[S, A, B] {
|
||||
|
||||
@@ -83,6 +83,8 @@ func Monoid[A any]() func(S.Semigroup[A]) M.Monoid[Option[A]] {
|
||||
// intMonoid := monoid.MakeMonoid(func(a, b int) int { return a + b }, 0)
|
||||
// optMonoid := AlternativeMonoid(intMonoid)
|
||||
// result := optMonoid.Concat(Some(2), Some(3)) // Some(5)
|
||||
//
|
||||
//go:inline
|
||||
func AlternativeMonoid[A any](m M.Monoid[A]) M.Monoid[Option[A]] {
|
||||
return M.AlternativeMonoid(
|
||||
Of[A],
|
||||
@@ -103,9 +105,81 @@ func AlternativeMonoid[A any](m M.Monoid[A]) M.Monoid[Option[A]] {
|
||||
// optMonoid.Concat(Some(2), Some(3)) // Some(2) - returns first Some
|
||||
// optMonoid.Concat(None[int](), Some(3)) // Some(3)
|
||||
// optMonoid.Empty() // None
|
||||
//
|
||||
//go:inline
|
||||
func AltMonoid[A any]() M.Monoid[Option[A]] {
|
||||
return M.AltMonoid(
|
||||
None[A],
|
||||
MonadAlt[A],
|
||||
)
|
||||
}
|
||||
|
||||
// takeFirst is a helper function that returns the first Some value, or the second if the first is None.
|
||||
func takeFirst[A any](l, r Option[A]) Option[A] {
|
||||
if IsSome(l) {
|
||||
return l
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
// FirstMonoid creates a Monoid for Option[A] that returns the first Some value.
|
||||
// This monoid prefers the left operand when it is Some, otherwise returns the right operand.
|
||||
// The empty value is None.
|
||||
//
|
||||
// This is equivalent to AltMonoid but implemented more directly.
|
||||
//
|
||||
// Truth table:
|
||||
//
|
||||
// | x | y | concat(x, y) |
|
||||
// | ------- | ------- | ------------ |
|
||||
// | none | none | none |
|
||||
// | some(a) | none | some(a) |
|
||||
// | none | some(b) | some(b) |
|
||||
// | some(a) | some(b) | some(a) |
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// optMonoid := FirstMonoid[int]()
|
||||
// optMonoid.Concat(Some(2), Some(3)) // Some(2) - returns first Some
|
||||
// optMonoid.Concat(None[int](), Some(3)) // Some(3)
|
||||
// optMonoid.Concat(Some(2), None[int]()) // Some(2)
|
||||
// optMonoid.Empty() // None
|
||||
//
|
||||
//go:inline
|
||||
func FirstMonoid[A any]() M.Monoid[Option[A]] {
|
||||
return M.MakeMonoid(takeFirst[A], None[A]())
|
||||
}
|
||||
|
||||
// takeLast is a helper function that returns the last Some value, or the first if the last is None.
|
||||
func takeLast[A any](l, r Option[A]) Option[A] {
|
||||
if IsSome(r) {
|
||||
return r
|
||||
}
|
||||
return l
|
||||
}
|
||||
|
||||
// LastMonoid creates a Monoid for Option[A] that returns the last Some value.
|
||||
// This monoid prefers the right operand when it is Some, otherwise returns the left operand.
|
||||
// The empty value is None.
|
||||
//
|
||||
// Truth table:
|
||||
//
|
||||
// | x | y | concat(x, y) |
|
||||
// | ------- | ------- | ------------ |
|
||||
// | none | none | none |
|
||||
// | some(a) | none | some(a) |
|
||||
// | none | some(b) | some(b) |
|
||||
// | some(a) | some(b) | some(b) |
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// optMonoid := LastMonoid[int]()
|
||||
// optMonoid.Concat(Some(2), Some(3)) // Some(3) - returns last Some
|
||||
// optMonoid.Concat(None[int](), Some(3)) // Some(3)
|
||||
// optMonoid.Concat(Some(2), None[int]()) // Some(2)
|
||||
// optMonoid.Empty() // None
|
||||
//
|
||||
//go:inline
|
||||
func LastMonoid[A any]() M.Monoid[Option[A]] {
|
||||
return M.MakeMonoid(takeLast[A], None[A]())
|
||||
}
|
||||
|
||||
445
v2/option/monoid_test.go
Normal file
445
v2/option/monoid_test.go
Normal file
@@ -0,0 +1,445 @@
|
||||
// Copyright (c) 2025 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 option
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
M "github.com/IBM/fp-go/v2/monoid"
|
||||
N "github.com/IBM/fp-go/v2/number"
|
||||
S "github.com/IBM/fp-go/v2/semigroup"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// TestSemigroupAssociativity tests the associativity law for Semigroup
|
||||
func TestSemigroupAssociativity(t *testing.T) {
|
||||
intSemigroup := S.MakeSemigroup(func(a, b int) int { return a + b })
|
||||
optSemigroup := Semigroup[int]()(intSemigroup)
|
||||
|
||||
a := Some(1)
|
||||
b := Some(2)
|
||||
c := Some(3)
|
||||
|
||||
// Test that (a • b) • c = a • (b • c)
|
||||
left := optSemigroup.Concat(optSemigroup.Concat(a, b), c)
|
||||
right := optSemigroup.Concat(a, optSemigroup.Concat(b, c))
|
||||
|
||||
assert.Equal(t, left, right)
|
||||
assert.Equal(t, Some(6), left)
|
||||
}
|
||||
|
||||
// TestSemigroupWithNone tests Semigroup behavior with None values
|
||||
func TestSemigroupWithNone(t *testing.T) {
|
||||
intSemigroup := S.MakeSemigroup(func(a, b int) int { return a + b })
|
||||
optSemigroup := Semigroup[int]()(intSemigroup)
|
||||
|
||||
t.Run("None with None", func(t *testing.T) {
|
||||
result := optSemigroup.Concat(None[int](), None[int]())
|
||||
assert.Equal(t, None[int](), result)
|
||||
})
|
||||
|
||||
t.Run("associativity with None", func(t *testing.T) {
|
||||
a := None[int]()
|
||||
b := Some(2)
|
||||
c := Some(3)
|
||||
|
||||
left := optSemigroup.Concat(optSemigroup.Concat(a, b), c)
|
||||
right := optSemigroup.Concat(a, optSemigroup.Concat(b, c))
|
||||
|
||||
assert.Equal(t, left, right)
|
||||
assert.Equal(t, Some(5), left)
|
||||
})
|
||||
}
|
||||
|
||||
// TestMonoidIdentityLaws tests the identity laws for Monoid
|
||||
func TestMonoidIdentityLaws(t *testing.T) {
|
||||
intSemigroup := S.MakeSemigroup(func(a, b int) int { return a + b })
|
||||
optMonoid := Monoid[int]()(intSemigroup)
|
||||
|
||||
t.Run("left identity with Some", func(t *testing.T) {
|
||||
x := Some(5)
|
||||
result := optMonoid.Concat(optMonoid.Empty(), x)
|
||||
assert.Equal(t, x, result)
|
||||
})
|
||||
|
||||
t.Run("right identity with Some", func(t *testing.T) {
|
||||
x := Some(5)
|
||||
result := optMonoid.Concat(x, optMonoid.Empty())
|
||||
assert.Equal(t, x, result)
|
||||
})
|
||||
|
||||
t.Run("left identity with None", func(t *testing.T) {
|
||||
x := None[int]()
|
||||
result := optMonoid.Concat(optMonoid.Empty(), x)
|
||||
assert.Equal(t, x, result)
|
||||
})
|
||||
|
||||
t.Run("right identity with None", func(t *testing.T) {
|
||||
x := None[int]()
|
||||
result := optMonoid.Concat(x, optMonoid.Empty())
|
||||
assert.Equal(t, x, result)
|
||||
})
|
||||
}
|
||||
|
||||
// TestAlternativeMonoidIdentityLaws tests identity laws for AlternativeMonoid
|
||||
func TestAlternativeMonoidIdentityLaws(t *testing.T) {
|
||||
intMonoid := N.MonoidSum[int]()
|
||||
optMonoid := AlternativeMonoid(intMonoid)
|
||||
|
||||
t.Run("left identity", func(t *testing.T) {
|
||||
x := Some(5)
|
||||
result := optMonoid.Concat(optMonoid.Empty(), x)
|
||||
assert.Equal(t, x, result)
|
||||
})
|
||||
|
||||
t.Run("right identity", func(t *testing.T) {
|
||||
x := Some(5)
|
||||
result := optMonoid.Concat(x, optMonoid.Empty())
|
||||
assert.Equal(t, x, result)
|
||||
})
|
||||
|
||||
t.Run("empty is Some(0)", func(t *testing.T) {
|
||||
empty := optMonoid.Empty()
|
||||
assert.Equal(t, Some(0), empty)
|
||||
})
|
||||
}
|
||||
|
||||
// TestAltMonoidIdentityLaws tests identity laws for AltMonoid
|
||||
func TestAltMonoidIdentityLaws(t *testing.T) {
|
||||
optMonoid := AltMonoid[int]()
|
||||
|
||||
t.Run("left identity", func(t *testing.T) {
|
||||
x := Some(5)
|
||||
result := optMonoid.Concat(optMonoid.Empty(), x)
|
||||
assert.Equal(t, x, result)
|
||||
})
|
||||
|
||||
t.Run("right identity", func(t *testing.T) {
|
||||
x := Some(5)
|
||||
result := optMonoid.Concat(x, optMonoid.Empty())
|
||||
assert.Equal(t, x, result)
|
||||
})
|
||||
|
||||
t.Run("associativity", func(t *testing.T) {
|
||||
a := Some(1)
|
||||
b := None[int]()
|
||||
c := Some(3)
|
||||
|
||||
left := optMonoid.Concat(optMonoid.Concat(a, b), c)
|
||||
right := optMonoid.Concat(a, optMonoid.Concat(b, c))
|
||||
|
||||
assert.Equal(t, left, right)
|
||||
assert.Equal(t, Some(1), left)
|
||||
})
|
||||
}
|
||||
|
||||
// TestFirstMonoid tests the FirstMonoid implementation
|
||||
func TestFirstMonoid(t *testing.T) {
|
||||
optMonoid := FirstMonoid[int]()
|
||||
|
||||
t.Run("both Some values - returns first", func(t *testing.T) {
|
||||
result := optMonoid.Concat(Some(2), Some(3))
|
||||
assert.Equal(t, Some(2), result)
|
||||
})
|
||||
|
||||
t.Run("left Some, right None", func(t *testing.T) {
|
||||
result := optMonoid.Concat(Some(2), None[int]())
|
||||
assert.Equal(t, Some(2), result)
|
||||
})
|
||||
|
||||
t.Run("left None, right Some", func(t *testing.T) {
|
||||
result := optMonoid.Concat(None[int](), Some(3))
|
||||
assert.Equal(t, Some(3), result)
|
||||
})
|
||||
|
||||
t.Run("both None", func(t *testing.T) {
|
||||
result := optMonoid.Concat(None[int](), None[int]())
|
||||
assert.Equal(t, None[int](), result)
|
||||
})
|
||||
|
||||
t.Run("empty value", func(t *testing.T) {
|
||||
empty := optMonoid.Empty()
|
||||
assert.Equal(t, None[int](), empty)
|
||||
})
|
||||
|
||||
t.Run("left identity", func(t *testing.T) {
|
||||
x := Some(5)
|
||||
result := optMonoid.Concat(optMonoid.Empty(), x)
|
||||
assert.Equal(t, x, result)
|
||||
})
|
||||
|
||||
t.Run("right identity", func(t *testing.T) {
|
||||
x := Some(5)
|
||||
result := optMonoid.Concat(x, optMonoid.Empty())
|
||||
assert.Equal(t, x, result)
|
||||
})
|
||||
|
||||
t.Run("associativity", func(t *testing.T) {
|
||||
a := Some(1)
|
||||
b := Some(2)
|
||||
c := Some(3)
|
||||
|
||||
left := optMonoid.Concat(optMonoid.Concat(a, b), c)
|
||||
right := optMonoid.Concat(a, optMonoid.Concat(b, c))
|
||||
|
||||
assert.Equal(t, left, right)
|
||||
assert.Equal(t, Some(1), left)
|
||||
})
|
||||
|
||||
t.Run("multiple concatenations", func(t *testing.T) {
|
||||
// Should return the first Some value encountered
|
||||
result := optMonoid.Concat(
|
||||
optMonoid.Concat(None[int](), Some(1)),
|
||||
optMonoid.Concat(Some(2), Some(3)),
|
||||
)
|
||||
assert.Equal(t, Some(1), result)
|
||||
})
|
||||
|
||||
t.Run("with strings", func(t *testing.T) {
|
||||
strMonoid := FirstMonoid[string]()
|
||||
|
||||
result := strMonoid.Concat(Some("first"), Some("second"))
|
||||
assert.Equal(t, Some("first"), result)
|
||||
|
||||
result = strMonoid.Concat(None[string](), Some("second"))
|
||||
assert.Equal(t, Some("second"), result)
|
||||
})
|
||||
}
|
||||
|
||||
// TestLastMonoid tests the LastMonoid implementation
|
||||
func TestLastMonoid(t *testing.T) {
|
||||
optMonoid := LastMonoid[int]()
|
||||
|
||||
t.Run("both Some values - returns last", func(t *testing.T) {
|
||||
result := optMonoid.Concat(Some(2), Some(3))
|
||||
assert.Equal(t, Some(3), result)
|
||||
})
|
||||
|
||||
t.Run("left Some, right None", func(t *testing.T) {
|
||||
result := optMonoid.Concat(Some(2), None[int]())
|
||||
assert.Equal(t, Some(2), result)
|
||||
})
|
||||
|
||||
t.Run("left None, right Some", func(t *testing.T) {
|
||||
result := optMonoid.Concat(None[int](), Some(3))
|
||||
assert.Equal(t, Some(3), result)
|
||||
})
|
||||
|
||||
t.Run("both None", func(t *testing.T) {
|
||||
result := optMonoid.Concat(None[int](), None[int]())
|
||||
assert.Equal(t, None[int](), result)
|
||||
})
|
||||
|
||||
t.Run("empty value", func(t *testing.T) {
|
||||
empty := optMonoid.Empty()
|
||||
assert.Equal(t, None[int](), empty)
|
||||
})
|
||||
|
||||
t.Run("left identity", func(t *testing.T) {
|
||||
x := Some(5)
|
||||
result := optMonoid.Concat(optMonoid.Empty(), x)
|
||||
assert.Equal(t, x, result)
|
||||
})
|
||||
|
||||
t.Run("right identity", func(t *testing.T) {
|
||||
x := Some(5)
|
||||
result := optMonoid.Concat(x, optMonoid.Empty())
|
||||
assert.Equal(t, x, result)
|
||||
})
|
||||
|
||||
t.Run("associativity", func(t *testing.T) {
|
||||
a := Some(1)
|
||||
b := Some(2)
|
||||
c := Some(3)
|
||||
|
||||
left := optMonoid.Concat(optMonoid.Concat(a, b), c)
|
||||
right := optMonoid.Concat(a, optMonoid.Concat(b, c))
|
||||
|
||||
assert.Equal(t, left, right)
|
||||
assert.Equal(t, Some(3), left)
|
||||
})
|
||||
|
||||
t.Run("multiple concatenations", func(t *testing.T) {
|
||||
// Should return the last Some value encountered
|
||||
result := optMonoid.Concat(
|
||||
optMonoid.Concat(Some(1), Some(2)),
|
||||
optMonoid.Concat(Some(3), None[int]()),
|
||||
)
|
||||
assert.Equal(t, Some(3), result)
|
||||
})
|
||||
|
||||
t.Run("with strings", func(t *testing.T) {
|
||||
strMonoid := LastMonoid[string]()
|
||||
|
||||
result := strMonoid.Concat(Some("first"), Some("second"))
|
||||
assert.Equal(t, Some("second"), result)
|
||||
|
||||
result = strMonoid.Concat(Some("first"), None[string]())
|
||||
assert.Equal(t, Some("first"), result)
|
||||
})
|
||||
}
|
||||
|
||||
// TestFirstMonoidVsAltMonoid verifies FirstMonoid and AltMonoid have the same behavior
|
||||
func TestFirstMonoidVsAltMonoid(t *testing.T) {
|
||||
firstMonoid := FirstMonoid[int]()
|
||||
altMonoid := AltMonoid[int]()
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
left Option[int]
|
||||
right Option[int]
|
||||
}{
|
||||
{"both Some", Some(1), Some(2)},
|
||||
{"left Some, right None", Some(1), None[int]()},
|
||||
{"left None, right Some", None[int](), Some(2)},
|
||||
{"both None", None[int](), None[int]()},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
firstResult := firstMonoid.Concat(tc.left, tc.right)
|
||||
altResult := altMonoid.Concat(tc.left, tc.right)
|
||||
assert.Equal(t, firstResult, altResult, "FirstMonoid and AltMonoid should behave the same")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestFirstMonoidVsLastMonoid verifies the difference between FirstMonoid and LastMonoid
|
||||
func TestFirstMonoidVsLastMonoid(t *testing.T) {
|
||||
firstMonoid := FirstMonoid[int]()
|
||||
lastMonoid := LastMonoid[int]()
|
||||
|
||||
t.Run("both Some - different results", func(t *testing.T) {
|
||||
firstResult := firstMonoid.Concat(Some(1), Some(2))
|
||||
lastResult := lastMonoid.Concat(Some(1), Some(2))
|
||||
|
||||
assert.Equal(t, Some(1), firstResult)
|
||||
assert.Equal(t, Some(2), lastResult)
|
||||
assert.NotEqual(t, firstResult, lastResult)
|
||||
})
|
||||
|
||||
t.Run("with None - same results", func(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
left Option[int]
|
||||
right Option[int]
|
||||
expected Option[int]
|
||||
}{
|
||||
{"left Some, right None", Some(1), None[int](), Some(1)},
|
||||
{"left None, right Some", None[int](), Some(2), Some(2)},
|
||||
{"both None", None[int](), None[int](), None[int]()},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
firstResult := firstMonoid.Concat(tc.left, tc.right)
|
||||
lastResult := lastMonoid.Concat(tc.left, tc.right)
|
||||
|
||||
assert.Equal(t, tc.expected, firstResult)
|
||||
assert.Equal(t, tc.expected, lastResult)
|
||||
assert.Equal(t, firstResult, lastResult)
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// TestMonoidComparison compares different monoid implementations
|
||||
func TestMonoidComparison(t *testing.T) {
|
||||
t.Run("Monoid vs AlternativeMonoid with addition", func(t *testing.T) {
|
||||
intSemigroup := S.MakeSemigroup(func(a, b int) int { return a + b })
|
||||
regularMonoid := Monoid[int]()(intSemigroup)
|
||||
|
||||
intMonoid := M.MakeMonoid(func(a, b int) int { return a + b }, 0)
|
||||
altMonoid := AlternativeMonoid(intMonoid)
|
||||
|
||||
// Both should combine Some values the same way
|
||||
assert.Equal(t,
|
||||
regularMonoid.Concat(Some(2), Some(3)),
|
||||
altMonoid.Concat(Some(2), Some(3)),
|
||||
)
|
||||
|
||||
// But empty values differ
|
||||
assert.Equal(t, None[int](), regularMonoid.Empty())
|
||||
assert.Equal(t, Some(0), altMonoid.Empty())
|
||||
})
|
||||
}
|
||||
|
||||
// TestMonoidLaws verifies monoid laws for all monoid implementations
|
||||
func TestMonoidLaws(t *testing.T) {
|
||||
t.Run("Monoid with addition", func(t *testing.T) {
|
||||
intSemigroup := N.SemigroupSum[int]()
|
||||
optMonoid := Monoid[int]()(intSemigroup)
|
||||
|
||||
a := Some(1)
|
||||
b := Some(2)
|
||||
c := Some(3)
|
||||
|
||||
// Associativity: (a • b) • c = a • (b • c)
|
||||
left := optMonoid.Concat(optMonoid.Concat(a, b), c)
|
||||
right := optMonoid.Concat(a, optMonoid.Concat(b, c))
|
||||
assert.Equal(t, left, right)
|
||||
|
||||
// Left identity: Empty() • a = a
|
||||
leftId := optMonoid.Concat(optMonoid.Empty(), a)
|
||||
assert.Equal(t, a, leftId)
|
||||
|
||||
// Right identity: a • Empty() = a
|
||||
rightId := optMonoid.Concat(a, optMonoid.Empty())
|
||||
assert.Equal(t, a, rightId)
|
||||
})
|
||||
|
||||
t.Run("FirstMonoid laws", func(t *testing.T) {
|
||||
optMonoid := FirstMonoid[int]()
|
||||
|
||||
a := Some(1)
|
||||
b := Some(2)
|
||||
c := Some(3)
|
||||
|
||||
// Associativity
|
||||
left := optMonoid.Concat(optMonoid.Concat(a, b), c)
|
||||
right := optMonoid.Concat(a, optMonoid.Concat(b, c))
|
||||
assert.Equal(t, left, right)
|
||||
|
||||
// Left identity
|
||||
leftId := optMonoid.Concat(optMonoid.Empty(), a)
|
||||
assert.Equal(t, a, leftId)
|
||||
|
||||
// Right identity
|
||||
rightId := optMonoid.Concat(a, optMonoid.Empty())
|
||||
assert.Equal(t, a, rightId)
|
||||
})
|
||||
|
||||
t.Run("LastMonoid laws", func(t *testing.T) {
|
||||
optMonoid := LastMonoid[int]()
|
||||
|
||||
a := Some(1)
|
||||
b := Some(2)
|
||||
c := Some(3)
|
||||
|
||||
// Associativity
|
||||
left := optMonoid.Concat(optMonoid.Concat(a, b), c)
|
||||
right := optMonoid.Concat(a, optMonoid.Concat(b, c))
|
||||
assert.Equal(t, left, right)
|
||||
|
||||
// Left identity
|
||||
leftId := optMonoid.Concat(optMonoid.Empty(), a)
|
||||
assert.Equal(t, a, leftId)
|
||||
|
||||
// Right identity
|
||||
rightId := optMonoid.Concat(a, optMonoid.Empty())
|
||||
assert.Equal(t, a, rightId)
|
||||
})
|
||||
}
|
||||
@@ -156,6 +156,8 @@ func Reverse[T any](o Ord[T]) Ord[T] {
|
||||
// This allows ordering values of type B by first transforming them to type A
|
||||
// and then using the ordering for type A.
|
||||
//
|
||||
// See: https://github.com/fantasyland/fantasy-land?tab=readme-ov-file#profunctor
|
||||
//
|
||||
// Parameters:
|
||||
// - f: A transformation function from B to A
|
||||
//
|
||||
|
||||
@@ -73,7 +73,7 @@ Map operations transform one or both values:
|
||||
// Map both values
|
||||
p4 := pair.MonadBiMap(p,
|
||||
func(n int) string { return fmt.Sprintf("%d", n) },
|
||||
func(s string) int { return len(s) },
|
||||
S.Size,
|
||||
) // Pair[string, int]{"5", 5}
|
||||
|
||||
Curried versions for composition:
|
||||
@@ -91,7 +91,7 @@ Curried versions for composition:
|
||||
// Compose multiple transformations
|
||||
transform := F.Flow2(
|
||||
pair.MapHead[string](N.Mul(2)),
|
||||
pair.MapTail[int](func(s string) int { return len(s) }),
|
||||
pair.MapTail[int](S.Size),
|
||||
)
|
||||
result := transform(p) // Pair[int, int]{10, 5}
|
||||
|
||||
@@ -147,7 +147,7 @@ Apply functions wrapped in pairs to values in pairs:
|
||||
intSum := N.SemigroupSum[int]()
|
||||
|
||||
// Function in a pair
|
||||
pf := pair.MakePair(10, func(s string) int { return len(s) })
|
||||
pf := pair.MakePair(10, S.Size)
|
||||
|
||||
// Value in a pair
|
||||
pv := pair.MakePair(5, "hello")
|
||||
@@ -244,7 +244,7 @@ Functor - Map over values:
|
||||
|
||||
// Functor for tail
|
||||
functor := pair.FunctorTail[int, string, int]()
|
||||
mapper := functor.Map(func(s string) int { return len(s) })
|
||||
mapper := functor.Map(S.Size)
|
||||
|
||||
p := pair.MakePair(5, "hello")
|
||||
result := mapper(p) // Pair[int, int]{5, 5}
|
||||
@@ -267,7 +267,7 @@ Applicative - Apply wrapped functions:
|
||||
applicative := pair.ApplicativeTail[string, int, int](intSum)
|
||||
|
||||
// Create a pair with a function
|
||||
pf := applicative.Of(func(s string) int { return len(s) })
|
||||
pf := applicative.Of(S.Size)
|
||||
|
||||
// Apply to a value
|
||||
pv := pair.MakePair(5, "hello")
|
||||
|
||||
@@ -233,7 +233,7 @@ func PointedTail[B, A any](m monoid.Monoid[A]) pointed.Pointed[B, Pair[A, B]] {
|
||||
// Example:
|
||||
//
|
||||
// functor := pair.FunctorTail[string, int, int]()
|
||||
// mapper := functor.Map(func(s string) int { return len(s) })
|
||||
// mapper := functor.Map(S.Size)
|
||||
// p := pair.MakePair(5, "hello")
|
||||
// p2 := mapper(p) // Pair[int, int]{5, 5}
|
||||
func FunctorTail[B, A, B1 any]() functor.Functor[B, B1, Pair[A, B], Pair[A, B1]] {
|
||||
@@ -250,7 +250,7 @@ func FunctorTail[B, A, B1 any]() functor.Functor[B, B1, Pair[A, B], Pair[A, B1]]
|
||||
//
|
||||
// intSum := M.MonoidSum[int]()
|
||||
// applicative := pair.ApplicativeTail[string, int, int](intSum)
|
||||
// pf := applicative.Of(func(s string) int { return len(s) })
|
||||
// pf := applicative.Of(S.Size)
|
||||
// pv := pair.MakePair(5, "hello")
|
||||
// result := applicative.Ap(pv)(pf) // Pair[int, int]{5, 5}
|
||||
func ApplicativeTail[B, A, B1 any](m monoid.Monoid[A]) applicative.Applicative[B, B1, Pair[A, B], Pair[A, B1], Pair[A, func(B) B1]] {
|
||||
@@ -291,7 +291,7 @@ func Pointed[B, A any](m monoid.Monoid[A]) pointed.Pointed[B, Pair[A, B]] {
|
||||
// Example:
|
||||
//
|
||||
// functor := pair.Functor[string, int, int]()
|
||||
// mapper := functor.Map(func(s string) int { return len(s) })
|
||||
// mapper := functor.Map(S.Size)
|
||||
// p := pair.MakePair(5, "hello")
|
||||
// p2 := mapper(p) // Pair[int, int]{5, 5}
|
||||
func Functor[B, A, B1 any]() functor.Functor[B, B1, Pair[A, B], Pair[A, B1]] {
|
||||
@@ -307,7 +307,7 @@ func Functor[B, A, B1 any]() functor.Functor[B, B1, Pair[A, B], Pair[A, B1]] {
|
||||
//
|
||||
// intSum := M.MonoidSum[int]()
|
||||
// applicative := pair.Applicative[string, int, int](intSum)
|
||||
// pf := applicative.Of(func(s string) int { return len(s) })
|
||||
// pf := applicative.Of(S.Size)
|
||||
// pv := pair.MakePair(5, "hello")
|
||||
// result := applicative.Ap(pv)(pf) // Pair[int, int]{5, 5}
|
||||
func Applicative[B, A, B1 any](m monoid.Monoid[A]) applicative.Applicative[B, B1, Pair[A, B], Pair[A, B1], Pair[A, func(B) B1]] {
|
||||
|
||||
315
v2/pair/monoid.go
Normal file
315
v2/pair/monoid.go
Normal file
@@ -0,0 +1,315 @@
|
||||
// Copyright (c) 2024 - 2025 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 (
|
||||
F "github.com/IBM/fp-go/v2/function"
|
||||
M "github.com/IBM/fp-go/v2/monoid"
|
||||
)
|
||||
|
||||
// Monoid creates a simple component-wise monoid for [Pair].
|
||||
//
|
||||
// This function creates a monoid that combines pairs by independently combining their
|
||||
// head and tail components using the provided monoids. Both components are combined
|
||||
// in NORMAL left-to-right order.
|
||||
//
|
||||
// IMPORTANT: This is DIFFERENT from [ApplicativeMonoidTail] and [ApplicativeMonoidHead],
|
||||
// which use applicative functor operations and reverse the order of the non-focused component.
|
||||
//
|
||||
// Use this function when you want:
|
||||
// - Simple, predictable left-to-right combination for both components
|
||||
// - Behavior that matches intuition for non-commutative operations
|
||||
// - Direct component-wise combination without applicative functor semantics
|
||||
//
|
||||
// Use [ApplicativeMonoidTail] or [ApplicativeMonoidHead] when you need applicative
|
||||
// functor semantics for lifting monoid operations into the Pair context.
|
||||
//
|
||||
// Parameters:
|
||||
// - l: A monoid for the head (left) values of type L
|
||||
// - r: A monoid for the tail (right) values of type R
|
||||
//
|
||||
// Returns:
|
||||
// - A Monoid[Pair[L, R]] that combines both components left-to-right
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// import (
|
||||
// N "github.com/IBM/fp-go/v2/number"
|
||||
// S "github.com/IBM/fp-go/v2/string"
|
||||
// )
|
||||
//
|
||||
// intAdd := N.MonoidSum[int]()
|
||||
// strConcat := S.Monoid
|
||||
//
|
||||
// pairMonoid := pair.Monoid(intAdd, strConcat)
|
||||
//
|
||||
// p1 := pair.MakePair(5, "hello")
|
||||
// p2 := pair.MakePair(10, " world")
|
||||
//
|
||||
// result := pairMonoid.Concat(p1, p2)
|
||||
// // result is Pair[int, string]{15, "hello world"}
|
||||
// // Both components combine left-to-right: (5+10, "hello"+" world")
|
||||
//
|
||||
// empty := pairMonoid.Empty()
|
||||
// // empty is Pair[int, string]{0, ""}
|
||||
//
|
||||
// Comparison with ApplicativeMonoidTail:
|
||||
//
|
||||
// strConcat := S.Monoid
|
||||
//
|
||||
// // Simple component-wise monoid
|
||||
// simpleMonoid := pair.Monoid(strConcat, strConcat)
|
||||
// p1 := pair.MakePair("A", "1")
|
||||
// p2 := pair.MakePair("B", "2")
|
||||
// result1 := simpleMonoid.Concat(p1, p2)
|
||||
// // result1 is Pair[string, string]{"AB", "12"}
|
||||
// // Both components: left-to-right
|
||||
//
|
||||
// // Applicative monoid
|
||||
// appMonoid := pair.ApplicativeMonoidTail(strConcat, strConcat)
|
||||
// result2 := appMonoid.Concat(p1, p2)
|
||||
// // result2 is Pair[string, string]{"BA", "12"}
|
||||
// // Head: reversed, Tail: normal
|
||||
//
|
||||
//go:inline
|
||||
func Monoid[L, R any](l M.Monoid[L], r M.Monoid[R]) M.Monoid[Pair[L, R]] {
|
||||
return M.MakeMonoid(
|
||||
func(pl, pr Pair[L, R]) Pair[L, R] {
|
||||
return MakePair(l.Concat(Head(pl), Head(pr)), r.Concat(Tail(pl), Tail(pr)))
|
||||
},
|
||||
MakePair(l.Empty(), r.Empty()),
|
||||
)
|
||||
}
|
||||
|
||||
// ApplicativeMonoid creates a monoid for [Pair] using applicative functor operations on the tail.
|
||||
//
|
||||
// This is an alias for [ApplicativeMonoidTail], which lifts the right (tail) monoid into the
|
||||
// Pair applicative functor. The left monoid provides the semigroup for combining head values
|
||||
// during applicative operations.
|
||||
//
|
||||
// IMPORTANT BEHAVIORAL NOTE: The applicative implementation causes the HEAD component to be
|
||||
// combined in REVERSE order (right-to-left) while the TAIL combines normally (left-to-right).
|
||||
// This differs from Haskell's standard Applicative instance for pairs, which combines the
|
||||
// first component left-to-right. This matters for non-commutative operations like string
|
||||
// concatenation.
|
||||
//
|
||||
// Parameters:
|
||||
// - l: A monoid for the head (left) values of type L
|
||||
// - r: A monoid for the tail (right) values of type R
|
||||
//
|
||||
// Returns:
|
||||
// - A Monoid[Pair[L, R]] that combines pairs using applicative operations on the tail
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// import (
|
||||
// N "github.com/IBM/fp-go/v2/number"
|
||||
// S "github.com/IBM/fp-go/v2/string"
|
||||
// )
|
||||
//
|
||||
// intAdd := N.MonoidSum[int]()
|
||||
// strConcat := S.Monoid
|
||||
//
|
||||
// pairMonoid := pair.ApplicativeMonoid(intAdd, strConcat)
|
||||
//
|
||||
// p1 := pair.MakePair(10, "foo")
|
||||
// p2 := pair.MakePair(20, "bar")
|
||||
//
|
||||
// result := pairMonoid.Concat(p1, p2)
|
||||
// // result is Pair[int, string]{30, "foobar"}
|
||||
// // Note: head combines normally (10+20), tail combines normally ("foo"+"bar")
|
||||
//
|
||||
// empty := pairMonoid.Empty()
|
||||
// // empty is Pair[int, string]{0, ""}
|
||||
//
|
||||
//go:inline
|
||||
func ApplicativeMonoid[L, R any](l M.Monoid[L], r M.Monoid[R]) M.Monoid[Pair[L, R]] {
|
||||
return ApplicativeMonoidTail(l, r)
|
||||
}
|
||||
|
||||
// ApplicativeMonoidTail creates a monoid for [Pair] by lifting the tail monoid into the applicative functor.
|
||||
//
|
||||
// This function constructs a monoid using the applicative structure of Pair, focusing on
|
||||
// the tail (right) value. The head values are combined using the left monoid's semigroup
|
||||
// operation during applicative application.
|
||||
//
|
||||
// CRITICAL BEHAVIORAL NOTE: The HEAD values are combined in REVERSE order (right-to-left),
|
||||
// while TAIL values combine in normal order (left-to-right). This is due to how the
|
||||
// applicative `ap` operation is implemented for Pair.
|
||||
//
|
||||
// NOTE: This differs from Haskell's standard Applicative instance for (,) which combines
|
||||
// the first component left-to-right. The reversal occurs because MonadApTail implements:
|
||||
//
|
||||
// MakePair(sg.Concat(second.head, first.head), ...)
|
||||
//
|
||||
// Example showing the reversal with non-commutative operations:
|
||||
//
|
||||
// strConcat := S.Monoid
|
||||
// pairMonoid := pair.ApplicativeMonoidTail(strConcat, strConcat)
|
||||
// p1 := pair.MakePair("hello", "foo")
|
||||
// p2 := pair.MakePair(" world", "bar")
|
||||
// result := pairMonoid.Concat(p1, p2)
|
||||
// // result is Pair[string, string]{" worldhello", "foobar"}
|
||||
// // ^^^^^^^^^^^^^^ ^^^^^^
|
||||
// // REVERSED! normal order
|
||||
//
|
||||
// In Haskell's Applicative for (,), this would give ("hellohello world", "foobar")
|
||||
//
|
||||
// The resulting monoid satisfies the standard monoid laws:
|
||||
// - Associativity: Concat(Concat(p1, p2), p3) = Concat(p1, Concat(p2, p3))
|
||||
// - Left identity: Concat(Empty(), p) = p
|
||||
// - Right identity: Concat(p, Empty()) = p
|
||||
//
|
||||
// Parameters:
|
||||
// - l: A monoid for the head (left) values of type L
|
||||
// - r: A monoid for the tail (right) values of type R
|
||||
//
|
||||
// Returns:
|
||||
// - A Monoid[Pair[L, R]] that combines pairs component-wise
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// import (
|
||||
// N "github.com/IBM/fp-go/v2/number"
|
||||
// M "github.com/IBM/fp-go/v2/monoid"
|
||||
// )
|
||||
//
|
||||
// intAdd := N.MonoidSum[int]()
|
||||
// intMul := N.MonoidProduct[int]()
|
||||
//
|
||||
// pairMonoid := pair.ApplicativeMonoidTail(intAdd, intMul)
|
||||
//
|
||||
// p1 := pair.MakePair(5, 3)
|
||||
// p2 := pair.MakePair(10, 4)
|
||||
//
|
||||
// result := pairMonoid.Concat(p1, p2)
|
||||
// // result is Pair[int, int]{15, 12} (5+10, 3*4)
|
||||
// // Note: Addition is commutative, so order doesn't matter for head
|
||||
//
|
||||
// empty := pairMonoid.Empty()
|
||||
// // empty is Pair[int, int]{0, 1}
|
||||
//
|
||||
// Example with different types:
|
||||
//
|
||||
// import S "github.com/IBM/fp-go/v2/string"
|
||||
//
|
||||
// boolAnd := M.MakeMonoid(func(a, b bool) bool { return a && b }, true)
|
||||
// strConcat := S.Monoid
|
||||
//
|
||||
// pairMonoid := pair.ApplicativeMonoidTail(boolAnd, strConcat)
|
||||
//
|
||||
// p1 := pair.MakePair(true, "hello")
|
||||
// p2 := pair.MakePair(true, " world")
|
||||
//
|
||||
// result := pairMonoid.Concat(p1, p2)
|
||||
// // result is Pair[bool, string]{true, "hello world"}
|
||||
// // Note: Boolean AND is commutative, so order doesn't matter for head
|
||||
//
|
||||
//go:inline
|
||||
func ApplicativeMonoidTail[L, R any](l M.Monoid[L], r M.Monoid[R]) M.Monoid[Pair[L, R]] {
|
||||
return M.ApplicativeMonoid(
|
||||
FromHead[R](l.Empty()),
|
||||
MonadMapTail[L, R, func(R) R],
|
||||
F.Bind1of3(MonadApTail[L, R, R])(l),
|
||||
r)
|
||||
}
|
||||
|
||||
// ApplicativeMonoidHead creates a monoid for [Pair] by lifting the head monoid into the applicative functor.
|
||||
//
|
||||
// This function constructs a monoid using the applicative structure of Pair, focusing on
|
||||
// the head (left) value. The tail values are combined using the right monoid's semigroup
|
||||
// operation during applicative application.
|
||||
//
|
||||
// This is the dual of [ApplicativeMonoidTail], operating on the head instead of the tail.
|
||||
//
|
||||
// CRITICAL BEHAVIORAL NOTE: The TAIL values are combined in REVERSE order (right-to-left),
|
||||
// while HEAD values combine in normal order (left-to-right). This is the opposite behavior
|
||||
// of ApplicativeMonoidTail. The reversal occurs because MonadApHead implements:
|
||||
//
|
||||
// MakePair(..., sg.Concat(second.tail, first.tail))
|
||||
//
|
||||
// Example showing the reversal with non-commutative operations:
|
||||
//
|
||||
// strConcat := S.Monoid
|
||||
// pairMonoid := pair.ApplicativeMonoidHead(strConcat, strConcat)
|
||||
// p1 := pair.MakePair("hello", "foo")
|
||||
// p2 := pair.MakePair(" world", "bar")
|
||||
// result := pairMonoid.Concat(p1, p2)
|
||||
// // result is Pair[string, string]{"hello world", "barfoo"}
|
||||
// // ^^^^^^^^^^^^ ^^^^^^^^
|
||||
// // normal order REVERSED!
|
||||
//
|
||||
// The resulting monoid satisfies the standard monoid laws:
|
||||
// - Associativity: Concat(Concat(p1, p2), p3) = Concat(p1, Concat(p2, p3))
|
||||
// - Left identity: Concat(Empty(), p) = p
|
||||
// - Right identity: Concat(p, Empty()) = p
|
||||
//
|
||||
// Parameters:
|
||||
// - l: A monoid for the head (left) values of type L
|
||||
// - r: A monoid for the tail (right) values of type R
|
||||
//
|
||||
// Returns:
|
||||
// - A Monoid[Pair[L, R]] that combines pairs component-wise
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// import (
|
||||
// N "github.com/IBM/fp-go/v2/number"
|
||||
// M "github.com/IBM/fp-go/v2/monoid"
|
||||
// )
|
||||
//
|
||||
// intMul := N.MonoidProduct[int]()
|
||||
// intAdd := N.MonoidSum[int]()
|
||||
//
|
||||
// pairMonoid := pair.ApplicativeMonoidHead(intMul, intAdd)
|
||||
//
|
||||
// p1 := pair.MakePair(3, 5)
|
||||
// p2 := pair.MakePair(4, 10)
|
||||
//
|
||||
// result := pairMonoid.Concat(p1, p2)
|
||||
// // result is Pair[int, int]{12, 15} (3*4, 5+10)
|
||||
// // Note: Both operations are commutative, so order doesn't matter
|
||||
//
|
||||
// empty := pairMonoid.Empty()
|
||||
// // empty is Pair[int, int]{1, 0}
|
||||
//
|
||||
// Example comparing Head vs Tail with non-commutative operations:
|
||||
//
|
||||
// import S "github.com/IBM/fp-go/v2/string"
|
||||
//
|
||||
// strConcat := S.Monoid
|
||||
//
|
||||
// // Using ApplicativeMonoidHead - tail values REVERSED
|
||||
// headMonoid := pair.ApplicativeMonoidHead(strConcat, strConcat)
|
||||
// p1 := pair.MakePair("hello", "foo")
|
||||
// p2 := pair.MakePair(" world", "bar")
|
||||
// result := headMonoid.Concat(p1, p2)
|
||||
// // result is Pair[string, string]{"hello world", "barfoo"}
|
||||
//
|
||||
// // Using ApplicativeMonoidTail - head values REVERSED
|
||||
// tailMonoid := pair.ApplicativeMonoidTail(strConcat, strConcat)
|
||||
// result2 := tailMonoid.Concat(p1, p2)
|
||||
// // result2 is Pair[string, string]{" worldhello", "foobar"}
|
||||
// // DIFFERENT result! Head and tail are swapped in their reversal behavior
|
||||
//
|
||||
//go:inline
|
||||
func ApplicativeMonoidHead[L, R any](l M.Monoid[L], r M.Monoid[R]) M.Monoid[Pair[L, R]] {
|
||||
return M.ApplicativeMonoid(
|
||||
FromTail[L](r.Empty()),
|
||||
MonadMapHead[R, L, func(L) L],
|
||||
F.Bind1of3(MonadApHead[R, L, L])(r),
|
||||
l)
|
||||
}
|
||||
756
v2/pair/monoid_test.go
Normal file
756
v2/pair/monoid_test.go
Normal file
@@ -0,0 +1,756 @@
|
||||
// Copyright (c) 2024 - 2025 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 (
|
||||
"testing"
|
||||
|
||||
M "github.com/IBM/fp-go/v2/monoid"
|
||||
N "github.com/IBM/fp-go/v2/number"
|
||||
S "github.com/IBM/fp-go/v2/string"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// TestApplicativeMonoidTail tests the ApplicativeMonoidTail implementation
|
||||
func TestApplicativeMonoidTail(t *testing.T) {
|
||||
t.Run("integer addition and string concatenation", func(t *testing.T) {
|
||||
intAdd := N.MonoidSum[int]()
|
||||
strConcat := S.Monoid
|
||||
|
||||
pairMonoid := ApplicativeMonoidTail(intAdd, strConcat)
|
||||
|
||||
p1 := MakePair(5, "hello")
|
||||
p2 := MakePair(3, " world")
|
||||
|
||||
result := pairMonoid.Concat(p1, p2)
|
||||
assert.Equal(t, 8, Head(result))
|
||||
assert.Equal(t, "hello world", Tail(result))
|
||||
})
|
||||
|
||||
t.Run("integer multiplication and addition", func(t *testing.T) {
|
||||
intMul := N.MonoidProduct[int]()
|
||||
intAdd := N.MonoidSum[int]()
|
||||
|
||||
pairMonoid := ApplicativeMonoidTail(intMul, intAdd)
|
||||
|
||||
p1 := MakePair(3, 5)
|
||||
p2 := MakePair(4, 10)
|
||||
|
||||
result := pairMonoid.Concat(p1, p2)
|
||||
assert.Equal(t, 12, Head(result)) // 3 * 4
|
||||
assert.Equal(t, 15, Tail(result)) // 5 + 10
|
||||
})
|
||||
|
||||
t.Run("boolean AND and OR", func(t *testing.T) {
|
||||
boolAnd := M.MakeMonoid(func(a, b bool) bool { return a && b }, true)
|
||||
boolOr := M.MakeMonoid(func(a, b bool) bool { return a || b }, false)
|
||||
|
||||
pairMonoid := ApplicativeMonoidTail(boolAnd, boolOr)
|
||||
|
||||
p1 := MakePair(true, false)
|
||||
p2 := MakePair(true, true)
|
||||
|
||||
result := pairMonoid.Concat(p1, p2)
|
||||
assert.Equal(t, true, Head(result)) // true && true
|
||||
assert.Equal(t, true, Tail(result)) // false || true
|
||||
})
|
||||
|
||||
t.Run("empty value", func(t *testing.T) {
|
||||
intAdd := N.MonoidSum[int]()
|
||||
strConcat := S.Monoid
|
||||
|
||||
pairMonoid := ApplicativeMonoidTail(intAdd, strConcat)
|
||||
|
||||
empty := pairMonoid.Empty()
|
||||
assert.Equal(t, 0, Head(empty))
|
||||
assert.Equal(t, "", Tail(empty))
|
||||
})
|
||||
|
||||
t.Run("left identity law", func(t *testing.T) {
|
||||
intAdd := N.MonoidSum[int]()
|
||||
strConcat := S.Monoid
|
||||
|
||||
pairMonoid := ApplicativeMonoidTail(intAdd, strConcat)
|
||||
|
||||
p := MakePair(5, "test")
|
||||
result := pairMonoid.Concat(pairMonoid.Empty(), p)
|
||||
|
||||
assert.Equal(t, p, result)
|
||||
})
|
||||
|
||||
t.Run("right identity law", func(t *testing.T) {
|
||||
intAdd := N.MonoidSum[int]()
|
||||
strConcat := S.Monoid
|
||||
|
||||
pairMonoid := ApplicativeMonoidTail(intAdd, strConcat)
|
||||
|
||||
p := MakePair(5, "test")
|
||||
result := pairMonoid.Concat(p, pairMonoid.Empty())
|
||||
|
||||
assert.Equal(t, p, result)
|
||||
})
|
||||
|
||||
t.Run("associativity law", func(t *testing.T) {
|
||||
intAdd := N.MonoidSum[int]()
|
||||
strConcat := S.Monoid
|
||||
|
||||
pairMonoid := ApplicativeMonoidTail(intAdd, strConcat)
|
||||
|
||||
p1 := MakePair(1, "a")
|
||||
p2 := MakePair(2, "b")
|
||||
p3 := MakePair(3, "c")
|
||||
|
||||
left := pairMonoid.Concat(pairMonoid.Concat(p1, p2), p3)
|
||||
right := pairMonoid.Concat(p1, pairMonoid.Concat(p2, p3))
|
||||
|
||||
assert.Equal(t, left, right)
|
||||
assert.Equal(t, 6, Head(left))
|
||||
assert.Equal(t, "abc", Tail(left))
|
||||
})
|
||||
|
||||
t.Run("multiple concatenations", func(t *testing.T) {
|
||||
intAdd := N.MonoidSum[int]()
|
||||
intMul := N.MonoidProduct[int]()
|
||||
|
||||
pairMonoid := ApplicativeMonoidTail(intAdd, intMul)
|
||||
|
||||
pairs := []Pair[int, int]{
|
||||
MakePair(1, 2),
|
||||
MakePair(3, 4),
|
||||
MakePair(5, 6),
|
||||
}
|
||||
|
||||
result := pairMonoid.Empty()
|
||||
for _, p := range pairs {
|
||||
result = pairMonoid.Concat(result, p)
|
||||
}
|
||||
|
||||
assert.Equal(t, 9, Head(result)) // 0 + 1 + 3 + 5
|
||||
assert.Equal(t, 48, Tail(result)) // 1 * 2 * 4 * 6
|
||||
})
|
||||
}
|
||||
|
||||
// TestApplicativeMonoidHead tests the ApplicativeMonoidHead implementation
|
||||
func TestApplicativeMonoidHead(t *testing.T) {
|
||||
t.Run("integer multiplication and addition", func(t *testing.T) {
|
||||
intMul := N.MonoidProduct[int]()
|
||||
intAdd := N.MonoidSum[int]()
|
||||
|
||||
pairMonoid := ApplicativeMonoidHead(intMul, intAdd)
|
||||
|
||||
p1 := MakePair(3, 5)
|
||||
p2 := MakePair(4, 10)
|
||||
|
||||
result := pairMonoid.Concat(p1, p2)
|
||||
assert.Equal(t, 12, Head(result)) // 3 * 4
|
||||
assert.Equal(t, 15, Tail(result)) // 5 + 10
|
||||
})
|
||||
|
||||
t.Run("string concatenation and boolean OR", func(t *testing.T) {
|
||||
strConcat := S.Monoid
|
||||
boolOr := M.MakeMonoid(func(a, b bool) bool { return a || b }, false)
|
||||
|
||||
pairMonoid := ApplicativeMonoidHead(strConcat, boolOr)
|
||||
|
||||
p1 := MakePair("hello", false)
|
||||
p2 := MakePair(" world", true)
|
||||
|
||||
result := pairMonoid.Concat(p1, p2)
|
||||
assert.Equal(t, "hello world", Head(result))
|
||||
assert.Equal(t, true, Tail(result))
|
||||
})
|
||||
|
||||
t.Run("empty value", func(t *testing.T) {
|
||||
intMul := N.MonoidProduct[int]()
|
||||
intAdd := N.MonoidSum[int]()
|
||||
|
||||
pairMonoid := ApplicativeMonoidHead(intMul, intAdd)
|
||||
|
||||
empty := pairMonoid.Empty()
|
||||
assert.Equal(t, 1, Head(empty))
|
||||
assert.Equal(t, 0, Tail(empty))
|
||||
})
|
||||
|
||||
t.Run("left identity law", func(t *testing.T) {
|
||||
intMul := N.MonoidProduct[int]()
|
||||
intAdd := N.MonoidSum[int]()
|
||||
|
||||
pairMonoid := ApplicativeMonoidHead(intMul, intAdd)
|
||||
|
||||
p := MakePair(5, 10)
|
||||
result := pairMonoid.Concat(pairMonoid.Empty(), p)
|
||||
|
||||
assert.Equal(t, p, result)
|
||||
})
|
||||
|
||||
t.Run("right identity law", func(t *testing.T) {
|
||||
intMul := N.MonoidProduct[int]()
|
||||
intAdd := N.MonoidSum[int]()
|
||||
|
||||
pairMonoid := ApplicativeMonoidHead(intMul, intAdd)
|
||||
|
||||
p := MakePair(5, 10)
|
||||
result := pairMonoid.Concat(p, pairMonoid.Empty())
|
||||
|
||||
assert.Equal(t, p, result)
|
||||
})
|
||||
|
||||
t.Run("associativity law", func(t *testing.T) {
|
||||
intMul := N.MonoidProduct[int]()
|
||||
intAdd := N.MonoidSum[int]()
|
||||
|
||||
pairMonoid := ApplicativeMonoidHead(intMul, intAdd)
|
||||
|
||||
p1 := MakePair(2, 1)
|
||||
p2 := MakePair(3, 2)
|
||||
p3 := MakePair(4, 3)
|
||||
|
||||
left := pairMonoid.Concat(pairMonoid.Concat(p1, p2), p3)
|
||||
right := pairMonoid.Concat(p1, pairMonoid.Concat(p2, p3))
|
||||
|
||||
assert.Equal(t, left, right)
|
||||
assert.Equal(t, 24, Head(left)) // 2 * 3 * 4
|
||||
assert.Equal(t, 6, Tail(left)) // 1 + 2 + 3
|
||||
})
|
||||
|
||||
t.Run("multiple concatenations", func(t *testing.T) {
|
||||
intAdd := N.MonoidSum[int]()
|
||||
intMul := N.MonoidProduct[int]()
|
||||
|
||||
pairMonoid := ApplicativeMonoidHead(intAdd, intMul)
|
||||
|
||||
pairs := []Pair[int, int]{
|
||||
MakePair(1, 2),
|
||||
MakePair(3, 4),
|
||||
MakePair(5, 6),
|
||||
}
|
||||
|
||||
result := pairMonoid.Empty()
|
||||
for _, p := range pairs {
|
||||
result = pairMonoid.Concat(result, p)
|
||||
}
|
||||
|
||||
assert.Equal(t, 9, Head(result)) // 0 + 1 + 3 + 5
|
||||
assert.Equal(t, 48, Tail(result)) // 1 * 2 * 4 * 6
|
||||
})
|
||||
}
|
||||
|
||||
// TestApplicativeMonoid tests the ApplicativeMonoid alias
|
||||
func TestApplicativeMonoid(t *testing.T) {
|
||||
t.Run("is alias for ApplicativeMonoidTail", func(t *testing.T) {
|
||||
intAdd := N.MonoidSum[int]()
|
||||
strConcat := S.Monoid
|
||||
|
||||
monoid1 := ApplicativeMonoid(intAdd, strConcat)
|
||||
monoid2 := ApplicativeMonoidTail(intAdd, strConcat)
|
||||
|
||||
p1 := MakePair(5, "hello")
|
||||
p2 := MakePair(3, " world")
|
||||
|
||||
result1 := monoid1.Concat(p1, p2)
|
||||
result2 := monoid2.Concat(p1, p2)
|
||||
|
||||
assert.Equal(t, result1, result2)
|
||||
assert.Equal(t, 8, Head(result1))
|
||||
assert.Equal(t, "hello world", Tail(result1))
|
||||
})
|
||||
|
||||
t.Run("empty values are identical", func(t *testing.T) {
|
||||
intAdd := N.MonoidSum[int]()
|
||||
strConcat := S.Monoid
|
||||
|
||||
monoid1 := ApplicativeMonoid(intAdd, strConcat)
|
||||
monoid2 := ApplicativeMonoidTail(intAdd, strConcat)
|
||||
|
||||
assert.Equal(t, monoid1.Empty(), monoid2.Empty())
|
||||
})
|
||||
}
|
||||
|
||||
// TestMonoidHeadVsTail compares ApplicativeMonoidHead and ApplicativeMonoidTail
|
||||
func TestMonoidHeadVsTail(t *testing.T) {
|
||||
t.Run("same result with commutative operations", func(t *testing.T) {
|
||||
intAdd := N.MonoidSum[int]()
|
||||
intMul := N.MonoidProduct[int]()
|
||||
|
||||
headMonoid := ApplicativeMonoidHead(intMul, intAdd)
|
||||
tailMonoid := ApplicativeMonoidTail(intMul, intAdd)
|
||||
|
||||
p1 := MakePair(2, 3)
|
||||
p2 := MakePair(4, 5)
|
||||
|
||||
resultHead := headMonoid.Concat(p1, p2)
|
||||
resultTail := tailMonoid.Concat(p1, p2)
|
||||
|
||||
// Both should give same result since operations are commutative
|
||||
assert.Equal(t, 8, Head(resultHead)) // 2 * 4
|
||||
assert.Equal(t, 8, Tail(resultHead)) // 3 + 5
|
||||
assert.Equal(t, 8, Head(resultTail)) // 2 * 4
|
||||
assert.Equal(t, 8, Tail(resultTail)) // 3 + 5
|
||||
})
|
||||
|
||||
t.Run("different empty values", func(t *testing.T) {
|
||||
intAdd := N.MonoidSum[int]()
|
||||
intMul := N.MonoidProduct[int]()
|
||||
|
||||
headMonoid := ApplicativeMonoidHead(intMul, intAdd)
|
||||
tailMonoid := ApplicativeMonoidTail(intAdd, intMul)
|
||||
|
||||
emptyHead := headMonoid.Empty()
|
||||
emptyTail := tailMonoid.Empty()
|
||||
|
||||
assert.Equal(t, 1, Head(emptyHead)) // intMul empty
|
||||
assert.Equal(t, 0, Tail(emptyHead)) // intAdd empty
|
||||
assert.Equal(t, 0, Head(emptyTail)) // intAdd empty
|
||||
assert.Equal(t, 1, Tail(emptyTail)) // intMul empty
|
||||
})
|
||||
}
|
||||
|
||||
// TestMonoidLaws verifies monoid laws for all implementations
|
||||
func TestMonoidLaws(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
monoid M.Monoid[Pair[int, int]]
|
||||
p1, p2, p3 Pair[int, int]
|
||||
}{
|
||||
{
|
||||
name: "ApplicativeMonoidTail",
|
||||
monoid: ApplicativeMonoidTail(N.MonoidSum[int](), N.MonoidProduct[int]()),
|
||||
p1: MakePair(1, 2),
|
||||
p2: MakePair(3, 4),
|
||||
p3: MakePair(5, 6),
|
||||
},
|
||||
{
|
||||
name: "ApplicativeMonoidHead",
|
||||
monoid: ApplicativeMonoidHead(N.MonoidProduct[int](), N.MonoidSum[int]()),
|
||||
p1: MakePair(2, 1),
|
||||
p2: MakePair(3, 2),
|
||||
p3: MakePair(4, 3),
|
||||
},
|
||||
{
|
||||
name: "ApplicativeMonoid",
|
||||
monoid: ApplicativeMonoid(N.MonoidSum[int](), N.MonoidSum[int]()),
|
||||
p1: MakePair(1, 2),
|
||||
p2: MakePair(3, 4),
|
||||
p3: MakePair(5, 6),
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
t.Run("associativity", func(t *testing.T) {
|
||||
left := tc.monoid.Concat(tc.monoid.Concat(tc.p1, tc.p2), tc.p3)
|
||||
right := tc.monoid.Concat(tc.p1, tc.monoid.Concat(tc.p2, tc.p3))
|
||||
assert.Equal(t, left, right)
|
||||
})
|
||||
|
||||
t.Run("left identity", func(t *testing.T) {
|
||||
result := tc.monoid.Concat(tc.monoid.Empty(), tc.p1)
|
||||
assert.Equal(t, tc.p1, result)
|
||||
})
|
||||
|
||||
t.Run("right identity", func(t *testing.T) {
|
||||
result := tc.monoid.Concat(tc.p1, tc.monoid.Empty())
|
||||
assert.Equal(t, tc.p1, result)
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestMonoidEdgeCases tests edge cases for monoid operations
|
||||
func TestMonoidEdgeCases(t *testing.T) {
|
||||
t.Run("concatenating empty with empty", func(t *testing.T) {
|
||||
intAdd := N.MonoidSum[int]()
|
||||
strConcat := S.Monoid
|
||||
|
||||
pairMonoid := ApplicativeMonoidTail(intAdd, strConcat)
|
||||
|
||||
result := pairMonoid.Concat(pairMonoid.Empty(), pairMonoid.Empty())
|
||||
assert.Equal(t, pairMonoid.Empty(), result)
|
||||
})
|
||||
|
||||
t.Run("chain of operations", func(t *testing.T) {
|
||||
intAdd := N.MonoidSum[int]()
|
||||
intMul := N.MonoidProduct[int]()
|
||||
|
||||
pairMonoid := ApplicativeMonoidTail(intAdd, intMul)
|
||||
|
||||
result := pairMonoid.Concat(
|
||||
pairMonoid.Concat(
|
||||
pairMonoid.Concat(MakePair(1, 2), MakePair(2, 3)),
|
||||
MakePair(3, 4),
|
||||
),
|
||||
MakePair(4, 5),
|
||||
)
|
||||
|
||||
assert.Equal(t, 10, Head(result)) // 1 + 2 + 3 + 4
|
||||
assert.Equal(t, 120, Tail(result)) // 2 * 3 * 4 * 5
|
||||
})
|
||||
|
||||
t.Run("zero values", func(t *testing.T) {
|
||||
intAdd := N.MonoidSum[int]()
|
||||
intMul := N.MonoidProduct[int]()
|
||||
|
||||
pairMonoid := ApplicativeMonoidTail(intAdd, intMul)
|
||||
|
||||
p1 := MakePair(0, 0)
|
||||
p2 := MakePair(5, 10)
|
||||
|
||||
result := pairMonoid.Concat(p1, p2)
|
||||
assert.Equal(t, 5, Head(result))
|
||||
assert.Equal(t, 0, Tail(result)) // 0 * 10 = 0
|
||||
})
|
||||
|
||||
t.Run("negative values", func(t *testing.T) {
|
||||
intAdd := N.MonoidSum[int]()
|
||||
intMul := N.MonoidProduct[int]()
|
||||
|
||||
pairMonoid := ApplicativeMonoidTail(intAdd, intMul)
|
||||
|
||||
p1 := MakePair(-5, -2)
|
||||
p2 := MakePair(3, 4)
|
||||
|
||||
result := pairMonoid.Concat(p1, p2)
|
||||
assert.Equal(t, -2, Head(result)) // -5 + 3
|
||||
assert.Equal(t, -8, Tail(result)) // -2 * 4
|
||||
})
|
||||
}
|
||||
|
||||
// TestMonoidWithDifferentTypes tests monoids with various type combinations
|
||||
func TestMonoidWithDifferentTypes(t *testing.T) {
|
||||
t.Run("string and boolean", func(t *testing.T) {
|
||||
strConcat := S.Monoid
|
||||
boolAnd := M.MakeMonoid(func(a, b bool) bool { return a && b }, true)
|
||||
|
||||
pairMonoid := ApplicativeMonoidTail(strConcat, boolAnd)
|
||||
|
||||
p1 := MakePair("hello", true)
|
||||
p2 := MakePair(" world", true)
|
||||
|
||||
result := pairMonoid.Concat(p1, p2)
|
||||
// Note: The order depends on the applicative implementation
|
||||
assert.Equal(t, " worldhello", Head(result))
|
||||
assert.Equal(t, true, Tail(result))
|
||||
})
|
||||
|
||||
t.Run("boolean and string", func(t *testing.T) {
|
||||
boolOr := M.MakeMonoid(func(a, b bool) bool { return a || b }, false)
|
||||
strConcat := S.Monoid
|
||||
|
||||
pairMonoid := ApplicativeMonoidTail(boolOr, strConcat)
|
||||
|
||||
p1 := MakePair(false, "foo")
|
||||
p2 := MakePair(true, "bar")
|
||||
|
||||
result := pairMonoid.Concat(p1, p2)
|
||||
assert.Equal(t, true, Head(result))
|
||||
assert.Equal(t, "foobar", Tail(result))
|
||||
})
|
||||
|
||||
t.Run("float64 addition and multiplication", func(t *testing.T) {
|
||||
floatAdd := N.MonoidSum[float64]()
|
||||
floatMul := N.MonoidProduct[float64]()
|
||||
|
||||
pairMonoid := ApplicativeMonoidTail(floatAdd, floatMul)
|
||||
|
||||
p1 := MakePair(1.5, 2.0)
|
||||
p2 := MakePair(2.5, 3.0)
|
||||
|
||||
result := pairMonoid.Concat(p1, p2)
|
||||
assert.Equal(t, 4.0, Head(result))
|
||||
assert.Equal(t, 6.0, Tail(result))
|
||||
})
|
||||
}
|
||||
|
||||
// TestMonoidCommutativity tests behavior with non-commutative operations
|
||||
func TestMonoidCommutativity(t *testing.T) {
|
||||
t.Run("string concatenation is not commutative", func(t *testing.T) {
|
||||
strConcat := S.Monoid
|
||||
|
||||
pairMonoid := ApplicativeMonoidTail(strConcat, strConcat)
|
||||
|
||||
p1 := MakePair("hello", "foo")
|
||||
p2 := MakePair(" world", "bar")
|
||||
|
||||
result1 := pairMonoid.Concat(p1, p2)
|
||||
result2 := pairMonoid.Concat(p2, p1)
|
||||
|
||||
// The applicative implementation reverses the order for head values
|
||||
assert.Equal(t, " worldhello", Head(result1))
|
||||
assert.Equal(t, "foobar", Tail(result1))
|
||||
assert.Equal(t, "hello world", Head(result2))
|
||||
assert.Equal(t, "barfoo", Tail(result2))
|
||||
assert.NotEqual(t, result1, result2)
|
||||
})
|
||||
}
|
||||
|
||||
// TestSimpleMonoid tests the basic Monoid function (non-applicative)
|
||||
func TestSimpleMonoid(t *testing.T) {
|
||||
t.Run("combines both components left-to-right", func(t *testing.T) {
|
||||
strConcat := S.Monoid
|
||||
|
||||
pairMonoid := Monoid(strConcat, strConcat)
|
||||
|
||||
p1 := MakePair("hello", "foo")
|
||||
p2 := MakePair(" world", "bar")
|
||||
|
||||
result := pairMonoid.Concat(p1, p2)
|
||||
|
||||
// Both components combine in normal left-to-right order
|
||||
assert.Equal(t, "hello world", Head(result))
|
||||
assert.Equal(t, "foobar", Tail(result))
|
||||
})
|
||||
|
||||
t.Run("empty value", func(t *testing.T) {
|
||||
intAdd := N.MonoidSum[int]()
|
||||
strConcat := S.Monoid
|
||||
|
||||
pairMonoid := Monoid(intAdd, strConcat)
|
||||
|
||||
empty := pairMonoid.Empty()
|
||||
assert.Equal(t, 0, Head(empty))
|
||||
assert.Equal(t, "", Tail(empty))
|
||||
})
|
||||
|
||||
t.Run("monoid laws", func(t *testing.T) {
|
||||
intAdd := N.MonoidSum[int]()
|
||||
intMul := N.MonoidProduct[int]()
|
||||
|
||||
pairMonoid := Monoid(intAdd, intMul)
|
||||
|
||||
p1 := MakePair(1, 2)
|
||||
p2 := MakePair(3, 4)
|
||||
p3 := MakePair(5, 6)
|
||||
|
||||
// Associativity
|
||||
left := pairMonoid.Concat(pairMonoid.Concat(p1, p2), p3)
|
||||
right := pairMonoid.Concat(p1, pairMonoid.Concat(p2, p3))
|
||||
assert.Equal(t, left, right)
|
||||
|
||||
// Left identity
|
||||
assert.Equal(t, p1, pairMonoid.Concat(pairMonoid.Empty(), p1))
|
||||
|
||||
// Right identity
|
||||
assert.Equal(t, p1, pairMonoid.Concat(p1, pairMonoid.Empty()))
|
||||
})
|
||||
}
|
||||
|
||||
// TestMonoidComparison compares the simple Monoid with applicative versions
|
||||
func TestMonoidComparison(t *testing.T) {
|
||||
t.Run("Monoid vs ApplicativeMonoidTail with strings", func(t *testing.T) {
|
||||
strConcat := S.Monoid
|
||||
|
||||
simpleMonoid := Monoid(strConcat, strConcat)
|
||||
appMonoid := ApplicativeMonoidTail(strConcat, strConcat)
|
||||
|
||||
p1 := MakePair("A", "1")
|
||||
p2 := MakePair("B", "2")
|
||||
|
||||
simpleResult := simpleMonoid.Concat(p1, p2)
|
||||
appResult := appMonoid.Concat(p1, p2)
|
||||
|
||||
// Simple monoid: both components left-to-right
|
||||
assert.Equal(t, "AB", Head(simpleResult))
|
||||
assert.Equal(t, "12", Tail(simpleResult))
|
||||
|
||||
// Applicative monoid: head reversed, tail normal
|
||||
assert.Equal(t, "BA", Head(appResult))
|
||||
assert.Equal(t, "12", Tail(appResult))
|
||||
|
||||
// They produce different results!
|
||||
assert.NotEqual(t, simpleResult, appResult)
|
||||
})
|
||||
|
||||
t.Run("Monoid vs ApplicativeMonoidHead with strings", func(t *testing.T) {
|
||||
strConcat := S.Monoid
|
||||
|
||||
simpleMonoid := Monoid(strConcat, strConcat)
|
||||
appMonoid := ApplicativeMonoidHead(strConcat, strConcat)
|
||||
|
||||
p1 := MakePair("A", "1")
|
||||
p2 := MakePair("B", "2")
|
||||
|
||||
simpleResult := simpleMonoid.Concat(p1, p2)
|
||||
appResult := appMonoid.Concat(p1, p2)
|
||||
|
||||
// Simple monoid: both components left-to-right
|
||||
assert.Equal(t, "AB", Head(simpleResult))
|
||||
assert.Equal(t, "12", Tail(simpleResult))
|
||||
|
||||
// Applicative monoid: head normal, tail reversed
|
||||
assert.Equal(t, "AB", Head(appResult))
|
||||
assert.Equal(t, "21", Tail(appResult))
|
||||
|
||||
// They produce different results!
|
||||
assert.NotEqual(t, simpleResult, appResult)
|
||||
})
|
||||
|
||||
t.Run("all three produce same result with commutative operations", func(t *testing.T) {
|
||||
intAdd := N.MonoidSum[int]()
|
||||
intMul := N.MonoidProduct[int]()
|
||||
|
||||
simpleMonoid := Monoid(intAdd, intMul)
|
||||
appTailMonoid := ApplicativeMonoidTail(intAdd, intMul)
|
||||
appHeadMonoid := ApplicativeMonoidHead(intAdd, intMul)
|
||||
|
||||
p1 := MakePair(2, 3)
|
||||
p2 := MakePair(4, 5)
|
||||
|
||||
simpleResult := simpleMonoid.Concat(p1, p2)
|
||||
appTailResult := appTailMonoid.Concat(p1, p2)
|
||||
appHeadResult := appHeadMonoid.Concat(p1, p2)
|
||||
|
||||
// All produce the same result with commutative operations
|
||||
// Simple: (2+4, 3*5) = (6, 15)
|
||||
// AppTail: (4+2, 3*5) = (6, 15) - addition is commutative
|
||||
// AppHead: (2+4, 5*3) = (6, 15) - multiplication is commutative
|
||||
assert.Equal(t, 6, Head(simpleResult))
|
||||
assert.Equal(t, 15, Tail(simpleResult))
|
||||
assert.Equal(t, simpleResult, appTailResult)
|
||||
assert.Equal(t, simpleResult, appHeadResult)
|
||||
})
|
||||
|
||||
t.Run("Monoid matches Haskell behavior", func(t *testing.T) {
|
||||
strConcat := S.Monoid
|
||||
|
||||
pairMonoid := Monoid(strConcat, strConcat)
|
||||
|
||||
p1 := MakePair("hello", "foo")
|
||||
p2 := MakePair(" world", "bar")
|
||||
|
||||
result := pairMonoid.Concat(p1, p2)
|
||||
|
||||
// This matches what you'd expect from simple tuple combination
|
||||
// and is closer to intuitive behavior
|
||||
assert.Equal(t, "hello world", Head(result))
|
||||
assert.Equal(t, "foobar", Tail(result))
|
||||
})
|
||||
}
|
||||
|
||||
// TestMonoidHaskellComparison documents how this implementation differs from Haskell's
|
||||
// standard Applicative instance for pairs (tuples).
|
||||
func TestMonoidHaskellComparison(t *testing.T) {
|
||||
t.Run("ApplicativeMonoidTail reverses head order unlike Haskell", func(t *testing.T) {
|
||||
strConcat := S.Monoid
|
||||
|
||||
pairMonoid := ApplicativeMonoidTail(strConcat, strConcat)
|
||||
|
||||
p1 := MakePair("hello", "foo")
|
||||
p2 := MakePair(" world", "bar")
|
||||
|
||||
result := pairMonoid.Concat(p1, p2)
|
||||
|
||||
// Go implementation: head is reversed, tail is normal
|
||||
assert.Equal(t, " worldhello", Head(result))
|
||||
assert.Equal(t, "foobar", Tail(result))
|
||||
|
||||
// In Haskell's Applicative for (,):
|
||||
// (u, f) <*> (v, x) = (u <> v, f x)
|
||||
// pure (<>) <*> ("hello", "foo") <*> (" world", "bar")
|
||||
// would give: ("hello world", "foobar")
|
||||
// Note: Haskell combines first component left-to-right, not reversed
|
||||
})
|
||||
|
||||
t.Run("ApplicativeMonoidHead reverses tail order", func(t *testing.T) {
|
||||
strConcat := S.Monoid
|
||||
|
||||
pairMonoid := ApplicativeMonoidHead(strConcat, strConcat)
|
||||
|
||||
p1 := MakePair("hello", "foo")
|
||||
p2 := MakePair(" world", "bar")
|
||||
|
||||
result := pairMonoid.Concat(p1, p2)
|
||||
|
||||
// Go implementation: head is normal, tail is reversed
|
||||
assert.Equal(t, "hello world", Head(result))
|
||||
assert.Equal(t, "barfoo", Tail(result))
|
||||
|
||||
// This is the dual operation, focusing on head instead of tail
|
||||
})
|
||||
|
||||
t.Run("behavior with commutative operations matches Haskell", func(t *testing.T) {
|
||||
intAdd := N.MonoidSum[int]()
|
||||
intMul := N.MonoidProduct[int]()
|
||||
|
||||
pairMonoid := ApplicativeMonoidTail(intAdd, intMul)
|
||||
|
||||
p1 := MakePair(5, 3)
|
||||
p2 := MakePair(10, 4)
|
||||
|
||||
result := pairMonoid.Concat(p1, p2)
|
||||
|
||||
// With commutative operations, order doesn't matter
|
||||
// Both Go and Haskell give the same result
|
||||
assert.Equal(t, 15, Head(result)) // 5 + 10 = 10 + 5
|
||||
assert.Equal(t, 12, Tail(result)) // 3 * 4 = 4 * 3
|
||||
})
|
||||
}
|
||||
|
||||
// TestMonoidOrderingDocumentation provides clear examples of the ordering behavior
|
||||
// for documentation purposes.
|
||||
func TestMonoidOrderingDocumentation(t *testing.T) {
|
||||
t.Run("ApplicativeMonoidTail ordering", func(t *testing.T) {
|
||||
strConcat := S.Monoid
|
||||
pairMonoid := ApplicativeMonoidTail(strConcat, strConcat)
|
||||
|
||||
p1 := MakePair("A", "1")
|
||||
p2 := MakePair("B", "2")
|
||||
p3 := MakePair("C", "3")
|
||||
|
||||
// Concat p1 and p2
|
||||
r12 := pairMonoid.Concat(p1, p2)
|
||||
assert.Equal(t, "BA", Head(r12)) // Head: reversed (p2 + p1)
|
||||
assert.Equal(t, "12", Tail(r12)) // Tail: normal (p1 + p2)
|
||||
|
||||
// Concat all three
|
||||
r123 := pairMonoid.Concat(r12, p3)
|
||||
assert.Equal(t, "CBA", Head(r123)) // Head: reversed (p3 + p2 + p1)
|
||||
assert.Equal(t, "123", Tail(r123)) // Tail: normal (p1 + p2 + p3)
|
||||
})
|
||||
|
||||
t.Run("ApplicativeMonoidHead ordering", func(t *testing.T) {
|
||||
strConcat := S.Monoid
|
||||
pairMonoid := ApplicativeMonoidHead(strConcat, strConcat)
|
||||
|
||||
p1 := MakePair("A", "1")
|
||||
p2 := MakePair("B", "2")
|
||||
p3 := MakePair("C", "3")
|
||||
|
||||
// Concat p1 and p2
|
||||
r12 := pairMonoid.Concat(p1, p2)
|
||||
assert.Equal(t, "AB", Head(r12)) // Head: normal (p1 + p2)
|
||||
assert.Equal(t, "21", Tail(r12)) // Tail: reversed (p2 + p1)
|
||||
|
||||
// Concat all three
|
||||
r123 := pairMonoid.Concat(r12, p3)
|
||||
assert.Equal(t, "ABC", Head(r123)) // Head: normal (p1 + p2 + p3)
|
||||
assert.Equal(t, "321", Tail(r123)) // Tail: reversed (p3 + p2 + p1)
|
||||
})
|
||||
|
||||
t.Run("empty values respect ordering", func(t *testing.T) {
|
||||
strConcat := S.Monoid
|
||||
pairMonoid := ApplicativeMonoidTail(strConcat, strConcat)
|
||||
|
||||
empty := pairMonoid.Empty()
|
||||
p := MakePair("X", "Y")
|
||||
|
||||
// Empty is identity regardless of order
|
||||
r1 := pairMonoid.Concat(empty, p)
|
||||
r2 := pairMonoid.Concat(p, empty)
|
||||
|
||||
assert.Equal(t, p, r1)
|
||||
assert.Equal(t, p, r2)
|
||||
})
|
||||
}
|
||||
@@ -189,7 +189,7 @@ func MonadMapTail[A, B, B1 any](fa Pair[A, B], f func(B) B1) Pair[A, B1] {
|
||||
// p := pair.MakePair(5, "hello")
|
||||
// p2 := pair.MonadBiMap(p,
|
||||
// func(n int) string { return fmt.Sprintf("%d", n) },
|
||||
// func(s string) int { return len(s) },
|
||||
// S.Size,
|
||||
// ) // Pair[string, int]{"5", 5}
|
||||
//
|
||||
//go:inline
|
||||
@@ -202,7 +202,7 @@ func MonadBiMap[A, B, A1, B1 any](fa Pair[A, B], f func(A) A1, g func(B) B1) Pai
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// mapper := pair.Map[int](func(s string) int { return len(s) })
|
||||
// mapper := pair.Map[int](S.Size)
|
||||
// p := pair.MakePair(5, "hello")
|
||||
// p2 := mapper(p) // Pair[int, int]{5, 5}
|
||||
//
|
||||
@@ -232,7 +232,7 @@ func MapHead[B, A, A1 any](f func(A) A1) func(Pair[A, B]) Pair[A1, B] {
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// mapper := pair.MapTail[int](func(s string) int { return len(s) })
|
||||
// mapper := pair.MapTail[int](S.Size)
|
||||
// p := pair.MakePair(5, "hello")
|
||||
// p2 := mapper(p) // Pair[int, int]{5, 5}
|
||||
//
|
||||
@@ -248,7 +248,7 @@ func MapTail[A, B, B1 any](f func(B) B1) Operator[A, B, B1] {
|
||||
//
|
||||
// mapper := pair.BiMap(
|
||||
// func(n int) string { return fmt.Sprintf("%d", n) },
|
||||
// func(s string) int { return len(s) },
|
||||
// S.Size,
|
||||
// )
|
||||
// p := pair.MakePair(5, "hello")
|
||||
// p2 := mapper(p) // Pair[string, int]{"5", 5}
|
||||
@@ -400,7 +400,7 @@ func MonadApHead[B, A, A1 any](sg Semigroup[B], faa Pair[func(A) A1, B], fa Pair
|
||||
// import N "github.com/IBM/fp-go/v2/number"
|
||||
//
|
||||
// intSum := N.SemigroupSum[int]()
|
||||
// pf := pair.MakePair(10, func(s string) int { return len(s) })
|
||||
// pf := pair.MakePair(10, S.Size)
|
||||
// pv := pair.MakePair(5, "hello")
|
||||
// result := pair.MonadApTail(intSum, pf, pv) // Pair[int, int]{15, 5}
|
||||
//
|
||||
@@ -417,7 +417,7 @@ func MonadApTail[A, B, B1 any](sg Semigroup[A], fbb Pair[A, func(B) B1], fb Pair
|
||||
// import N "github.com/IBM/fp-go/v2/number"
|
||||
//
|
||||
// intSum := N.SemigroupSum[int]()
|
||||
// pf := pair.MakePair(10, func(s string) int { return len(s) })
|
||||
// pf := pair.MakePair(10, S.Size)
|
||||
// pv := pair.MakePair(5, "hello")
|
||||
// result := pair.MonadAp(intSum, pf, pv) // Pair[int, int]{15, 5}
|
||||
//
|
||||
@@ -454,7 +454,7 @@ func ApHead[B, A, A1 any](sg Semigroup[B], fa Pair[A, B]) func(Pair[func(A) A1,
|
||||
// intSum := N.SemigroupSum[int]()
|
||||
// pv := pair.MakePair(5, "hello")
|
||||
// ap := pair.ApTail(intSum, pv)
|
||||
// pf := pair.MakePair(10, func(s string) int { return len(s) })
|
||||
// pf := pair.MakePair(10, S.Size)
|
||||
// result := ap(pf) // Pair[int, int]{15, 5}
|
||||
func ApTail[A, B, B1 any](sg Semigroup[A], fb Pair[A, B]) Operator[A, func(B) B1, B1] {
|
||||
return func(fbb Pair[A, func(B) B1]) Pair[A, B1] {
|
||||
@@ -472,7 +472,7 @@ func ApTail[A, B, B1 any](sg Semigroup[A], fb Pair[A, B]) Operator[A, func(B) B1
|
||||
// intSum := N.SemigroupSum[int]()
|
||||
// pv := pair.MakePair(5, "hello")
|
||||
// ap := pair.Ap(intSum, pv)
|
||||
// pf := pair.MakePair(10, func(s string) int { return len(s) })
|
||||
// pf := pair.MakePair(10, S.Size)
|
||||
// result := ap(pf) // Pair[int, int]{15, 5}
|
||||
//
|
||||
//go:inline
|
||||
|
||||
78
v2/reader/profunctor.go
Normal file
78
v2/reader/profunctor.go
Normal file
@@ -0,0 +1,78 @@
|
||||
// Copyright (c) 2025 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 reader
|
||||
|
||||
import "github.com/IBM/fp-go/v2/function"
|
||||
|
||||
// Promap is the profunctor map operation that transforms both the input and output of a Reader.
|
||||
// It applies f to the input (contravariantly) and g to the output (covariantly).
|
||||
//
|
||||
// See: https://github.com/fantasyland/fantasy-land?tab=readme-ov-file#profunctor
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// type Config struct { Port int }
|
||||
// type Env struct { Config Config }
|
||||
// getPort := func(c Config) int { return c.Port }
|
||||
// extractConfig := func(e Env) Config { return e.Config }
|
||||
// toString := strconv.Itoa
|
||||
// r := reader.Promap(extractConfig, toString)(getPort)
|
||||
// result := r(Env{Config: Config{Port: 8080}}) // "8080"
|
||||
func Promap[E, A, D, B any](f func(D) E, g func(A) B) Kleisli[D, Reader[E, A], B] {
|
||||
return function.Bind13of3(function.Flow3[func(D) E, func(E) A, func(A) B])(f, g)
|
||||
}
|
||||
|
||||
// Local changes the value of the local context during the execution of the action `ma`.
|
||||
// This is similar to Contravariant's contramap and allows you to modify the environment
|
||||
// before passing it to a Reader.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// type DetailedConfig struct { Host string; Port int }
|
||||
// type SimpleConfig struct { Host string }
|
||||
// getHost := func(c SimpleConfig) string { return c.Host }
|
||||
// simplify := func(d DetailedConfig) SimpleConfig { return SimpleConfig{Host: d.Host} }
|
||||
// r := reader.Local(simplify)(getHost)
|
||||
// result := r(DetailedConfig{Host: "localhost", Port: 8080}) // "localhost"
|
||||
//
|
||||
//go:inline
|
||||
func Local[A, R1, R2 any](f func(R2) R1) Kleisli[R2, Reader[R1, A], A] {
|
||||
return Compose[A](f)
|
||||
}
|
||||
|
||||
// Contramap is an alias for Local.
|
||||
// It changes the value of the local context during the execution of a Reader.
|
||||
// This is the contravariant functor operation that transforms the input environment.
|
||||
//
|
||||
// Contramap is semantically identical to Local - both modify the environment before
|
||||
// passing it to a Reader. The name "Contramap" emphasizes the contravariant nature
|
||||
// of the transformation (transforming the input rather than the output).
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// type DetailedConfig struct { Host string; Port int }
|
||||
// type SimpleConfig struct { Host string }
|
||||
// getHost := func(c SimpleConfig) string { return c.Host }
|
||||
// simplify := func(d DetailedConfig) SimpleConfig { return SimpleConfig{Host: d.Host} }
|
||||
// r := reader.Contramap(simplify)(getHost)
|
||||
// result := r(DetailedConfig{Host: "localhost", Port: 8080}) // "localhost"
|
||||
//
|
||||
// See also: Local
|
||||
//
|
||||
//go:inline
|
||||
func Contramap[A, R1, R2 any](f func(R2) R1) Kleisli[R2, Reader[R1, A], A] {
|
||||
return Compose[A](f)
|
||||
}
|
||||
@@ -367,10 +367,10 @@ func Flatten[R, A any](mma Reader[R, Reader[R, A]]) Reader[R, A] {
|
||||
// getConfig := func(e Env) Config { return e.Config }
|
||||
// getPort := func(c Config) int { return c.Port }
|
||||
// getPortFromEnv := reader.Compose(getConfig)(getPort)
|
||||
//
|
||||
//go:inline
|
||||
func Compose[C, R, B any](ab Reader[R, B]) Kleisli[R, Reader[B, C], C] {
|
||||
return func(bc Reader[B, C]) Reader[R, C] {
|
||||
return function.Flow2(ab, bc)
|
||||
}
|
||||
return function.Bind1st(function.Flow2[Reader[R, B], Reader[B, C]], ab)
|
||||
}
|
||||
|
||||
// First applies a Reader to the first element of a tuple, leaving the second element unchanged.
|
||||
@@ -401,42 +401,6 @@ func Second[A, B, C any](pbc Reader[B, C]) Reader[T.Tuple2[A, B], T.Tuple2[A, C]
|
||||
}
|
||||
}
|
||||
|
||||
// Promap is the profunctor map operation that transforms both the input and output of a Reader.
|
||||
// It applies f to the input (contravariantly) and g to the output (covariantly).
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// type Config struct { Port int }
|
||||
// type Env struct { Config Config }
|
||||
// getPort := func(c Config) int { return c.Port }
|
||||
// extractConfig := func(e Env) Config { return e.Config }
|
||||
// toString := strconv.Itoa
|
||||
// r := reader.Promap(extractConfig, toString)(getPort)
|
||||
// result := r(Env{Config: Config{Port: 8080}}) // "8080"
|
||||
func Promap[E, A, D, B any](f func(D) E, g func(A) B) Kleisli[D, Reader[E, A], B] {
|
||||
return func(fea Reader[E, A]) Reader[D, B] {
|
||||
return function.Flow3(f, fea, g)
|
||||
}
|
||||
}
|
||||
|
||||
// Local changes the value of the local context during the execution of the action `ma`.
|
||||
// This is similar to Contravariant's contramap and allows you to modify the environment
|
||||
// before passing it to a Reader.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// type DetailedConfig struct { Host string; Port int }
|
||||
// type SimpleConfig struct { Host string }
|
||||
// getHost := func(c SimpleConfig) string { return c.Host }
|
||||
// simplify := func(d DetailedConfig) SimpleConfig { return SimpleConfig{Host: d.Host} }
|
||||
// r := reader.Local(simplify)(getHost)
|
||||
// result := r(DetailedConfig{Host: "localhost", Port: 8080}) // "localhost"
|
||||
//
|
||||
//go:inline
|
||||
func Local[A, R2, R1 any](f func(R2) R1) Kleisli[R2, Reader[R1, A], A] {
|
||||
return Compose[A](f)
|
||||
}
|
||||
|
||||
// Read applies a context to a Reader to obtain its value.
|
||||
// This is the "run" operation that executes a Reader with a specific environment.
|
||||
//
|
||||
@@ -446,8 +410,10 @@ func Local[A, R2, R1 any](f func(R2) R1) Kleisli[R2, Reader[R1, A], A] {
|
||||
// getPort := reader.Asks(func(c Config) int { return c.Port })
|
||||
// run := reader.Read(Config{Port: 8080})
|
||||
// port := run(getPort) // 8080
|
||||
//
|
||||
//go:inline
|
||||
func Read[A, E any](e E) func(Reader[E, A]) A {
|
||||
return I.Ap[A](e)
|
||||
return I.Flap[A](e)
|
||||
}
|
||||
|
||||
// MonadFlap is the monadic version of Flap.
|
||||
@@ -461,6 +427,8 @@ func Read[A, E any](e E) func(Reader[E, A]) A {
|
||||
// }
|
||||
// r := reader.MonadFlap(getMultiplier, 5)
|
||||
// result := r(Config{Multiplier: 3}) // 15
|
||||
//
|
||||
//go:inline
|
||||
func MonadFlap[R, B, A any](fab Reader[R, func(A) B], a A) Reader[R, B] {
|
||||
return functor.MonadFlap(MonadMap[R, func(A) B, B], fab, a)
|
||||
}
|
||||
@@ -477,6 +445,8 @@ func MonadFlap[R, B, A any](fab Reader[R, func(A) B], a A) Reader[R, B] {
|
||||
// applyTo5 := reader.Flap[Config](5)
|
||||
// r := applyTo5(getMultiplier)
|
||||
// result := r(Config{Multiplier: 3}) // 15
|
||||
//
|
||||
//go:inline
|
||||
func Flap[R, B, A any](a A) Operator[R, func(A) B, B] {
|
||||
return functor.Flap(Map[R, func(A) B, B], a)
|
||||
}
|
||||
|
||||
76
v2/readereither/profunctor.go
Normal file
76
v2/readereither/profunctor.go
Normal file
@@ -0,0 +1,76 @@
|
||||
// Copyright (c) 2025 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 readereither
|
||||
|
||||
import (
|
||||
"github.com/IBM/fp-go/v2/either"
|
||||
"github.com/IBM/fp-go/v2/reader"
|
||||
)
|
||||
|
||||
// Promap is the profunctor map operation that transforms both the input and output of a ReaderEither.
|
||||
// It applies f to the input environment (contravariantly) and g to the output value (covariantly).
|
||||
//
|
||||
// See: https://github.com/fantasyland/fantasy-land?tab=readme-ov-file#profunctor
|
||||
//
|
||||
// This operation allows you to:
|
||||
// - Adapt the environment type before passing it to the ReaderEither (via f)
|
||||
// - Transform the success value after the computation completes (via g)
|
||||
//
|
||||
// The error type E remains unchanged through the transformation.
|
||||
//
|
||||
// Type Parameters:
|
||||
// - R: The original environment type expected by the ReaderEither
|
||||
// - E: The error type (unchanged)
|
||||
// - A: The original success type produced by the ReaderEither
|
||||
// - D: The new input environment type
|
||||
// - B: The new output success type
|
||||
//
|
||||
// Parameters:
|
||||
// - f: Function to transform the input environment from D to R (contravariant)
|
||||
// - g: Function to transform the output success value from A to B (covariant)
|
||||
//
|
||||
// Returns:
|
||||
// - A Kleisli arrow that takes a ReaderEither[R, E, A] and returns a ReaderEither[D, E, B]
|
||||
//
|
||||
//go:inline
|
||||
func Promap[R, E, A, D, B any](f func(D) R, g func(A) B) Kleisli[D, E, ReaderEither[R, E, A], B] {
|
||||
return reader.Promap(f, either.Map[E](g))
|
||||
}
|
||||
|
||||
// Contramap changes the value of the local environment during the execution of a ReaderEither.
|
||||
// This is the contravariant functor operation that transforms the input environment.
|
||||
//
|
||||
// See: https://github.com/fantasyland/fantasy-land?tab=readme-ov-file#profunctor
|
||||
//
|
||||
// Contramap is useful for adapting a ReaderEither to work with a different environment type
|
||||
// by providing a function that converts the new environment to the expected one.
|
||||
//
|
||||
// Type Parameters:
|
||||
// - E: The error type (unchanged)
|
||||
// - A: The success type (unchanged)
|
||||
// - R2: The new input environment type
|
||||
// - R1: The original environment type expected by the ReaderEither
|
||||
//
|
||||
// Parameters:
|
||||
// - f: Function to transform the environment from R2 to R1
|
||||
//
|
||||
// Returns:
|
||||
// - A Kleisli arrow that takes a ReaderEither[R1, E, A] and returns a ReaderEither[R2, E, A]
|
||||
//
|
||||
//go:inline
|
||||
func Contramap[E, A, R1, R2 any](f func(R2) R1) Kleisli[R2, E, ReaderEither[R1, E, A], A] {
|
||||
return reader.Contramap[Either[E, A]](f)
|
||||
}
|
||||
135
v2/readereither/profunctor_test.go
Normal file
135
v2/readereither/profunctor_test.go
Normal file
@@ -0,0 +1,135 @@
|
||||
// Copyright (c) 2025 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 readereither
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
E "github.com/IBM/fp-go/v2/either"
|
||||
N "github.com/IBM/fp-go/v2/number"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type SimpleConfig struct {
|
||||
Port int
|
||||
}
|
||||
|
||||
type DetailedConfig struct {
|
||||
Host string
|
||||
Port int
|
||||
}
|
||||
|
||||
// TestPromapBasic tests basic Promap functionality
|
||||
func TestPromapBasic(t *testing.T) {
|
||||
t.Run("transform both input and output", func(t *testing.T) {
|
||||
// ReaderEither that reads port from SimpleConfig
|
||||
getPort := func(c SimpleConfig) Either[string, int] {
|
||||
return E.Of[string](c.Port)
|
||||
}
|
||||
|
||||
// Transform DetailedConfig to SimpleConfig and int to string
|
||||
simplify := func(d DetailedConfig) SimpleConfig {
|
||||
return SimpleConfig{Port: d.Port}
|
||||
}
|
||||
toString := strconv.Itoa
|
||||
|
||||
adapted := Promap[SimpleConfig, string](simplify, toString)(getPort)
|
||||
result := adapted(DetailedConfig{Host: "localhost", Port: 8080})
|
||||
|
||||
assert.Equal(t, E.Of[string]("8080"), result)
|
||||
})
|
||||
|
||||
t.Run("handles error case", func(t *testing.T) {
|
||||
// ReaderEither that returns an error
|
||||
getError := func(c SimpleConfig) Either[string, int] {
|
||||
return E.Left[int]("error occurred")
|
||||
}
|
||||
|
||||
simplify := func(d DetailedConfig) SimpleConfig {
|
||||
return SimpleConfig{Port: d.Port}
|
||||
}
|
||||
toString := strconv.Itoa
|
||||
|
||||
adapted := Promap[SimpleConfig, string](simplify, toString)(getError)
|
||||
result := adapted(DetailedConfig{Host: "localhost", Port: 8080})
|
||||
|
||||
assert.Equal(t, E.Left[string]("error occurred"), result)
|
||||
})
|
||||
}
|
||||
|
||||
// TestContramapBasic tests basic Contramap functionality
|
||||
func TestContramapBasic(t *testing.T) {
|
||||
t.Run("environment adaptation", func(t *testing.T) {
|
||||
// ReaderEither that reads from SimpleConfig
|
||||
getPort := func(c SimpleConfig) Either[string, int] {
|
||||
return E.Of[string](c.Port)
|
||||
}
|
||||
|
||||
// Adapt to work with DetailedConfig
|
||||
simplify := func(d DetailedConfig) SimpleConfig {
|
||||
return SimpleConfig{Port: d.Port}
|
||||
}
|
||||
|
||||
adapted := Contramap[string, int](simplify)(getPort)
|
||||
result := adapted(DetailedConfig{Host: "localhost", Port: 9000})
|
||||
|
||||
assert.Equal(t, E.Of[string](9000), result)
|
||||
})
|
||||
|
||||
t.Run("preserves error", func(t *testing.T) {
|
||||
getError := func(c SimpleConfig) Either[string, int] {
|
||||
return E.Left[int]("config error")
|
||||
}
|
||||
|
||||
simplify := func(d DetailedConfig) SimpleConfig {
|
||||
return SimpleConfig{Port: d.Port}
|
||||
}
|
||||
|
||||
adapted := Contramap[string, int](simplify)(getError)
|
||||
result := adapted(DetailedConfig{Host: "localhost", Port: 9000})
|
||||
|
||||
assert.Equal(t, E.Left[int]("config error"), result)
|
||||
})
|
||||
}
|
||||
|
||||
// TestPromapComposition tests that Promap can be composed
|
||||
func TestPromapComposition(t *testing.T) {
|
||||
t.Run("compose two Promap transformations", func(t *testing.T) {
|
||||
type Config1 struct{ Value int }
|
||||
type Config2 struct{ Value int }
|
||||
type Config3 struct{ Value int }
|
||||
|
||||
reader := func(c Config1) Either[string, int] {
|
||||
return E.Of[string](c.Value)
|
||||
}
|
||||
|
||||
f1 := func(c2 Config2) Config1 { return Config1{Value: c2.Value} }
|
||||
g1 := N.Mul(2)
|
||||
|
||||
f2 := func(c3 Config3) Config2 { return Config2{Value: c3.Value} }
|
||||
g2 := N.Add(10)
|
||||
|
||||
// Apply two Promap transformations
|
||||
step1 := Promap[Config1, string](f1, g1)(reader)
|
||||
step2 := Promap[Config2, string](f2, g2)(step1)
|
||||
|
||||
result := step2(Config3{Value: 5})
|
||||
|
||||
// (5 * 2) + 10 = 20
|
||||
assert.Equal(t, E.Of[string](20), result)
|
||||
})
|
||||
}
|
||||
@@ -151,7 +151,7 @@ func BiMap[E, E1, E2, A, B any](f func(E1) E2, g func(A) B) func(ReaderEither[E,
|
||||
|
||||
// Local changes the value of the local context during the execution of the action `ma` (similar to `Contravariant`'s
|
||||
// `contramap`).
|
||||
func Local[E, A, R2, R1 any](f func(R2) R1) func(ReaderEither[R1, E, A]) ReaderEither[R2, E, A] {
|
||||
func Local[E, A, R1, R2 any](f func(R2) R1) func(ReaderEither[R1, E, A]) ReaderEither[R2, E, A] {
|
||||
return reader.Local[Either[E, A]](f)
|
||||
}
|
||||
|
||||
|
||||
236
v2/readerio/profunctor.go
Normal file
236
v2/readerio/profunctor.go
Normal file
@@ -0,0 +1,236 @@
|
||||
// Copyright (c) 2025 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 readerio
|
||||
|
||||
import (
|
||||
"github.com/IBM/fp-go/v2/io"
|
||||
"github.com/IBM/fp-go/v2/reader"
|
||||
)
|
||||
|
||||
// Promap is the profunctor map operation that transforms both the input and output of a ReaderIO.
|
||||
// It applies f to the input environment (contravariantly) and g to the output value (covariantly).
|
||||
//
|
||||
// See: https://github.com/fantasyland/fantasy-land?tab=readme-ov-file#profunctor
|
||||
//
|
||||
// This operation allows you to:
|
||||
// - Adapt the environment type before passing it to the ReaderIO (via f)
|
||||
// - Transform the result value after the IO effect completes (via g)
|
||||
//
|
||||
// The transformation happens in two stages:
|
||||
// 1. The input environment D is transformed to E using f (contravariant)
|
||||
// 2. The ReaderIO[E, A] is executed with the transformed environment
|
||||
// 3. The result value A is transformed to B using g (covariant) within the IO context
|
||||
//
|
||||
// Type Parameters:
|
||||
// - E: The original environment type expected by the ReaderIO
|
||||
// - A: The original result type produced by the ReaderIO
|
||||
// - D: The new input environment type
|
||||
// - B: The new output result type
|
||||
//
|
||||
// Parameters:
|
||||
// - f: Function to transform the input environment from D to E (contravariant)
|
||||
// - g: Function to transform the output value from A to B (covariant)
|
||||
//
|
||||
// Returns:
|
||||
// - A Kleisli arrow that takes a ReaderIO[E, A] and returns a ReaderIO[D, B]
|
||||
//
|
||||
// Example - Adapting environment and transforming result:
|
||||
//
|
||||
// type DetailedConfig struct {
|
||||
// Host string
|
||||
// Port int
|
||||
// Debug bool
|
||||
// }
|
||||
//
|
||||
// type SimpleConfig struct {
|
||||
// Host string
|
||||
// Port int
|
||||
// }
|
||||
//
|
||||
// // ReaderIO that reads port from SimpleConfig
|
||||
// getPort := readerio.Asks(func(c SimpleConfig) io.IO[int] {
|
||||
// return io.Of(c.Port)
|
||||
// })
|
||||
//
|
||||
// // Adapt DetailedConfig to SimpleConfig and convert int to string
|
||||
// simplify := func(d DetailedConfig) SimpleConfig {
|
||||
// return SimpleConfig{Host: d.Host, Port: d.Port}
|
||||
// }
|
||||
// toString := strconv.Itoa
|
||||
//
|
||||
// adapted := readerio.Promap(simplify, toString)(getPort)
|
||||
// result := adapted(DetailedConfig{Host: "localhost", Port: 8080, Debug: true})()
|
||||
// // result = "8080"
|
||||
//
|
||||
// Example - Logging with environment transformation:
|
||||
//
|
||||
// type AppEnv struct {
|
||||
// Logger *log.Logger
|
||||
// Config Config
|
||||
// }
|
||||
//
|
||||
// type LoggerEnv struct {
|
||||
// Logger *log.Logger
|
||||
// }
|
||||
//
|
||||
// logMessage := func(msg string) readerio.ReaderIO[LoggerEnv, func()] {
|
||||
// return readerio.Asks(func(env LoggerEnv) io.IO[func()] {
|
||||
// return io.Of(func() { env.Logger.Println(msg) })
|
||||
// })
|
||||
// }
|
||||
//
|
||||
// extractLogger := func(app AppEnv) LoggerEnv {
|
||||
// return LoggerEnv{Logger: app.Logger}
|
||||
// }
|
||||
// ignore := func(func()) string { return "logged" }
|
||||
//
|
||||
// logAndReturn := readerio.Promap(extractLogger, ignore)(logMessage("Hello"))
|
||||
// // Now works with AppEnv and returns string instead of func()
|
||||
//
|
||||
//go:inline
|
||||
func Promap[E, A, D, B any](f func(D) E, g func(A) B) Kleisli[D, ReaderIO[E, A], B] {
|
||||
return reader.Promap(f, io.Map(g))
|
||||
}
|
||||
|
||||
// Local changes the value of the local environment during the execution of a ReaderIO.
|
||||
// This allows you to modify or adapt the environment before passing it to a ReaderIO computation.
|
||||
//
|
||||
// Local is particularly useful for:
|
||||
// - Extracting a subset of a larger environment
|
||||
// - Transforming environment types
|
||||
// - Providing different views of the same environment to different computations
|
||||
//
|
||||
// The transformation is contravariant - it transforms the input environment before
|
||||
// the ReaderIO computation sees it, but doesn't affect the output value.
|
||||
//
|
||||
// Type Parameters:
|
||||
// - A: The result type produced by the ReaderIO
|
||||
// - R2: The new input environment type
|
||||
// - R1: The original environment type expected by the ReaderIO
|
||||
//
|
||||
// Parameters:
|
||||
// - f: Function to transform the environment from R2 to R1
|
||||
//
|
||||
// Returns:
|
||||
// - A Kleisli arrow that takes a ReaderIO[R1, A] and returns a ReaderIO[R2, A]
|
||||
//
|
||||
// Example - Extracting a subset of environment:
|
||||
//
|
||||
// type AppConfig struct {
|
||||
// Database DatabaseConfig
|
||||
// Server ServerConfig
|
||||
// Logger *log.Logger
|
||||
// }
|
||||
//
|
||||
// type DatabaseConfig struct {
|
||||
// Host string
|
||||
// Port int
|
||||
// }
|
||||
//
|
||||
// // ReaderIO that only needs DatabaseConfig
|
||||
// connectDB := readerio.Asks(func(cfg DatabaseConfig) io.IO[string] {
|
||||
// return io.Of(fmt.Sprintf("Connected to %s:%d", cfg.Host, cfg.Port))
|
||||
// })
|
||||
//
|
||||
// // Extract database config from full app config
|
||||
// extractDB := func(app AppConfig) DatabaseConfig {
|
||||
// return app.Database
|
||||
// }
|
||||
//
|
||||
// // Adapt to work with full AppConfig
|
||||
// connectWithAppConfig := readerio.Local(extractDB)(connectDB)
|
||||
// result := connectWithAppConfig(AppConfig{
|
||||
// Database: DatabaseConfig{Host: "localhost", Port: 5432},
|
||||
// })()
|
||||
// // result = "Connected to localhost:5432"
|
||||
//
|
||||
// Example - Providing different views:
|
||||
//
|
||||
// type FullEnv struct {
|
||||
// UserID int
|
||||
// Role string
|
||||
// }
|
||||
//
|
||||
// type UserEnv struct {
|
||||
// UserID int
|
||||
// }
|
||||
//
|
||||
// getUserData := readerio.Asks(func(env UserEnv) io.IO[string] {
|
||||
// return io.Of(fmt.Sprintf("User: %d", env.UserID))
|
||||
// })
|
||||
//
|
||||
// toUserEnv := func(full FullEnv) UserEnv {
|
||||
// return UserEnv{UserID: full.UserID}
|
||||
// }
|
||||
//
|
||||
// adapted := readerio.Local(toUserEnv)(getUserData)
|
||||
// result := adapted(FullEnv{UserID: 42, Role: "admin"})()
|
||||
// // result = "User: 42"
|
||||
//
|
||||
//go:inline
|
||||
func Local[A, R1, R2 any](f func(R2) R1) Kleisli[R2, ReaderIO[R1, A], A] {
|
||||
return reader.Local[IO[A]](f)
|
||||
}
|
||||
|
||||
// Contramap is an alias for Local.
|
||||
// It changes the value of the local environment during the execution of a ReaderIO.
|
||||
// This is the contravariant functor operation that transforms the input environment.
|
||||
//
|
||||
// Contramap is semantically identical to Local - both modify the environment before
|
||||
// passing it to a ReaderIO. The name "Contramap" emphasizes the contravariant nature
|
||||
// of the transformation (transforming the input rather than the output).
|
||||
//
|
||||
// Type Parameters:
|
||||
// - A: The result type produced by the ReaderIO
|
||||
// - R2: The new input environment type
|
||||
// - R1: The original environment type expected by the ReaderIO
|
||||
//
|
||||
// Parameters:
|
||||
// - f: Function to transform the environment from R2 to R1
|
||||
//
|
||||
// Returns:
|
||||
// - A Kleisli arrow that takes a ReaderIO[R1, A] and returns a ReaderIO[R2, A]
|
||||
//
|
||||
// Example - Environment adaptation:
|
||||
//
|
||||
// type DetailedEnv struct {
|
||||
// Config Config
|
||||
// Logger *log.Logger
|
||||
// Metrics Metrics
|
||||
// }
|
||||
//
|
||||
// type SimpleEnv struct {
|
||||
// Config Config
|
||||
// }
|
||||
//
|
||||
// readConfig := readerio.Asks(func(env SimpleEnv) io.IO[string] {
|
||||
// return io.Of(env.Config.Value)
|
||||
// })
|
||||
//
|
||||
// simplify := func(detailed DetailedEnv) SimpleEnv {
|
||||
// return SimpleEnv{Config: detailed.Config}
|
||||
// }
|
||||
//
|
||||
// adapted := readerio.Contramap(simplify)(readConfig)
|
||||
// result := adapted(DetailedEnv{Config: Config{Value: "test"}})()
|
||||
// // result = "test"
|
||||
//
|
||||
// See also: Local
|
||||
//
|
||||
//go:inline
|
||||
func Contramap[A, R1, R2 any](f func(R2) R1) Kleisli[R2, ReaderIO[R1, A], A] {
|
||||
return reader.Contramap[IO[A]](f)
|
||||
}
|
||||
614
v2/readerio/profunctor_test.go
Normal file
614
v2/readerio/profunctor_test.go
Normal file
@@ -0,0 +1,614 @@
|
||||
// Copyright (c) 2025 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 readerio
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/IBM/fp-go/v2/io"
|
||||
N "github.com/IBM/fp-go/v2/number"
|
||||
"github.com/IBM/fp-go/v2/reader"
|
||||
S "github.com/IBM/fp-go/v2/string"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// Test environment types
|
||||
type DetailedConfig struct {
|
||||
Host string
|
||||
Port int
|
||||
Debug bool
|
||||
}
|
||||
|
||||
type SimpleConfig struct {
|
||||
Host string
|
||||
Port int
|
||||
}
|
||||
|
||||
type AppConfig struct {
|
||||
Database DatabaseConfig
|
||||
Server ServerConfig
|
||||
LogLevel string
|
||||
}
|
||||
|
||||
type DatabaseConfig struct {
|
||||
Host string
|
||||
Port int
|
||||
}
|
||||
|
||||
type ServerConfig struct {
|
||||
Port int
|
||||
Timeout int
|
||||
}
|
||||
|
||||
type UserEnv struct {
|
||||
UserID int
|
||||
}
|
||||
|
||||
type FullEnv struct {
|
||||
UserID int
|
||||
Role string
|
||||
}
|
||||
|
||||
// TestPromapBasic tests basic Promap functionality
|
||||
func TestPromapBasic(t *testing.T) {
|
||||
t.Run("transform both input and output", func(t *testing.T) {
|
||||
// ReaderIO that reads port from SimpleConfig
|
||||
getPort := func(c SimpleConfig) IO[int] {
|
||||
return io.Of(c.Port)
|
||||
}
|
||||
|
||||
// Adapt DetailedConfig to SimpleConfig and convert int to string
|
||||
simplify := func(d DetailedConfig) SimpleConfig {
|
||||
return SimpleConfig{Host: d.Host, Port: d.Port}
|
||||
}
|
||||
toString := strconv.Itoa
|
||||
|
||||
adapted := Promap(simplify, toString)(getPort)
|
||||
result := adapted(DetailedConfig{Host: "localhost", Port: 8080, Debug: true})()
|
||||
|
||||
assert.Equal(t, "8080", result)
|
||||
})
|
||||
|
||||
t.Run("identity transformations", func(t *testing.T) {
|
||||
getValue := func(n int) IO[int] {
|
||||
return io.Of(n * 2)
|
||||
}
|
||||
|
||||
// Identity functions should not change behavior
|
||||
identity := reader.Ask[int]()
|
||||
adapted := Promap(identity, identity)(getValue)
|
||||
result := adapted(5)()
|
||||
|
||||
assert.Equal(t, 10, result)
|
||||
})
|
||||
|
||||
t.Run("compose multiple transformations", func(t *testing.T) {
|
||||
getPort := func(c SimpleConfig) IO[int] {
|
||||
return io.Of(c.Port)
|
||||
}
|
||||
|
||||
// First transformation
|
||||
simplify := func(d DetailedConfig) SimpleConfig {
|
||||
return SimpleConfig{Host: d.Host, Port: d.Port}
|
||||
}
|
||||
double := N.Mul(2)
|
||||
|
||||
step1 := Promap(simplify, double)(getPort)
|
||||
|
||||
// Second transformation
|
||||
addDebug := func(d DetailedConfig) DetailedConfig {
|
||||
d.Debug = true
|
||||
return d
|
||||
}
|
||||
toString := S.Format[int]("Port: %d")
|
||||
|
||||
step2 := Promap(addDebug, toString)(step1)
|
||||
result := step2(DetailedConfig{Host: "localhost", Port: 8080})()
|
||||
|
||||
assert.Equal(t, "Port: 16160", result)
|
||||
})
|
||||
}
|
||||
|
||||
// TestPromapWithIO tests Promap with actual IO effects
|
||||
func TestPromapWithIO(t *testing.T) {
|
||||
t.Run("transform IO result", func(t *testing.T) {
|
||||
counter := 0
|
||||
getAndIncrement := func(n int) IO[int] {
|
||||
return func() int {
|
||||
counter++
|
||||
return n + counter
|
||||
}
|
||||
}
|
||||
|
||||
double := reader.Ask[int]()
|
||||
toString := S.Format[int]("Result: %d")
|
||||
|
||||
adapted := Promap(double, toString)(getAndIncrement)
|
||||
result := adapted(10)()
|
||||
|
||||
assert.Equal(t, "Result: 11", result)
|
||||
assert.Equal(t, 1, counter)
|
||||
})
|
||||
|
||||
t.Run("environment transformation with side effects", func(t *testing.T) {
|
||||
var log []string
|
||||
|
||||
logAndReturn := func(msg string) IO[string] {
|
||||
return func() string {
|
||||
log = append(log, msg)
|
||||
return msg
|
||||
}
|
||||
}
|
||||
|
||||
addPrefix := S.Prepend("Input: ")
|
||||
addSuffix := S.Append(" [processed]")
|
||||
|
||||
adapted := Promap(addPrefix, addSuffix)(logAndReturn)
|
||||
result := adapted("test")()
|
||||
|
||||
assert.Equal(t, "Input: test [processed]", result)
|
||||
assert.Equal(t, []string{"Input: test"}, log)
|
||||
})
|
||||
}
|
||||
|
||||
// TestPromapEnvironmentExtraction tests extracting subsets of environments
|
||||
func TestPromapEnvironmentExtraction(t *testing.T) {
|
||||
t.Run("extract database config", func(t *testing.T) {
|
||||
connectDB := func(cfg DatabaseConfig) IO[string] {
|
||||
return io.Of(fmt.Sprintf("Connected to %s:%d", cfg.Host, cfg.Port))
|
||||
}
|
||||
|
||||
extractDB := func(app AppConfig) DatabaseConfig {
|
||||
return app.Database
|
||||
}
|
||||
identity := reader.Ask[string]()
|
||||
|
||||
adapted := Promap(extractDB, identity)(connectDB)
|
||||
result := adapted(AppConfig{
|
||||
Database: DatabaseConfig{Host: "localhost", Port: 5432},
|
||||
Server: ServerConfig{Port: 8080, Timeout: 30},
|
||||
})()
|
||||
|
||||
assert.Equal(t, "Connected to localhost:5432", result)
|
||||
})
|
||||
|
||||
t.Run("extract and transform", func(t *testing.T) {
|
||||
getServerPort := func(cfg ServerConfig) IO[int] {
|
||||
return io.Of(cfg.Port)
|
||||
}
|
||||
|
||||
extractServer := func(app AppConfig) ServerConfig {
|
||||
return app.Server
|
||||
}
|
||||
formatPort := func(port int) string {
|
||||
return fmt.Sprintf("Server listening on port %d", port)
|
||||
}
|
||||
|
||||
adapted := Promap(extractServer, formatPort)(getServerPort)
|
||||
result := adapted(AppConfig{
|
||||
Server: ServerConfig{Port: 8080, Timeout: 30},
|
||||
})()
|
||||
|
||||
assert.Equal(t, "Server listening on port 8080", result)
|
||||
})
|
||||
}
|
||||
|
||||
// TestLocalBasic tests basic Local functionality
|
||||
func TestLocalBasic(t *testing.T) {
|
||||
t.Run("extract subset of environment", func(t *testing.T) {
|
||||
connectDB := func(cfg DatabaseConfig) IO[string] {
|
||||
return io.Of(fmt.Sprintf("Connected to %s:%d", cfg.Host, cfg.Port))
|
||||
}
|
||||
|
||||
extractDB := func(app AppConfig) DatabaseConfig {
|
||||
return app.Database
|
||||
}
|
||||
|
||||
adapted := Local[string](extractDB)(connectDB)
|
||||
result := adapted(AppConfig{
|
||||
Database: DatabaseConfig{Host: "localhost", Port: 5432},
|
||||
})()
|
||||
|
||||
assert.Equal(t, "Connected to localhost:5432", result)
|
||||
})
|
||||
|
||||
t.Run("transform environment type", func(t *testing.T) {
|
||||
getUserData := func(env UserEnv) IO[string] {
|
||||
return io.Of(fmt.Sprintf("User: %d", env.UserID))
|
||||
}
|
||||
|
||||
toUserEnv := func(full FullEnv) UserEnv {
|
||||
return UserEnv{UserID: full.UserID}
|
||||
}
|
||||
|
||||
adapted := Local[string](toUserEnv)(getUserData)
|
||||
result := adapted(FullEnv{UserID: 42, Role: "admin"})()
|
||||
|
||||
assert.Equal(t, "User: 42", result)
|
||||
})
|
||||
|
||||
t.Run("identity transformation", func(t *testing.T) {
|
||||
getValue := func(n int) IO[int] {
|
||||
return io.Of(n * 2)
|
||||
}
|
||||
|
||||
identity := reader.Ask[int]()
|
||||
adapted := Local[int](identity)(getValue)
|
||||
result := adapted(5)()
|
||||
|
||||
assert.Equal(t, 10, result)
|
||||
})
|
||||
}
|
||||
|
||||
// TestLocalComposition tests composing Local transformations
|
||||
func TestLocalComposition(t *testing.T) {
|
||||
t.Run("compose two Local transformations", func(t *testing.T) {
|
||||
getPort := func(cfg DatabaseConfig) IO[int] {
|
||||
return io.Of(cfg.Port)
|
||||
}
|
||||
|
||||
extractDB := func(app AppConfig) DatabaseConfig {
|
||||
return app.Database
|
||||
}
|
||||
|
||||
// First Local
|
||||
step1 := Local[int](extractDB)(getPort)
|
||||
|
||||
// Second Local - add default values
|
||||
addDefaults := func(app AppConfig) AppConfig {
|
||||
if app.Database.Host == "" {
|
||||
app.Database.Host = "localhost"
|
||||
}
|
||||
return app
|
||||
}
|
||||
|
||||
step2 := Local[int](addDefaults)(step1)
|
||||
result := step2(AppConfig{
|
||||
Database: DatabaseConfig{Host: "", Port: 5432},
|
||||
})()
|
||||
|
||||
assert.Equal(t, 5432, result)
|
||||
})
|
||||
|
||||
t.Run("chain multiple environment transformations", func(t *testing.T) {
|
||||
getHost := func(cfg SimpleConfig) IO[string] {
|
||||
return io.Of(cfg.Host)
|
||||
}
|
||||
|
||||
// Transform DetailedConfig -> SimpleConfig
|
||||
simplify := func(d DetailedConfig) SimpleConfig {
|
||||
return SimpleConfig{Host: d.Host, Port: d.Port}
|
||||
}
|
||||
|
||||
adapted := Local[string](simplify)(getHost)
|
||||
result := adapted(DetailedConfig{Host: "example.com", Port: 8080, Debug: true})()
|
||||
|
||||
assert.Equal(t, "example.com", result)
|
||||
})
|
||||
}
|
||||
|
||||
// TestLocalWithIO tests Local with IO effects
|
||||
func TestLocalWithIO(t *testing.T) {
|
||||
t.Run("environment transformation with side effects", func(t *testing.T) {
|
||||
var accessLog []int
|
||||
|
||||
logAccess := func(id int) IO[string] {
|
||||
return func() string {
|
||||
accessLog = append(accessLog, id)
|
||||
return fmt.Sprintf("Accessed: %d", id)
|
||||
}
|
||||
}
|
||||
|
||||
extractUserID := func(env FullEnv) int {
|
||||
return env.UserID
|
||||
}
|
||||
|
||||
adapted := Local[string](extractUserID)(logAccess)
|
||||
result := adapted(FullEnv{UserID: 123, Role: "user"})()
|
||||
|
||||
assert.Equal(t, "Accessed: 123", result)
|
||||
assert.Equal(t, []int{123}, accessLog)
|
||||
})
|
||||
|
||||
t.Run("multiple executions with different environments", func(t *testing.T) {
|
||||
counter := 0
|
||||
increment := func(n int) IO[int] {
|
||||
return func() int {
|
||||
counter++
|
||||
return n + counter
|
||||
}
|
||||
}
|
||||
|
||||
double := N.Mul(2)
|
||||
adapted := Local[int](double)(increment)
|
||||
|
||||
result1 := adapted(5)() // 10 + 1 = 11
|
||||
result2 := adapted(10)() // 20 + 2 = 22
|
||||
|
||||
assert.Equal(t, 11, result1)
|
||||
assert.Equal(t, 22, result2)
|
||||
assert.Equal(t, 2, counter)
|
||||
})
|
||||
}
|
||||
|
||||
// TestContramapBasic tests basic Contramap functionality
|
||||
func TestContramapBasic(t *testing.T) {
|
||||
t.Run("environment adaptation", func(t *testing.T) {
|
||||
readConfig := func(env SimpleConfig) IO[string] {
|
||||
return io.Of(fmt.Sprintf("%s:%d", env.Host, env.Port))
|
||||
}
|
||||
|
||||
simplify := func(detailed DetailedConfig) SimpleConfig {
|
||||
return SimpleConfig{Host: detailed.Host, Port: detailed.Port}
|
||||
}
|
||||
|
||||
adapted := Contramap[string](simplify)(readConfig)
|
||||
result := adapted(DetailedConfig{Host: "localhost", Port: 8080, Debug: true})()
|
||||
|
||||
assert.Equal(t, "localhost:8080", result)
|
||||
})
|
||||
|
||||
t.Run("extract field from larger structure", func(t *testing.T) {
|
||||
getPort := func(port int) IO[string] {
|
||||
return io.Of(fmt.Sprintf("Port: %d", port))
|
||||
}
|
||||
|
||||
extractPort := func(cfg SimpleConfig) int {
|
||||
return cfg.Port
|
||||
}
|
||||
|
||||
adapted := Contramap[string](extractPort)(getPort)
|
||||
result := adapted(SimpleConfig{Host: "localhost", Port: 9000})()
|
||||
|
||||
assert.Equal(t, "Port: 9000", result)
|
||||
})
|
||||
}
|
||||
|
||||
// TestContramapVsLocal verifies Contramap and Local are equivalent
|
||||
func TestContramapVsLocal(t *testing.T) {
|
||||
t.Run("same behavior as Local", func(t *testing.T) {
|
||||
getValue := func(n int) IO[int] {
|
||||
return io.Of(n * 3)
|
||||
}
|
||||
|
||||
double := N.Mul(2)
|
||||
|
||||
localResult := Local[int](double)(getValue)(5)()
|
||||
contramapResult := Contramap[int](double)(getValue)(5)()
|
||||
|
||||
assert.Equal(t, localResult, contramapResult)
|
||||
assert.Equal(t, 30, localResult) // (5 * 2) * 3 = 30
|
||||
})
|
||||
|
||||
t.Run("environment extraction equivalence", func(t *testing.T) {
|
||||
getHost := func(cfg SimpleConfig) IO[string] {
|
||||
return io.Of(cfg.Host)
|
||||
}
|
||||
|
||||
simplify := func(d DetailedConfig) SimpleConfig {
|
||||
return SimpleConfig{Host: d.Host, Port: d.Port}
|
||||
}
|
||||
|
||||
env := DetailedConfig{Host: "example.com", Port: 8080, Debug: false}
|
||||
|
||||
localResult := Local[string](simplify)(getHost)(env)()
|
||||
contramapResult := Contramap[string](simplify)(getHost)(env)()
|
||||
|
||||
assert.Equal(t, localResult, contramapResult)
|
||||
assert.Equal(t, "example.com", localResult)
|
||||
})
|
||||
}
|
||||
|
||||
// TestProfunctorLaws tests profunctor laws
|
||||
func TestProfunctorLaws(t *testing.T) {
|
||||
t.Run("identity law", func(t *testing.T) {
|
||||
getValue := func(n int) IO[int] {
|
||||
return io.Of(n + 10)
|
||||
}
|
||||
|
||||
identity := reader.Ask[int]()
|
||||
|
||||
// Promap(id, id) should be equivalent to id
|
||||
adapted := Promap(identity, identity)(getValue)
|
||||
original := getValue(5)()
|
||||
transformed := adapted(5)()
|
||||
|
||||
assert.Equal(t, original, transformed)
|
||||
assert.Equal(t, 15, transformed)
|
||||
})
|
||||
|
||||
t.Run("composition law", func(t *testing.T) {
|
||||
getValue := func(n int) IO[int] {
|
||||
return io.Of(n * 2)
|
||||
}
|
||||
|
||||
f1 := N.Add(1)
|
||||
f2 := N.Mul(3)
|
||||
g1 := N.Sub(5)
|
||||
g2 := N.Mul(2)
|
||||
|
||||
// Promap(f1, g2) . Promap(f2, g1) should equal Promap(f2 . f1, g2 . g1)
|
||||
// Note: composition order is reversed for contravariant part
|
||||
step1 := Promap(f2, g1)(getValue)
|
||||
composed1 := Promap(f1, g2)(step1)
|
||||
|
||||
composed2 := Promap(
|
||||
func(x int) int { return f2(f1(x)) },
|
||||
func(x int) int { return g2(g1(x)) },
|
||||
)(getValue)
|
||||
|
||||
result1 := composed1(10)()
|
||||
result2 := composed2(10)()
|
||||
|
||||
assert.Equal(t, result1, result2)
|
||||
})
|
||||
}
|
||||
|
||||
// TestEdgeCases tests edge cases and special scenarios
|
||||
func TestEdgeCases(t *testing.T) {
|
||||
t.Run("empty struct environment", func(t *testing.T) {
|
||||
type Empty struct{}
|
||||
|
||||
getValue := func(e Empty) IO[int] {
|
||||
return io.Of(42)
|
||||
}
|
||||
|
||||
identity := reader.Ask[Empty]()
|
||||
adapted := Local[int](identity)(getValue)
|
||||
result := adapted(Empty{})()
|
||||
|
||||
assert.Equal(t, 42, result)
|
||||
})
|
||||
|
||||
t.Run("function type handling", func(t *testing.T) {
|
||||
getFunc := func(n int) IO[func(int) int] {
|
||||
return io.Of(N.Mul(2))
|
||||
}
|
||||
|
||||
double := N.Mul(2)
|
||||
applyFunc := reader.Read[int](5)
|
||||
|
||||
adapted := Promap(double, applyFunc)(getFunc)
|
||||
result := adapted(3)() // (3 * 2) = 6, then func(5) = 10
|
||||
|
||||
assert.Equal(t, 10, result)
|
||||
})
|
||||
|
||||
t.Run("complex nested transformations", func(t *testing.T) {
|
||||
type Level3 struct{ Value int }
|
||||
type Level2 struct{ L3 Level3 }
|
||||
type Level1 struct{ L2 Level2 }
|
||||
|
||||
getValue := func(l3 Level3) IO[int] {
|
||||
return io.Of(l3.Value)
|
||||
}
|
||||
|
||||
extract := func(l1 Level1) Level3 {
|
||||
return l1.L2.L3
|
||||
}
|
||||
multiply := N.Mul(10)
|
||||
|
||||
adapted := Promap(extract, multiply)(getValue)
|
||||
result := adapted(Level1{L2: Level2{L3: Level3{Value: 7}}})()
|
||||
|
||||
assert.Equal(t, 70, result)
|
||||
})
|
||||
}
|
||||
|
||||
// TestRealWorldScenarios tests practical use cases
|
||||
func TestRealWorldScenarios(t *testing.T) {
|
||||
t.Run("database connection with config extraction", func(t *testing.T) {
|
||||
type DBConfig struct {
|
||||
ConnectionString string
|
||||
}
|
||||
|
||||
type AppSettings struct {
|
||||
DB DBConfig
|
||||
APIKey string
|
||||
Timeout int
|
||||
}
|
||||
|
||||
connect := func(cfg DBConfig) IO[string] {
|
||||
return io.Of("Connected: " + cfg.ConnectionString)
|
||||
}
|
||||
|
||||
extractDB := func(settings AppSettings) DBConfig {
|
||||
return settings.DB
|
||||
}
|
||||
|
||||
adapted := Local[string](extractDB)(connect)
|
||||
result := adapted(AppSettings{
|
||||
DB: DBConfig{ConnectionString: "postgres://localhost"},
|
||||
APIKey: "secret",
|
||||
Timeout: 30,
|
||||
})()
|
||||
|
||||
assert.Equal(t, "Connected: postgres://localhost", result)
|
||||
})
|
||||
|
||||
t.Run("logging with environment transformation", func(t *testing.T) {
|
||||
type LogContext struct {
|
||||
RequestID string
|
||||
UserID int
|
||||
}
|
||||
|
||||
type RequestContext struct {
|
||||
RequestID string
|
||||
UserID int
|
||||
Path string
|
||||
Method string
|
||||
}
|
||||
|
||||
var logs []string
|
||||
logMessage := func(ctx LogContext) IO[func()] {
|
||||
return func() func() {
|
||||
return func() {
|
||||
logs = append(logs, fmt.Sprintf("[%s] User %d", ctx.RequestID, ctx.UserID))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extractLogContext := func(req RequestContext) LogContext {
|
||||
return LogContext{RequestID: req.RequestID, UserID: req.UserID}
|
||||
}
|
||||
|
||||
adapted := Local[func()](extractLogContext)(logMessage)
|
||||
result := adapted(RequestContext{
|
||||
RequestID: "req-123",
|
||||
UserID: 42,
|
||||
Path: "/api/users",
|
||||
Method: "GET",
|
||||
})()
|
||||
|
||||
result()
|
||||
assert.Equal(t, []string{"[req-123] User 42"}, logs)
|
||||
})
|
||||
|
||||
t.Run("API response transformation", func(t *testing.T) {
|
||||
type APIResponse struct {
|
||||
StatusCode int
|
||||
Body string
|
||||
}
|
||||
|
||||
type EnrichedResponse struct {
|
||||
Response APIResponse
|
||||
Timestamp int64
|
||||
RequestID string
|
||||
}
|
||||
|
||||
formatResponse := func(resp APIResponse) IO[string] {
|
||||
return io.Of(fmt.Sprintf("Status: %d, Body: %s", resp.StatusCode, resp.Body))
|
||||
}
|
||||
|
||||
extractResponse := func(enriched EnrichedResponse) APIResponse {
|
||||
return enriched.Response
|
||||
}
|
||||
addMetadata := func(s string) string {
|
||||
return "[API] " + s
|
||||
}
|
||||
|
||||
adapted := Promap(extractResponse, addMetadata)(formatResponse)
|
||||
result := adapted(EnrichedResponse{
|
||||
Response: APIResponse{StatusCode: 200, Body: "OK"},
|
||||
Timestamp: 1234567890,
|
||||
RequestID: "req-456",
|
||||
})()
|
||||
|
||||
assert.Equal(t, "[API] Status: 200, Body: OK", result)
|
||||
})
|
||||
}
|
||||
@@ -174,7 +174,7 @@ func TestFilterOrElse_WithMap(t *testing.T) {
|
||||
onNegative := func(n int) string { return "negative number" }
|
||||
|
||||
filter := FilterOrElse[context.Context](isPositive, onNegative)
|
||||
double := Map[context.Context, string](func(n int) int { return n * 2 })
|
||||
double := Map[context.Context, string](N.Mul(2))
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
|
||||
76
v2/readerioeither/profunctor.go
Normal file
76
v2/readerioeither/profunctor.go
Normal file
@@ -0,0 +1,76 @@
|
||||
// Copyright (c) 2025 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
|
||||
|
||||
import (
|
||||
"github.com/IBM/fp-go/v2/ioeither"
|
||||
"github.com/IBM/fp-go/v2/reader"
|
||||
)
|
||||
|
||||
// Promap is the profunctor map operation that transforms both the input and output of a ReaderIOEither.
|
||||
// It applies f to the input environment (contravariantly) and g to the output value (covariantly).
|
||||
//
|
||||
// See: https://github.com/fantasyland/fantasy-land?tab=readme-ov-file#profunctor
|
||||
//
|
||||
// This operation allows you to:
|
||||
// - Adapt the environment type before passing it to the ReaderIOEither (via f)
|
||||
// - Transform the success value after the IO effect completes (via g)
|
||||
//
|
||||
// The error type E remains unchanged through the transformation.
|
||||
//
|
||||
// Type Parameters:
|
||||
// - R: The original environment type expected by the ReaderIOEither
|
||||
// - E: The error type (unchanged)
|
||||
// - A: The original success type produced by the ReaderIOEither
|
||||
// - D: The new input environment type
|
||||
// - B: The new output success type
|
||||
//
|
||||
// Parameters:
|
||||
// - f: Function to transform the input environment from D to R (contravariant)
|
||||
// - g: Function to transform the output success value from A to B (covariant)
|
||||
//
|
||||
// Returns:
|
||||
// - A Kleisli arrow that takes a ReaderIOEither[R, E, A] and returns a ReaderIOEither[D, E, B]
|
||||
//
|
||||
//go:inline
|
||||
func Promap[R, E, A, D, B any](f func(D) R, g func(A) B) Kleisli[D, E, ReaderIOEither[R, E, A], B] {
|
||||
return reader.Promap(f, ioeither.Map[E](g))
|
||||
}
|
||||
|
||||
// Contramap changes the value of the local environment during the execution of a ReaderIOEither.
|
||||
// This is the contravariant functor operation that transforms the input environment.
|
||||
//
|
||||
// See: https://github.com/fantasyland/fantasy-land?tab=readme-ov-file#profunctor
|
||||
//
|
||||
// Contramap is useful for adapting a ReaderIOEither to work with a different environment type
|
||||
// by providing a function that converts the new environment to the expected one.
|
||||
//
|
||||
// Type Parameters:
|
||||
// - E: The error type (unchanged)
|
||||
// - A: The success type (unchanged)
|
||||
// - R2: The new input environment type
|
||||
// - R1: The original environment type expected by the ReaderIOEither
|
||||
//
|
||||
// Parameters:
|
||||
// - f: Function to transform the environment from R2 to R1
|
||||
//
|
||||
// Returns:
|
||||
// - A Kleisli arrow that takes a ReaderIOEither[R1, E, A] and returns a ReaderIOEither[R2, E, A]
|
||||
//
|
||||
//go:inline
|
||||
func Contramap[E, A, R1, R2 any](f func(R2) R1) Kleisli[R2, E, ReaderIOEither[R1, E, A], A] {
|
||||
return reader.Contramap[IOEither[E, A]](f)
|
||||
}
|
||||
133
v2/readerioeither/profunctor_test.go
Normal file
133
v2/readerioeither/profunctor_test.go
Normal file
@@ -0,0 +1,133 @@
|
||||
// Copyright (c) 2025 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
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
E "github.com/IBM/fp-go/v2/either"
|
||||
IOE "github.com/IBM/fp-go/v2/ioeither"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type SimpleConfig struct {
|
||||
Port int
|
||||
}
|
||||
|
||||
type DetailedConfig struct {
|
||||
Host string
|
||||
Port int
|
||||
}
|
||||
|
||||
// TestPromapBasic tests basic Promap functionality
|
||||
func TestPromapBasic(t *testing.T) {
|
||||
t.Run("transform both input and output", func(t *testing.T) {
|
||||
// ReaderIOEither that reads port from SimpleConfig
|
||||
getPort := func(c SimpleConfig) IOEither[string, int] {
|
||||
return IOE.Of[string](c.Port)
|
||||
}
|
||||
|
||||
// Transform DetailedConfig to SimpleConfig and int to string
|
||||
simplify := func(d DetailedConfig) SimpleConfig {
|
||||
return SimpleConfig{Port: d.Port}
|
||||
}
|
||||
toString := strconv.Itoa
|
||||
|
||||
adapted := Promap[SimpleConfig, string](simplify, toString)(getPort)
|
||||
result := adapted(DetailedConfig{Host: "localhost", Port: 8080})()
|
||||
|
||||
assert.Equal(t, E.Of[string]("8080"), result)
|
||||
})
|
||||
|
||||
t.Run("handles error case", func(t *testing.T) {
|
||||
// ReaderIOEither that returns an error
|
||||
getError := func(c SimpleConfig) IOEither[string, int] {
|
||||
return IOE.Left[int]("error occurred")
|
||||
}
|
||||
|
||||
simplify := func(d DetailedConfig) SimpleConfig {
|
||||
return SimpleConfig{Port: d.Port}
|
||||
}
|
||||
toString := strconv.Itoa
|
||||
|
||||
adapted := Promap[SimpleConfig, string](simplify, toString)(getError)
|
||||
result := adapted(DetailedConfig{Host: "localhost", Port: 8080})()
|
||||
|
||||
assert.Equal(t, E.Left[string]("error occurred"), result)
|
||||
})
|
||||
}
|
||||
|
||||
// TestContramapBasic tests basic Contramap functionality
|
||||
func TestContramapBasic(t *testing.T) {
|
||||
t.Run("environment adaptation", func(t *testing.T) {
|
||||
// ReaderIOEither that reads from SimpleConfig
|
||||
getPort := func(c SimpleConfig) IOEither[string, int] {
|
||||
return IOE.Of[string](c.Port)
|
||||
}
|
||||
|
||||
// Adapt to work with DetailedConfig
|
||||
simplify := func(d DetailedConfig) SimpleConfig {
|
||||
return SimpleConfig{Port: d.Port}
|
||||
}
|
||||
|
||||
adapted := Contramap[string, int](simplify)(getPort)
|
||||
result := adapted(DetailedConfig{Host: "localhost", Port: 9000})()
|
||||
|
||||
assert.Equal(t, E.Of[string](9000), result)
|
||||
})
|
||||
|
||||
t.Run("preserves error", func(t *testing.T) {
|
||||
getError := func(c SimpleConfig) IOEither[string, int] {
|
||||
return IOE.Left[int]("config error")
|
||||
}
|
||||
|
||||
simplify := func(d DetailedConfig) SimpleConfig {
|
||||
return SimpleConfig{Port: d.Port}
|
||||
}
|
||||
|
||||
adapted := Contramap[string, int](simplify)(getError)
|
||||
result := adapted(DetailedConfig{Host: "localhost", Port: 9000})()
|
||||
|
||||
assert.Equal(t, E.Left[int]("config error"), result)
|
||||
})
|
||||
}
|
||||
|
||||
// TestPromapWithIO tests Promap with actual IO effects
|
||||
func TestPromapWithIO(t *testing.T) {
|
||||
t.Run("transform IO result", func(t *testing.T) {
|
||||
counter := 0
|
||||
|
||||
// ReaderIOEither with side effect
|
||||
getPortWithEffect := func(c SimpleConfig) IOEither[string, int] {
|
||||
return func() E.Either[string, int] {
|
||||
counter++
|
||||
return E.Of[string](c.Port)
|
||||
}
|
||||
}
|
||||
|
||||
simplify := func(d DetailedConfig) SimpleConfig {
|
||||
return SimpleConfig{Port: d.Port}
|
||||
}
|
||||
toString := strconv.Itoa
|
||||
|
||||
adapted := Promap[SimpleConfig, string](simplify, toString)(getPortWithEffect)
|
||||
result := adapted(DetailedConfig{Host: "localhost", Port: 8080})()
|
||||
|
||||
assert.Equal(t, E.Of[string]("8080"), result)
|
||||
assert.Equal(t, 1, counter) // Side effect occurred
|
||||
})
|
||||
}
|
||||
@@ -182,7 +182,7 @@ func TestFilterOrElse_WithMap(t *testing.T) {
|
||||
onNegative := func(n int) error { return fmt.Errorf("negative number") }
|
||||
|
||||
filter := FilterOrElse[context.Context](isPositive, onNegative)
|
||||
double := Map[context.Context](func(n int) int { return n * 2 })
|
||||
double := Map[context.Context](N.Mul(2))
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
|
||||
73
v2/readerioresult/profunctor.go
Normal file
73
v2/readerioresult/profunctor.go
Normal file
@@ -0,0 +1,73 @@
|
||||
// Copyright (c) 2025 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 readerioresult
|
||||
|
||||
import (
|
||||
RIOE "github.com/IBM/fp-go/v2/readerioeither"
|
||||
)
|
||||
|
||||
// Promap is the profunctor map operation that transforms both the input and output of a ReaderIOResult.
|
||||
// It applies f to the input environment (contravariantly) and g to the output value (covariantly).
|
||||
//
|
||||
// See: https://github.com/fantasyland/fantasy-land?tab=readme-ov-file#profunctor
|
||||
//
|
||||
// This operation allows you to:
|
||||
// - Adapt the environment type before passing it to the ReaderIOResult (via f)
|
||||
// - Transform the success value after the IO effect completes (via g)
|
||||
//
|
||||
// The error type is fixed as error and remains unchanged through the transformation.
|
||||
//
|
||||
// Type Parameters:
|
||||
// - R: The original environment type expected by the ReaderIOResult
|
||||
// - A: The original success type produced by the ReaderIOResult
|
||||
// - D: The new input environment type
|
||||
// - B: The new output success type
|
||||
//
|
||||
// Parameters:
|
||||
// - f: Function to transform the input environment from D to R (contravariant)
|
||||
// - g: Function to transform the output success value from A to B (covariant)
|
||||
//
|
||||
// Returns:
|
||||
// - A Kleisli arrow that takes a ReaderIOResult[R, A] and returns a ReaderIOResult[D, B]
|
||||
//
|
||||
//go:inline
|
||||
func Promap[R, A, D, B any](f func(D) R, g func(A) B) Kleisli[D, ReaderIOResult[R, A], B] {
|
||||
return RIOE.Promap[R, error](f, g)
|
||||
}
|
||||
|
||||
// Contramap changes the value of the local environment during the execution of a ReaderIOResult.
|
||||
// This is the contravariant functor operation that transforms the input environment.
|
||||
//
|
||||
// See: https://github.com/fantasyland/fantasy-land?tab=readme-ov-file#profunctor
|
||||
//
|
||||
// Contramap is useful for adapting a ReaderIOResult to work with a different environment type
|
||||
// by providing a function that converts the new environment to the expected one.
|
||||
//
|
||||
// Type Parameters:
|
||||
// - A: The success type (unchanged)
|
||||
// - R2: The new input environment type
|
||||
// - R1: The original environment type expected by the ReaderIOResult
|
||||
//
|
||||
// Parameters:
|
||||
// - f: Function to transform the environment from R2 to R1
|
||||
//
|
||||
// Returns:
|
||||
// - A Kleisli arrow that takes a ReaderIOResult[R1, A] and returns a ReaderIOResult[R2, A]
|
||||
//
|
||||
//go:inline
|
||||
func Contramap[A, R1, R2 any](f func(R2) R1) Kleisli[R2, ReaderIOResult[R1, A], A] {
|
||||
return RIOE.Contramap[error, A](f)
|
||||
}
|
||||
98
v2/readerioresult/profunctor_test.go
Normal file
98
v2/readerioresult/profunctor_test.go
Normal file
@@ -0,0 +1,98 @@
|
||||
// Copyright (c) 2025 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 readerioresult
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
R "github.com/IBM/fp-go/v2/result"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type SimpleConfig struct {
|
||||
Port int
|
||||
}
|
||||
|
||||
type DetailedConfig struct {
|
||||
Host string
|
||||
Port int
|
||||
}
|
||||
|
||||
// TestPromapBasic tests basic Promap functionality
|
||||
func TestPromapBasic(t *testing.T) {
|
||||
t.Run("transform both input and output", func(t *testing.T) {
|
||||
// ReaderIOResult that reads port from SimpleConfig
|
||||
getPort := func(c SimpleConfig) IOResult[int] {
|
||||
return func() R.Result[int] {
|
||||
return R.Of(c.Port)
|
||||
}
|
||||
}
|
||||
|
||||
// Transform DetailedConfig to SimpleConfig and int to string
|
||||
simplify := func(d DetailedConfig) SimpleConfig {
|
||||
return SimpleConfig{Port: d.Port}
|
||||
}
|
||||
toString := strconv.Itoa
|
||||
|
||||
adapted := Promap(simplify, toString)(getPort)
|
||||
result := adapted(DetailedConfig{Host: "localhost", Port: 8080})()
|
||||
|
||||
assert.Equal(t, R.Of("8080"), result)
|
||||
})
|
||||
|
||||
t.Run("handles error case", func(t *testing.T) {
|
||||
// ReaderIOResult that returns an error
|
||||
getError := func(c SimpleConfig) IOResult[int] {
|
||||
return func() R.Result[int] {
|
||||
return R.Left[int](fmt.Errorf("error occurred"))
|
||||
}
|
||||
}
|
||||
|
||||
simplify := func(d DetailedConfig) SimpleConfig {
|
||||
return SimpleConfig{Port: d.Port}
|
||||
}
|
||||
toString := strconv.Itoa
|
||||
|
||||
adapted := Promap(simplify, toString)(getError)
|
||||
result := adapted(DetailedConfig{Host: "localhost", Port: 8080})()
|
||||
|
||||
assert.True(t, R.IsLeft(result))
|
||||
})
|
||||
}
|
||||
|
||||
// TestContramapBasic tests basic Contramap functionality
|
||||
func TestContramapBasic(t *testing.T) {
|
||||
t.Run("environment adaptation", func(t *testing.T) {
|
||||
// ReaderIOResult that reads from SimpleConfig
|
||||
getPort := func(c SimpleConfig) IOResult[int] {
|
||||
return func() R.Result[int] {
|
||||
return R.Of(c.Port)
|
||||
}
|
||||
}
|
||||
|
||||
// Adapt to work with DetailedConfig
|
||||
simplify := func(d DetailedConfig) SimpleConfig {
|
||||
return SimpleConfig{Port: d.Port}
|
||||
}
|
||||
|
||||
adapted := Contramap[int](simplify)(getPort)
|
||||
result := adapted(DetailedConfig{Host: "localhost", Port: 9000})()
|
||||
|
||||
assert.Equal(t, R.Of(9000), result)
|
||||
})
|
||||
}
|
||||
74
v2/readeroption/profunctor.go
Normal file
74
v2/readeroption/profunctor.go
Normal file
@@ -0,0 +1,74 @@
|
||||
// Copyright (c) 2025 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 readeroption
|
||||
|
||||
import (
|
||||
"github.com/IBM/fp-go/v2/option"
|
||||
"github.com/IBM/fp-go/v2/reader"
|
||||
)
|
||||
|
||||
// Promap is the profunctor map operation that transforms both the input and output of a ReaderOption.
|
||||
// It applies f to the input environment (contravariantly) and g to the output value (covariantly).
|
||||
//
|
||||
// See: https://github.com/fantasyland/fantasy-land?tab=readme-ov-file#profunctor
|
||||
//
|
||||
// This operation allows you to:
|
||||
// - Adapt the environment type before passing it to the ReaderOption (via f)
|
||||
// - Transform the Some value after the computation completes (via g)
|
||||
//
|
||||
// The None case remains unchanged through the transformation.
|
||||
//
|
||||
// Type Parameters:
|
||||
// - R: The original environment type expected by the ReaderOption
|
||||
// - A: The original value type produced by the ReaderOption
|
||||
// - D: The new input environment type
|
||||
// - B: The new output value type
|
||||
//
|
||||
// Parameters:
|
||||
// - f: Function to transform the input environment from D to R (contravariant)
|
||||
// - g: Function to transform the output Some value from A to B (covariant)
|
||||
//
|
||||
// Returns:
|
||||
// - A Kleisli arrow that takes a ReaderOption[R, A] and returns a ReaderOption[D, B]
|
||||
//
|
||||
//go:inline
|
||||
func Promap[R, A, D, B any](f func(D) R, g func(A) B) Kleisli[D, ReaderOption[R, A], B] {
|
||||
return reader.Promap(f, option.Map(g))
|
||||
}
|
||||
|
||||
// Contramap changes the value of the local environment during the execution of a ReaderOption.
|
||||
// This is the contravariant functor operation that transforms the input environment.
|
||||
//
|
||||
// See: https://github.com/fantasyland/fantasy-land?tab=readme-ov-file#profunctor
|
||||
//
|
||||
// Contramap is useful for adapting a ReaderOption to work with a different environment type
|
||||
// by providing a function that converts the new environment to the expected one.
|
||||
//
|
||||
// Type Parameters:
|
||||
// - A: The value type (unchanged)
|
||||
// - R2: The new input environment type
|
||||
// - R1: The original environment type expected by the ReaderOption
|
||||
//
|
||||
// Parameters:
|
||||
// - f: Function to transform the environment from R2 to R1
|
||||
//
|
||||
// Returns:
|
||||
// - A Kleisli arrow that takes a ReaderOption[R1, A] and returns a ReaderOption[R2, A]
|
||||
//
|
||||
//go:inline
|
||||
func Contramap[A, R1, R2 any](f func(R2) R1) Kleisli[R2, ReaderOption[R1, A], A] {
|
||||
return reader.Contramap[Option[A]](f)
|
||||
}
|
||||
106
v2/readeroption/profunctor_test.go
Normal file
106
v2/readeroption/profunctor_test.go
Normal file
@@ -0,0 +1,106 @@
|
||||
// Copyright (c) 2025 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 readeroption
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
O "github.com/IBM/fp-go/v2/option"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type SimpleConfig struct {
|
||||
Port int
|
||||
}
|
||||
|
||||
type DetailedConfig struct {
|
||||
Host string
|
||||
Port int
|
||||
}
|
||||
|
||||
// TestPromapBasic tests basic Promap functionality
|
||||
func TestPromapBasic(t *testing.T) {
|
||||
t.Run("transform both input and output with Some", func(t *testing.T) {
|
||||
// ReaderOption that reads port from SimpleConfig
|
||||
getPort := func(c SimpleConfig) Option[int] {
|
||||
return O.Of(c.Port)
|
||||
}
|
||||
|
||||
// Transform DetailedConfig to SimpleConfig and int to string
|
||||
simplify := func(d DetailedConfig) SimpleConfig {
|
||||
return SimpleConfig{Port: d.Port}
|
||||
}
|
||||
toString := strconv.Itoa
|
||||
|
||||
adapted := Promap(simplify, toString)(getPort)
|
||||
result := adapted(DetailedConfig{Host: "localhost", Port: 8080})
|
||||
|
||||
assert.Equal(t, O.Of("8080"), result)
|
||||
})
|
||||
|
||||
t.Run("handles None case", func(t *testing.T) {
|
||||
// ReaderOption that returns None
|
||||
getNone := func(c SimpleConfig) Option[int] {
|
||||
return O.None[int]()
|
||||
}
|
||||
|
||||
simplify := func(d DetailedConfig) SimpleConfig {
|
||||
return SimpleConfig{Port: d.Port}
|
||||
}
|
||||
toString := strconv.Itoa
|
||||
|
||||
adapted := Promap(simplify, toString)(getNone)
|
||||
result := adapted(DetailedConfig{Host: "localhost", Port: 8080})
|
||||
|
||||
assert.Equal(t, O.None[string](), result)
|
||||
})
|
||||
}
|
||||
|
||||
// TestContramapBasic tests basic Contramap functionality
|
||||
func TestContramapBasic(t *testing.T) {
|
||||
t.Run("environment adaptation", func(t *testing.T) {
|
||||
// ReaderOption that reads from SimpleConfig
|
||||
getPort := func(c SimpleConfig) Option[int] {
|
||||
return O.Of(c.Port)
|
||||
}
|
||||
|
||||
// Adapt to work with DetailedConfig
|
||||
simplify := func(d DetailedConfig) SimpleConfig {
|
||||
return SimpleConfig{Port: d.Port}
|
||||
}
|
||||
|
||||
adapted := Contramap[int](simplify)(getPort)
|
||||
result := adapted(DetailedConfig{Host: "localhost", Port: 9000})
|
||||
|
||||
assert.Equal(t, O.Of(9000), result)
|
||||
})
|
||||
|
||||
t.Run("preserves None", func(t *testing.T) {
|
||||
getNone := func(c SimpleConfig) Option[int] {
|
||||
return O.None[int]()
|
||||
}
|
||||
|
||||
simplify := func(d DetailedConfig) SimpleConfig {
|
||||
return SimpleConfig{Port: d.Port}
|
||||
}
|
||||
|
||||
adapted := Contramap[int](simplify)(getNone)
|
||||
result := adapted(DetailedConfig{Host: "localhost", Port: 9000})
|
||||
|
||||
assert.Equal(t, O.None[int](), result)
|
||||
})
|
||||
}
|
||||
@@ -320,7 +320,7 @@ func Flatten[E, A any](mma ReaderOption[E, ReaderOption[E, A]]) ReaderOption[E,
|
||||
// )
|
||||
//
|
||||
//go:inline
|
||||
func Local[A, R2, R1 any](f func(R2) R1) func(ReaderOption[R1, A]) ReaderOption[R2, A] {
|
||||
func Local[A, R1, R2 any](f func(R2) R1) func(ReaderOption[R1, A]) ReaderOption[R2, A] {
|
||||
return reader.Local[Option[A]](f)
|
||||
}
|
||||
|
||||
|
||||
@@ -182,7 +182,7 @@ func TestFilterOrElse_WithMap(t *testing.T) {
|
||||
onNegative := func(n int) error { return fmt.Errorf("negative number") }
|
||||
|
||||
filter := FilterOrElse[Config](isPositive, onNegative)
|
||||
double := Map[Config](func(n int) int { return n * 2 })
|
||||
double := Map[Config](N.Mul(2))
|
||||
|
||||
cfg := Config{MaxValue: 100}
|
||||
|
||||
|
||||
73
v2/readerresult/profunctor.go
Normal file
73
v2/readerresult/profunctor.go
Normal file
@@ -0,0 +1,73 @@
|
||||
// Copyright (c) 2025 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 readerresult
|
||||
|
||||
import (
|
||||
RE "github.com/IBM/fp-go/v2/readereither"
|
||||
)
|
||||
|
||||
// Promap is the profunctor map operation that transforms both the input and output of a ReaderResult.
|
||||
// It applies f to the input environment (contravariantly) and g to the output value (covariantly).
|
||||
//
|
||||
// See: https://github.com/fantasyland/fantasy-land?tab=readme-ov-file#profunctor
|
||||
//
|
||||
// This operation allows you to:
|
||||
// - Adapt the environment type before passing it to the ReaderResult (via f)
|
||||
// - Transform the success value after the computation completes (via g)
|
||||
//
|
||||
// The error type is fixed as error and remains unchanged through the transformation.
|
||||
//
|
||||
// Type Parameters:
|
||||
// - R: The original environment type expected by the ReaderResult
|
||||
// - A: The original success type produced by the ReaderResult
|
||||
// - D: The new input environment type
|
||||
// - B: The new output success type
|
||||
//
|
||||
// Parameters:
|
||||
// - f: Function to transform the input environment from D to R (contravariant)
|
||||
// - g: Function to transform the output success value from A to B (covariant)
|
||||
//
|
||||
// Returns:
|
||||
// - A Kleisli arrow that takes a ReaderResult[R, A] and returns a ReaderResult[D, B]
|
||||
//
|
||||
//go:inline
|
||||
func Promap[R, A, D, B any](f func(D) R, g func(A) B) Kleisli[D, ReaderResult[R, A], B] {
|
||||
return RE.Promap[R, error](f, g)
|
||||
}
|
||||
|
||||
// Contramap changes the value of the local environment during the execution of a ReaderResult.
|
||||
// This is the contravariant functor operation that transforms the input environment.
|
||||
//
|
||||
// See: https://github.com/fantasyland/fantasy-land?tab=readme-ov-file#profunctor
|
||||
//
|
||||
// Contramap is useful for adapting a ReaderResult to work with a different environment type
|
||||
// by providing a function that converts the new environment to the expected one.
|
||||
//
|
||||
// Type Parameters:
|
||||
// - A: The success type (unchanged)
|
||||
// - R2: The new input environment type
|
||||
// - R1: The original environment type expected by the ReaderResult
|
||||
//
|
||||
// Parameters:
|
||||
// - f: Function to transform the environment from R2 to R1
|
||||
//
|
||||
// Returns:
|
||||
// - A Kleisli arrow that takes a ReaderResult[R1, A] and returns a ReaderResult[R2, A]
|
||||
//
|
||||
//go:inline
|
||||
func Contramap[A, R1, R2 any](f func(R2) R1) Kleisli[R2, ReaderResult[R1, A], A] {
|
||||
return RE.Contramap[error, A](f)
|
||||
}
|
||||
107
v2/readerresult/profunctor_test.go
Normal file
107
v2/readerresult/profunctor_test.go
Normal file
@@ -0,0 +1,107 @@
|
||||
// Copyright (c) 2025 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 readerresult
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
R "github.com/IBM/fp-go/v2/result"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type SimpleConfig struct {
|
||||
Port int
|
||||
}
|
||||
|
||||
type DetailedConfig struct {
|
||||
Host string
|
||||
Port int
|
||||
}
|
||||
|
||||
// TestPromapBasic tests basic Promap functionality
|
||||
func TestPromapBasic(t *testing.T) {
|
||||
t.Run("transform both input and output", func(t *testing.T) {
|
||||
// ReaderResult that reads port from SimpleConfig
|
||||
getPort := func(c SimpleConfig) Result[int] {
|
||||
return R.Of(c.Port)
|
||||
}
|
||||
|
||||
// Transform DetailedConfig to SimpleConfig and int to string
|
||||
simplify := func(d DetailedConfig) SimpleConfig {
|
||||
return SimpleConfig{Port: d.Port}
|
||||
}
|
||||
toString := strconv.Itoa
|
||||
|
||||
adapted := Promap(simplify, toString)(getPort)
|
||||
result := adapted(DetailedConfig{Host: "localhost", Port: 8080})
|
||||
|
||||
assert.Equal(t, R.Of("8080"), result)
|
||||
})
|
||||
|
||||
t.Run("handles error case", func(t *testing.T) {
|
||||
// ReaderResult that returns an error
|
||||
getError := func(c SimpleConfig) Result[int] {
|
||||
return R.Left[int](fmt.Errorf("error occurred"))
|
||||
}
|
||||
|
||||
simplify := func(d DetailedConfig) SimpleConfig {
|
||||
return SimpleConfig{Port: d.Port}
|
||||
}
|
||||
toString := strconv.Itoa
|
||||
|
||||
adapted := Promap(simplify, toString)(getError)
|
||||
result := adapted(DetailedConfig{Host: "localhost", Port: 8080})
|
||||
|
||||
assert.True(t, R.IsLeft(result))
|
||||
})
|
||||
}
|
||||
|
||||
// TestContramapBasic tests basic Contramap functionality
|
||||
func TestContramapBasic(t *testing.T) {
|
||||
t.Run("environment adaptation", func(t *testing.T) {
|
||||
// ReaderResult that reads from SimpleConfig
|
||||
getPort := func(c SimpleConfig) Result[int] {
|
||||
return R.Of(c.Port)
|
||||
}
|
||||
|
||||
// Adapt to work with DetailedConfig
|
||||
simplify := func(d DetailedConfig) SimpleConfig {
|
||||
return SimpleConfig{Port: d.Port}
|
||||
}
|
||||
|
||||
adapted := Contramap[int](simplify)(getPort)
|
||||
result := adapted(DetailedConfig{Host: "localhost", Port: 9000})
|
||||
|
||||
assert.Equal(t, R.Of(9000), result)
|
||||
})
|
||||
|
||||
t.Run("preserves error", func(t *testing.T) {
|
||||
getError := func(c SimpleConfig) Result[int] {
|
||||
return R.Left[int](fmt.Errorf("config error"))
|
||||
}
|
||||
|
||||
simplify := func(d DetailedConfig) SimpleConfig {
|
||||
return SimpleConfig{Port: d.Port}
|
||||
}
|
||||
|
||||
adapted := Contramap[int](simplify)(getError)
|
||||
result := adapted(DetailedConfig{Host: "localhost", Port: 9000})
|
||||
|
||||
assert.True(t, R.IsLeft(result))
|
||||
})
|
||||
}
|
||||
@@ -719,7 +719,7 @@ func BiMap[R, A, B any](f Endomorphism[error], g func(A) B) Operator[R, A, B] {
|
||||
// // adapted now accepts DB instead of Config
|
||||
//
|
||||
//go:inline
|
||||
func Local[A, R2, R1 any](f func(R2) R1) func(ReaderResult[R1, A]) ReaderResult[R2, A] {
|
||||
func Local[A, R1, R2 any](f func(R2) R1) func(ReaderResult[R1, A]) ReaderResult[R2, A] {
|
||||
return reader.Local[Result[A]](f)
|
||||
}
|
||||
|
||||
|
||||
@@ -369,5 +369,3 @@ func TestIntegration_ReduceWithIndexToMap(t *testing.T) {
|
||||
}
|
||||
assert.Equal(t, expected, result)
|
||||
}
|
||||
|
||||
// Made with Bob
|
||||
|
||||
@@ -52,3 +52,61 @@ func AlternativeMonoid[A any](m M.Monoid[A]) Monoid[A] {
|
||||
func AltMonoid[A any](zero Lazy[Result[A]]) Monoid[A] {
|
||||
return either.AltMonoid(zero)
|
||||
}
|
||||
|
||||
// FirstMonoid creates a Monoid for Result[A] that returns the first Ok (Right) value.
|
||||
// This monoid prefers the left operand when it is Ok, otherwise returns the right operand.
|
||||
// The empty value is provided as a lazy computation.
|
||||
//
|
||||
// This is equivalent to AltMonoid but implemented more directly.
|
||||
//
|
||||
// Truth table:
|
||||
//
|
||||
// | x | y | concat(x, y) |
|
||||
// | --------- | --------- | ------------ |
|
||||
// | err(e1) | err(e2) | err(e2) |
|
||||
// | ok(a) | err(e) | ok(a) |
|
||||
// | err(e) | ok(b) | ok(b) |
|
||||
// | ok(a) | ok(b) | ok(a) |
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// import "errors"
|
||||
// zero := func() result.Result[int] { return result.Error[int](errors.New("empty")) }
|
||||
// m := result.FirstMonoid[int](zero)
|
||||
// m.Concat(result.Of(2), result.Of(3)) // Ok(2) - returns first Ok
|
||||
// m.Concat(result.Error[int](errors.New("err")), result.Of(3)) // Ok(3)
|
||||
// m.Concat(result.Of(2), result.Error[int](errors.New("err"))) // Ok(2)
|
||||
// m.Empty() // Error(error("empty"))
|
||||
//
|
||||
//go:inline
|
||||
func FirstMonoid[A any](zero Lazy[Result[A]]) M.Monoid[Result[A]] {
|
||||
return either.FirstMonoid(zero)
|
||||
}
|
||||
|
||||
// LastMonoid creates a Monoid for Result[A] that returns the last Ok (Right) value.
|
||||
// This monoid prefers the right operand when it is Ok, otherwise returns the left operand.
|
||||
// The empty value is provided as a lazy computation.
|
||||
//
|
||||
// Truth table:
|
||||
//
|
||||
// | x | y | concat(x, y) |
|
||||
// | --------- | --------- | ------------ |
|
||||
// | err(e1) | err(e2) | err(e1) |
|
||||
// | ok(a) | err(e) | ok(a) |
|
||||
// | err(e) | ok(b) | ok(b) |
|
||||
// | ok(a) | ok(b) | ok(b) |
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// import "errors"
|
||||
// zero := func() result.Result[int] { return result.Error[int](errors.New("empty")) }
|
||||
// m := result.LastMonoid[int](zero)
|
||||
// m.Concat(result.Of(2), result.Of(3)) // Ok(3) - returns last Ok
|
||||
// m.Concat(result.Error[int](errors.New("err")), result.Of(3)) // Ok(3)
|
||||
// m.Concat(result.Of(2), result.Error[int](errors.New("err"))) // Ok(2)
|
||||
// m.Empty() // Error(error("empty"))
|
||||
//
|
||||
//go:inline
|
||||
func LastMonoid[A any](zero Lazy[Result[A]]) M.Monoid[Result[A]] {
|
||||
return either.LastMonoid(zero)
|
||||
}
|
||||
|
||||
498
v2/result/monoid_test.go
Normal file
498
v2/result/monoid_test.go
Normal file
@@ -0,0 +1,498 @@
|
||||
// Copyright (c) 2025 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 result
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// TestFirstMonoid tests the FirstMonoid implementation
|
||||
func TestFirstMonoid(t *testing.T) {
|
||||
zero := func() Result[int] { return Left[int](errors.New("empty")) }
|
||||
m := FirstMonoid(zero)
|
||||
|
||||
t.Run("both Right values - returns first", func(t *testing.T) {
|
||||
result := m.Concat(Right(2), Right(3))
|
||||
assert.Equal(t, Right(2), result)
|
||||
})
|
||||
|
||||
t.Run("left Right, right Left", func(t *testing.T) {
|
||||
result := m.Concat(Right(2), Left[int](errors.New("err")))
|
||||
assert.Equal(t, Right(2), result)
|
||||
})
|
||||
|
||||
t.Run("left Left, right Right", func(t *testing.T) {
|
||||
result := m.Concat(Left[int](errors.New("err")), Right(3))
|
||||
assert.Equal(t, Right(3), result)
|
||||
})
|
||||
|
||||
t.Run("both Left", func(t *testing.T) {
|
||||
err1 := errors.New("err1")
|
||||
err2 := errors.New("err2")
|
||||
result := m.Concat(Left[int](err1), Left[int](err2))
|
||||
// Should return the second Left
|
||||
assert.True(t, IsLeft(result))
|
||||
_, leftErr := Unwrap(result)
|
||||
assert.Equal(t, err2, leftErr)
|
||||
})
|
||||
|
||||
t.Run("empty value", func(t *testing.T) {
|
||||
empty := m.Empty()
|
||||
assert.True(t, IsLeft(empty))
|
||||
_, leftErr := Unwrap(empty)
|
||||
assert.Equal(t, "empty", leftErr.Error())
|
||||
})
|
||||
|
||||
t.Run("left identity", func(t *testing.T) {
|
||||
x := Right(5)
|
||||
result := m.Concat(m.Empty(), x)
|
||||
assert.Equal(t, x, result)
|
||||
})
|
||||
|
||||
t.Run("right identity", func(t *testing.T) {
|
||||
x := Right(5)
|
||||
result := m.Concat(x, m.Empty())
|
||||
assert.Equal(t, x, result)
|
||||
})
|
||||
|
||||
t.Run("associativity", func(t *testing.T) {
|
||||
a := Right(1)
|
||||
b := Right(2)
|
||||
c := Right(3)
|
||||
|
||||
left := m.Concat(m.Concat(a, b), c)
|
||||
right := m.Concat(a, m.Concat(b, c))
|
||||
|
||||
assert.Equal(t, left, right)
|
||||
assert.Equal(t, Right(1), left)
|
||||
})
|
||||
|
||||
t.Run("multiple concatenations", func(t *testing.T) {
|
||||
// Should return the first Right value encountered
|
||||
result := m.Concat(
|
||||
m.Concat(Left[int](errors.New("err1")), Right(1)),
|
||||
m.Concat(Right(2), Right(3)),
|
||||
)
|
||||
assert.Equal(t, Right(1), result)
|
||||
})
|
||||
|
||||
t.Run("with strings", func(t *testing.T) {
|
||||
zeroStr := func() Result[string] { return Left[string](errors.New("empty")) }
|
||||
strMonoid := FirstMonoid(zeroStr)
|
||||
|
||||
result := strMonoid.Concat(Right("first"), Right("second"))
|
||||
assert.Equal(t, Right("first"), result)
|
||||
|
||||
result = strMonoid.Concat(Left[string](errors.New("err")), Right("second"))
|
||||
assert.Equal(t, Right("second"), result)
|
||||
})
|
||||
}
|
||||
|
||||
// TestLastMonoid tests the LastMonoid implementation
|
||||
func TestLastMonoid(t *testing.T) {
|
||||
zero := func() Result[int] { return Left[int](errors.New("empty")) }
|
||||
m := LastMonoid(zero)
|
||||
|
||||
t.Run("both Right values - returns last", func(t *testing.T) {
|
||||
result := m.Concat(Right(2), Right(3))
|
||||
assert.Equal(t, Right(3), result)
|
||||
})
|
||||
|
||||
t.Run("left Right, right Left", func(t *testing.T) {
|
||||
result := m.Concat(Right(2), Left[int](errors.New("err")))
|
||||
assert.Equal(t, Right(2), result)
|
||||
})
|
||||
|
||||
t.Run("left Left, right Right", func(t *testing.T) {
|
||||
result := m.Concat(Left[int](errors.New("err")), Right(3))
|
||||
assert.Equal(t, Right(3), result)
|
||||
})
|
||||
|
||||
t.Run("both Left", func(t *testing.T) {
|
||||
err1 := errors.New("err1")
|
||||
err2 := errors.New("err2")
|
||||
result := m.Concat(Left[int](err1), Left[int](err2))
|
||||
// Should return the first Left
|
||||
assert.True(t, IsLeft(result))
|
||||
_, leftErr := Unwrap(result)
|
||||
assert.Equal(t, err1, leftErr)
|
||||
})
|
||||
|
||||
t.Run("empty value", func(t *testing.T) {
|
||||
empty := m.Empty()
|
||||
assert.True(t, IsLeft(empty))
|
||||
_, leftErr := Unwrap(empty)
|
||||
assert.Equal(t, "empty", leftErr.Error())
|
||||
})
|
||||
|
||||
t.Run("left identity", func(t *testing.T) {
|
||||
x := Right(5)
|
||||
result := m.Concat(m.Empty(), x)
|
||||
assert.Equal(t, x, result)
|
||||
})
|
||||
|
||||
t.Run("right identity", func(t *testing.T) {
|
||||
x := Right(5)
|
||||
result := m.Concat(x, m.Empty())
|
||||
assert.Equal(t, x, result)
|
||||
})
|
||||
|
||||
t.Run("associativity", func(t *testing.T) {
|
||||
a := Right(1)
|
||||
b := Right(2)
|
||||
c := Right(3)
|
||||
|
||||
left := m.Concat(m.Concat(a, b), c)
|
||||
right := m.Concat(a, m.Concat(b, c))
|
||||
|
||||
assert.Equal(t, left, right)
|
||||
assert.Equal(t, Right(3), left)
|
||||
})
|
||||
|
||||
t.Run("multiple concatenations", func(t *testing.T) {
|
||||
// Should return the last Right value encountered
|
||||
result := m.Concat(
|
||||
m.Concat(Right(1), Right(2)),
|
||||
m.Concat(Right(3), Left[int](errors.New("err"))),
|
||||
)
|
||||
assert.Equal(t, Right(3), result)
|
||||
})
|
||||
|
||||
t.Run("with strings", func(t *testing.T) {
|
||||
zeroStr := func() Result[string] { return Left[string](errors.New("empty")) }
|
||||
strMonoid := LastMonoid(zeroStr)
|
||||
|
||||
result := strMonoid.Concat(Right("first"), Right("second"))
|
||||
assert.Equal(t, Right("second"), result)
|
||||
|
||||
result = strMonoid.Concat(Right("first"), Left[string](errors.New("err")))
|
||||
assert.Equal(t, Right("first"), result)
|
||||
})
|
||||
}
|
||||
|
||||
// TestAltMonoid tests the AltMonoid implementation
|
||||
func TestAltMonoid(t *testing.T) {
|
||||
zero := func() Result[int] { return Left[int](errors.New("empty")) }
|
||||
m := AltMonoid(zero)
|
||||
|
||||
t.Run("both Right values - returns first", func(t *testing.T) {
|
||||
result := m.Concat(Right(2), Right(3))
|
||||
assert.Equal(t, Right(2), result)
|
||||
})
|
||||
|
||||
t.Run("left Right, right Left", func(t *testing.T) {
|
||||
result := m.Concat(Right(2), Left[int](errors.New("err")))
|
||||
assert.Equal(t, Right(2), result)
|
||||
})
|
||||
|
||||
t.Run("left Left, right Right", func(t *testing.T) {
|
||||
result := m.Concat(Left[int](errors.New("err")), Right(3))
|
||||
assert.Equal(t, Right(3), result)
|
||||
})
|
||||
|
||||
t.Run("both Left", func(t *testing.T) {
|
||||
err1 := errors.New("err1")
|
||||
err2 := errors.New("err2")
|
||||
result := m.Concat(Left[int](err1), Left[int](err2))
|
||||
// Should return the second Left
|
||||
assert.True(t, IsLeft(result))
|
||||
_, leftErr := Unwrap(result)
|
||||
assert.Equal(t, err2, leftErr)
|
||||
})
|
||||
|
||||
t.Run("empty value", func(t *testing.T) {
|
||||
empty := m.Empty()
|
||||
assert.True(t, IsLeft(empty))
|
||||
_, leftErr := Unwrap(empty)
|
||||
assert.Equal(t, "empty", leftErr.Error())
|
||||
})
|
||||
|
||||
t.Run("left identity", func(t *testing.T) {
|
||||
x := Right(5)
|
||||
result := m.Concat(m.Empty(), x)
|
||||
assert.Equal(t, x, result)
|
||||
})
|
||||
|
||||
t.Run("right identity", func(t *testing.T) {
|
||||
x := Right(5)
|
||||
result := m.Concat(x, m.Empty())
|
||||
assert.Equal(t, x, result)
|
||||
})
|
||||
|
||||
t.Run("associativity", func(t *testing.T) {
|
||||
a := Right(1)
|
||||
b := Left[int](errors.New("err"))
|
||||
c := Right(3)
|
||||
|
||||
left := m.Concat(m.Concat(a, b), c)
|
||||
right := m.Concat(a, m.Concat(b, c))
|
||||
|
||||
assert.Equal(t, left, right)
|
||||
assert.Equal(t, Right(1), left)
|
||||
})
|
||||
}
|
||||
|
||||
// TestFirstMonoidVsAltMonoid verifies FirstMonoid and AltMonoid have the same behavior
|
||||
func TestFirstMonoidVsAltMonoid(t *testing.T) {
|
||||
zero := func() Result[int] { return Left[int](errors.New("empty")) }
|
||||
firstMonoid := FirstMonoid(zero)
|
||||
altMonoid := AltMonoid(zero)
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
left Result[int]
|
||||
right Result[int]
|
||||
}{
|
||||
{"both Right", Right(1), Right(2)},
|
||||
{"left Right, right Left", Right(1), Left[int](errors.New("err"))},
|
||||
{"left Left, right Right", Left[int](errors.New("err")), Right(2)},
|
||||
{"both Left", Left[int](errors.New("err1")), Left[int](errors.New("err2"))},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
firstResult := firstMonoid.Concat(tc.left, tc.right)
|
||||
altResult := altMonoid.Concat(tc.left, tc.right)
|
||||
|
||||
// Both should have the same Right/Left status
|
||||
assert.Equal(t, IsRight(firstResult), IsRight(altResult), "FirstMonoid and AltMonoid should have same Right/Left status")
|
||||
|
||||
if IsRight(firstResult) {
|
||||
rightVal1, _ := Unwrap(firstResult)
|
||||
rightVal2, _ := Unwrap(altResult)
|
||||
assert.Equal(t, rightVal1, rightVal2, "FirstMonoid and AltMonoid should have same Right value")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestFirstMonoidVsLastMonoid verifies the difference between FirstMonoid and LastMonoid
|
||||
func TestFirstMonoidVsLastMonoid(t *testing.T) {
|
||||
zero := func() Result[int] { return Left[int](errors.New("empty")) }
|
||||
firstMonoid := FirstMonoid(zero)
|
||||
lastMonoid := LastMonoid(zero)
|
||||
|
||||
t.Run("both Right - different results", func(t *testing.T) {
|
||||
firstResult := firstMonoid.Concat(Right(1), Right(2))
|
||||
lastResult := lastMonoid.Concat(Right(1), Right(2))
|
||||
|
||||
assert.Equal(t, Right(1), firstResult)
|
||||
assert.Equal(t, Right(2), lastResult)
|
||||
assert.NotEqual(t, firstResult, lastResult)
|
||||
})
|
||||
|
||||
t.Run("with Left values - different behavior", func(t *testing.T) {
|
||||
err1 := errors.New("err1")
|
||||
err2 := errors.New("err2")
|
||||
|
||||
// Both Left: FirstMonoid returns second, LastMonoid returns first
|
||||
firstResult := firstMonoid.Concat(Left[int](err1), Left[int](err2))
|
||||
lastResult := lastMonoid.Concat(Left[int](err1), Left[int](err2))
|
||||
|
||||
assert.True(t, IsLeft(firstResult))
|
||||
assert.True(t, IsLeft(lastResult))
|
||||
_, leftErr1 := Unwrap(firstResult)
|
||||
_, leftErr2 := Unwrap(lastResult)
|
||||
assert.Equal(t, err2, leftErr1)
|
||||
assert.Equal(t, err1, leftErr2)
|
||||
})
|
||||
|
||||
t.Run("mixed values - same results", func(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
left Result[int]
|
||||
right Result[int]
|
||||
expected Result[int]
|
||||
}{
|
||||
{"left Right, right Left", Right(1), Left[int](errors.New("err")), Right(1)},
|
||||
{"left Left, right Right", Left[int](errors.New("err")), Right(2), Right(2)},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
firstResult := firstMonoid.Concat(tc.left, tc.right)
|
||||
lastResult := lastMonoid.Concat(tc.left, tc.right)
|
||||
|
||||
assert.Equal(t, tc.expected, firstResult)
|
||||
assert.Equal(t, tc.expected, lastResult)
|
||||
assert.Equal(t, firstResult, lastResult)
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// TestMonoidLaws verifies monoid laws for all monoid implementations
|
||||
func TestMonoidLaws(t *testing.T) {
|
||||
t.Run("FirstMonoid laws", func(t *testing.T) {
|
||||
zero := func() Result[int] { return Left[int](errors.New("empty")) }
|
||||
m := FirstMonoid(zero)
|
||||
|
||||
a := Right(1)
|
||||
b := Right(2)
|
||||
c := Right(3)
|
||||
|
||||
// Associativity: (a • b) • c = a • (b • c)
|
||||
left := m.Concat(m.Concat(a, b), c)
|
||||
right := m.Concat(a, m.Concat(b, c))
|
||||
assert.Equal(t, left, right)
|
||||
|
||||
// Left identity: Empty() • a = a
|
||||
leftId := m.Concat(m.Empty(), a)
|
||||
assert.Equal(t, a, leftId)
|
||||
|
||||
// Right identity: a • Empty() = a
|
||||
rightId := m.Concat(a, m.Empty())
|
||||
assert.Equal(t, a, rightId)
|
||||
})
|
||||
|
||||
t.Run("LastMonoid laws", func(t *testing.T) {
|
||||
zero := func() Result[int] { return Left[int](errors.New("empty")) }
|
||||
m := LastMonoid(zero)
|
||||
|
||||
a := Right(1)
|
||||
b := Right(2)
|
||||
c := Right(3)
|
||||
|
||||
// Associativity: (a • b) • c = a • (b • c)
|
||||
left := m.Concat(m.Concat(a, b), c)
|
||||
right := m.Concat(a, m.Concat(b, c))
|
||||
assert.Equal(t, left, right)
|
||||
|
||||
// Left identity: Empty() • a = a
|
||||
leftId := m.Concat(m.Empty(), a)
|
||||
assert.Equal(t, a, leftId)
|
||||
|
||||
// Right identity: a • Empty() = a
|
||||
rightId := m.Concat(a, m.Empty())
|
||||
assert.Equal(t, a, rightId)
|
||||
})
|
||||
|
||||
t.Run("AltMonoid laws", func(t *testing.T) {
|
||||
zero := func() Result[int] { return Left[int](errors.New("empty")) }
|
||||
m := AltMonoid(zero)
|
||||
|
||||
a := Right(1)
|
||||
b := Right(2)
|
||||
c := Right(3)
|
||||
|
||||
// Associativity: (a • b) • c = a • (b • c)
|
||||
left := m.Concat(m.Concat(a, b), c)
|
||||
right := m.Concat(a, m.Concat(b, c))
|
||||
assert.Equal(t, left, right)
|
||||
|
||||
// Left identity: Empty() • a = a
|
||||
leftId := m.Concat(m.Empty(), a)
|
||||
assert.Equal(t, a, leftId)
|
||||
|
||||
// Right identity: a • Empty() = a
|
||||
rightId := m.Concat(a, m.Empty())
|
||||
assert.Equal(t, a, rightId)
|
||||
})
|
||||
|
||||
t.Run("FirstMonoid laws with Left values", func(t *testing.T) {
|
||||
zero := func() Result[int] { return Left[int](errors.New("empty")) }
|
||||
m := FirstMonoid(zero)
|
||||
|
||||
a := Left[int](errors.New("err1"))
|
||||
b := Left[int](errors.New("err2"))
|
||||
c := Left[int](errors.New("err3"))
|
||||
|
||||
// Associativity with Left values
|
||||
left := m.Concat(m.Concat(a, b), c)
|
||||
right := m.Concat(a, m.Concat(b, c))
|
||||
assert.Equal(t, left, right)
|
||||
})
|
||||
|
||||
t.Run("LastMonoid laws with Left values", func(t *testing.T) {
|
||||
zero := func() Result[int] { return Left[int](errors.New("empty")) }
|
||||
m := LastMonoid(zero)
|
||||
|
||||
a := Left[int](errors.New("err1"))
|
||||
b := Left[int](errors.New("err2"))
|
||||
c := Left[int](errors.New("err3"))
|
||||
|
||||
// Associativity with Left values
|
||||
left := m.Concat(m.Concat(a, b), c)
|
||||
right := m.Concat(a, m.Concat(b, c))
|
||||
assert.Equal(t, left, right)
|
||||
})
|
||||
}
|
||||
|
||||
// TestMonoidEdgeCases tests edge cases for monoid operations
|
||||
func TestMonoidEdgeCases(t *testing.T) {
|
||||
t.Run("FirstMonoid with empty concatenations", func(t *testing.T) {
|
||||
zero := func() Result[int] { return Left[int](errors.New("empty")) }
|
||||
m := FirstMonoid(zero)
|
||||
|
||||
// Empty with empty
|
||||
result := m.Concat(m.Empty(), m.Empty())
|
||||
assert.True(t, IsLeft(result))
|
||||
})
|
||||
|
||||
t.Run("LastMonoid with empty concatenations", func(t *testing.T) {
|
||||
zero := func() Result[int] { return Left[int](errors.New("empty")) }
|
||||
m := LastMonoid(zero)
|
||||
|
||||
// Empty with empty
|
||||
result := m.Concat(m.Empty(), m.Empty())
|
||||
assert.True(t, IsLeft(result))
|
||||
})
|
||||
|
||||
t.Run("FirstMonoid chain of operations", func(t *testing.T) {
|
||||
zero := func() Result[int] { return Left[int](errors.New("empty")) }
|
||||
m := FirstMonoid(zero)
|
||||
|
||||
// Chain multiple operations
|
||||
result := m.Concat(
|
||||
m.Concat(
|
||||
m.Concat(Left[int](errors.New("err1")), Left[int](errors.New("err2"))),
|
||||
Right(1),
|
||||
),
|
||||
m.Concat(Right(2), Right(3)),
|
||||
)
|
||||
assert.Equal(t, Right(1), result)
|
||||
})
|
||||
|
||||
t.Run("LastMonoid chain of operations", func(t *testing.T) {
|
||||
zero := func() Result[int] { return Left[int](errors.New("empty")) }
|
||||
m := LastMonoid(zero)
|
||||
|
||||
// Chain multiple operations
|
||||
result := m.Concat(
|
||||
m.Concat(Right(1), Right(2)),
|
||||
m.Concat(
|
||||
Right(3),
|
||||
m.Concat(Right(4), Left[int](errors.New("err"))),
|
||||
),
|
||||
)
|
||||
assert.Equal(t, Right(4), result)
|
||||
})
|
||||
|
||||
t.Run("AltMonoid chain of operations", func(t *testing.T) {
|
||||
zero := func() Result[int] { return Left[int](errors.New("empty")) }
|
||||
m := AltMonoid(zero)
|
||||
|
||||
// Chain multiple operations - should return first Right
|
||||
result := m.Concat(
|
||||
m.Concat(Left[int](errors.New("err1")), Right(1)),
|
||||
m.Concat(Right(2), Right(3)),
|
||||
)
|
||||
assert.Equal(t, Right(1), result)
|
||||
})
|
||||
}
|
||||
@@ -89,7 +89,7 @@ FunctionSemigroup - Lifts a semigroup to work with functions:
|
||||
// Lift to functions that return integers
|
||||
funcSG := SG.FunctionSemigroup[string](intSum)
|
||||
|
||||
f := func(s string) int { return len(s) }
|
||||
f := S.Size
|
||||
g := func(s string) int { return len(s) * 2 }
|
||||
|
||||
// Combine functions
|
||||
|
||||
@@ -78,7 +78,7 @@ func Reverse[A any](m Semigroup[A]) Semigroup[A] {
|
||||
// intSum := N.SemigroupSum[int]()
|
||||
// funcSG := semigroup.FunctionSemigroup[string](intSum)
|
||||
//
|
||||
// f := func(s string) int { return len(s) }
|
||||
// f := S.Size
|
||||
// g := func(s string) int { return len(s) * 2 }
|
||||
// combined := funcSG.Concat(f, g)
|
||||
// result := combined("hello") // 5 + 10 = 15
|
||||
|
||||
@@ -566,5 +566,3 @@ func TestMixedBindAndLet(t *testing.T) {
|
||||
assert.Equal(t, 10, pair.Tail(result).Doubled, "doubled should be 10")
|
||||
assert.Equal(t, "done", pair.Tail(result).Status, "status should be done")
|
||||
}
|
||||
|
||||
// Made with Bob
|
||||
|
||||
147
v2/state/monoid.go
Normal file
147
v2/state/monoid.go
Normal file
@@ -0,0 +1,147 @@
|
||||
// Copyright (c) 2024 - 2025 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 state
|
||||
|
||||
import (
|
||||
M "github.com/IBM/fp-go/v2/monoid"
|
||||
)
|
||||
|
||||
// ApplicativeMonoid lifts a monoid into the State applicative functor context.
|
||||
//
|
||||
// This function creates a monoid for State[S, A] values given a monoid for the base type A.
|
||||
// It uses the State monad's applicative operations (Of, MonadMap, MonadAp) to lift the
|
||||
// monoid operations into the State context, allowing you to combine stateful computations
|
||||
// that produce monoidal values.
|
||||
//
|
||||
// The resulting monoid combines State computations by:
|
||||
// 1. Threading the state through both computations sequentially
|
||||
// 2. Combining the produced values using the underlying monoid's Concat operation
|
||||
// 3. Returning a new State computation with the combined value and final state
|
||||
//
|
||||
// The empty element is a State computation that returns the underlying monoid's empty value
|
||||
// without modifying the state.
|
||||
//
|
||||
// This is particularly useful for:
|
||||
// - Accumulating results across multiple stateful computations
|
||||
// - Building complex state transformations that aggregate values
|
||||
// - Combining independent stateful operations that produce monoidal results
|
||||
//
|
||||
// Type Parameters:
|
||||
// - S: The state type that is threaded through the computations
|
||||
// - A: The value type that has a monoid structure
|
||||
//
|
||||
// Parameters:
|
||||
// - m: A monoid for the base type A that defines how to combine values
|
||||
//
|
||||
// Returns:
|
||||
// - A Monoid[State[S, A]] that combines stateful computations using the base monoid
|
||||
//
|
||||
// The resulting monoid satisfies the standard monoid laws:
|
||||
// - Associativity: Concat(Concat(s1, s2), s3) = Concat(s1, Concat(s2, s3))
|
||||
// - Left identity: Concat(Empty(), s) = s
|
||||
// - Right identity: Concat(s, Empty()) = s
|
||||
//
|
||||
// Example with integer addition:
|
||||
//
|
||||
// import (
|
||||
// N "github.com/IBM/fp-go/v2/number"
|
||||
// "github.com/IBM/fp-go/v2/pair"
|
||||
// )
|
||||
//
|
||||
// type Counter struct {
|
||||
// count int
|
||||
// }
|
||||
//
|
||||
// // Create a monoid for State[Counter, int] using integer addition
|
||||
// intAddMonoid := N.MonoidSum[int]()
|
||||
// stateMonoid := state.ApplicativeMonoid[Counter](intAddMonoid)
|
||||
//
|
||||
// // Create two stateful computations
|
||||
// s1 := state.Of[Counter](5) // Returns 5, state unchanged
|
||||
// s2 := state.Of[Counter](3) // Returns 3, state unchanged
|
||||
//
|
||||
// // Combine them using the monoid
|
||||
// combined := stateMonoid.Concat(s1, s2)
|
||||
// result := combined(Counter{count: 10})
|
||||
// // result = Pair{head: Counter{count: 10}, tail: 8} // 5 + 3
|
||||
//
|
||||
// // Empty element
|
||||
// empty := stateMonoid.Empty()
|
||||
// emptyResult := empty(Counter{count: 10})
|
||||
// // emptyResult = Pair{head: Counter{count: 10}, tail: 0}
|
||||
//
|
||||
// Example with string concatenation and state modification:
|
||||
//
|
||||
// import (
|
||||
// S "github.com/IBM/fp-go/v2/string"
|
||||
// )
|
||||
//
|
||||
// type Logger struct {
|
||||
// logs []string
|
||||
// }
|
||||
//
|
||||
// strMonoid := S.Monoid
|
||||
// stateMonoid := state.ApplicativeMonoid[Logger](strMonoid)
|
||||
//
|
||||
// // Stateful computation that logs and returns a message
|
||||
// logMessage := func(msg string) state.State[Logger, string] {
|
||||
// return func(s Logger) pair.Pair[Logger, string] {
|
||||
// newState := Logger{logs: append(s.logs, msg)}
|
||||
// return pair.MakePair(newState, msg)
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// s1 := logMessage("Hello")
|
||||
// s2 := logMessage(" World")
|
||||
//
|
||||
// // Combine the computations - both log entries are added, messages concatenated
|
||||
// combined := stateMonoid.Concat(s1, s2)
|
||||
// result := combined(Logger{logs: []string{}})
|
||||
// // result.head.logs = ["Hello", " World"]
|
||||
// // result.tail = "Hello World"
|
||||
//
|
||||
// Example demonstrating monoid laws:
|
||||
//
|
||||
// intAddMonoid := N.MonoidSum[int]()
|
||||
// m := state.ApplicativeMonoid[Counter](intAddMonoid)
|
||||
//
|
||||
// s1 := state.Of[Counter](1)
|
||||
// s2 := state.Of[Counter](2)
|
||||
// s3 := state.Of[Counter](3)
|
||||
//
|
||||
// initialState := Counter{count: 0}
|
||||
//
|
||||
// // Associativity
|
||||
// left := m.Concat(m.Concat(s1, s2), s3)
|
||||
// right := m.Concat(s1, m.Concat(s2, s3))
|
||||
// // Both produce: Pair{head: Counter{count: 0}, tail: 6}
|
||||
//
|
||||
// // Left identity
|
||||
// leftId := m.Concat(m.Empty(), s1)
|
||||
// // Produces: Pair{head: Counter{count: 0}, tail: 1}
|
||||
//
|
||||
// // Right identity
|
||||
// rightId := m.Concat(s1, m.Empty())
|
||||
// // Produces: Pair{head: Counter{count: 0}, tail: 1}
|
||||
//
|
||||
//go:inline
|
||||
func ApplicativeMonoid[S, A any](m M.Monoid[A]) M.Monoid[State[S, A]] {
|
||||
return M.ApplicativeMonoid(
|
||||
Of[S, A],
|
||||
MonadMap[S, func(A) func(A) A, A, func(A) A],
|
||||
MonadAp[A, S, A],
|
||||
m)
|
||||
}
|
||||
552
v2/state/monoid_test.go
Normal file
552
v2/state/monoid_test.go
Normal file
@@ -0,0 +1,552 @@
|
||||
// Copyright (c) 2024 - 2025 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 state
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
M "github.com/IBM/fp-go/v2/monoid"
|
||||
N "github.com/IBM/fp-go/v2/number"
|
||||
"github.com/IBM/fp-go/v2/pair"
|
||||
S "github.com/IBM/fp-go/v2/string"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// Counter is a simple state type for testing
|
||||
type Counter struct {
|
||||
count int
|
||||
}
|
||||
|
||||
// Logger is a state type that accumulates log messages
|
||||
type Logger struct {
|
||||
logs []string
|
||||
}
|
||||
|
||||
// TestApplicativeMonoidBasic tests basic monoid operations
|
||||
func TestApplicativeMonoidBasic(t *testing.T) {
|
||||
t.Run("integer addition", func(t *testing.T) {
|
||||
intAddMonoid := N.MonoidSum[int]()
|
||||
stateMonoid := ApplicativeMonoid[Counter](intAddMonoid)
|
||||
|
||||
s1 := Of[Counter](5)
|
||||
s2 := Of[Counter](3)
|
||||
|
||||
combined := stateMonoid.Concat(s1, s2)
|
||||
result := combined(Counter{count: 10})
|
||||
|
||||
assert.Equal(t, Counter{count: 10}, pair.Head(result))
|
||||
assert.Equal(t, 8, pair.Tail(result))
|
||||
})
|
||||
|
||||
t.Run("integer multiplication", func(t *testing.T) {
|
||||
intMulMonoid := N.MonoidProduct[int]()
|
||||
stateMonoid := ApplicativeMonoid[Counter](intMulMonoid)
|
||||
|
||||
s1 := Of[Counter](4)
|
||||
s2 := Of[Counter](5)
|
||||
|
||||
combined := stateMonoid.Concat(s1, s2)
|
||||
result := combined(Counter{count: 0})
|
||||
|
||||
assert.Equal(t, Counter{count: 0}, pair.Head(result))
|
||||
assert.Equal(t, 20, pair.Tail(result))
|
||||
})
|
||||
|
||||
t.Run("string concatenation", func(t *testing.T) {
|
||||
strMonoid := S.Monoid
|
||||
stateMonoid := ApplicativeMonoid[Counter](strMonoid)
|
||||
|
||||
s1 := Of[Counter]("Hello")
|
||||
s2 := Of[Counter](" World")
|
||||
|
||||
combined := stateMonoid.Concat(s1, s2)
|
||||
result := combined(Counter{count: 5})
|
||||
|
||||
assert.Equal(t, Counter{count: 5}, pair.Head(result))
|
||||
assert.Equal(t, "Hello World", pair.Tail(result))
|
||||
})
|
||||
|
||||
t.Run("boolean AND", func(t *testing.T) {
|
||||
boolAndMonoid := M.MakeMonoid(func(a, b bool) bool { return a && b }, true)
|
||||
stateMonoid := ApplicativeMonoid[Counter](boolAndMonoid)
|
||||
|
||||
s1 := Of[Counter](true)
|
||||
s2 := Of[Counter](true)
|
||||
|
||||
combined := stateMonoid.Concat(s1, s2)
|
||||
result := combined(Counter{count: 0})
|
||||
|
||||
assert.Equal(t, true, pair.Tail(result))
|
||||
|
||||
s3 := Of[Counter](false)
|
||||
combined2 := stateMonoid.Concat(s1, s3)
|
||||
result2 := combined2(Counter{count: 0})
|
||||
|
||||
assert.Equal(t, false, pair.Tail(result2))
|
||||
})
|
||||
}
|
||||
|
||||
// TestApplicativeMonoidEmpty tests the empty element
|
||||
func TestApplicativeMonoidEmpty(t *testing.T) {
|
||||
t.Run("integer addition empty", func(t *testing.T) {
|
||||
intAddMonoid := N.MonoidSum[int]()
|
||||
stateMonoid := ApplicativeMonoid[Counter](intAddMonoid)
|
||||
|
||||
empty := stateMonoid.Empty()
|
||||
result := empty(Counter{count: 5})
|
||||
|
||||
assert.Equal(t, Counter{count: 5}, pair.Head(result))
|
||||
assert.Equal(t, 0, pair.Tail(result))
|
||||
})
|
||||
|
||||
t.Run("integer multiplication empty", func(t *testing.T) {
|
||||
intMulMonoid := N.MonoidProduct[int]()
|
||||
stateMonoid := ApplicativeMonoid[Counter](intMulMonoid)
|
||||
|
||||
empty := stateMonoid.Empty()
|
||||
result := empty(Counter{count: 10})
|
||||
|
||||
assert.Equal(t, Counter{count: 10}, pair.Head(result))
|
||||
assert.Equal(t, 1, pair.Tail(result))
|
||||
})
|
||||
|
||||
t.Run("string concatenation empty", func(t *testing.T) {
|
||||
strMonoid := S.Monoid
|
||||
stateMonoid := ApplicativeMonoid[Counter](strMonoid)
|
||||
|
||||
empty := stateMonoid.Empty()
|
||||
result := empty(Counter{count: 0})
|
||||
|
||||
assert.Equal(t, Counter{count: 0}, pair.Head(result))
|
||||
assert.Equal(t, "", pair.Tail(result))
|
||||
})
|
||||
}
|
||||
|
||||
// TestApplicativeMonoidLaws verifies monoid laws
|
||||
func TestApplicativeMonoidLaws(t *testing.T) {
|
||||
t.Run("associativity with integer addition", func(t *testing.T) {
|
||||
intAddMonoid := N.MonoidSum[int]()
|
||||
m := ApplicativeMonoid[Counter](intAddMonoid)
|
||||
|
||||
s1 := Of[Counter](1)
|
||||
s2 := Of[Counter](2)
|
||||
s3 := Of[Counter](3)
|
||||
|
||||
initialState := Counter{count: 0}
|
||||
|
||||
// (s1 • s2) • s3
|
||||
left := m.Concat(m.Concat(s1, s2), s3)
|
||||
leftResult := left(initialState)
|
||||
|
||||
// s1 • (s2 • s3)
|
||||
right := m.Concat(s1, m.Concat(s2, s3))
|
||||
rightResult := right(initialState)
|
||||
|
||||
assert.Equal(t, leftResult, rightResult)
|
||||
assert.Equal(t, 6, pair.Tail(leftResult))
|
||||
})
|
||||
|
||||
t.Run("left identity with integer addition", func(t *testing.T) {
|
||||
intAddMonoid := N.MonoidSum[int]()
|
||||
m := ApplicativeMonoid[Counter](intAddMonoid)
|
||||
|
||||
s := Of[Counter](42)
|
||||
initialState := Counter{count: 5}
|
||||
|
||||
// Empty() • s = s
|
||||
result := m.Concat(m.Empty(), s)
|
||||
expected := s(initialState)
|
||||
actual := result(initialState)
|
||||
|
||||
assert.Equal(t, expected, actual)
|
||||
assert.Equal(t, 42, pair.Tail(actual))
|
||||
})
|
||||
|
||||
t.Run("right identity with integer addition", func(t *testing.T) {
|
||||
intAddMonoid := N.MonoidSum[int]()
|
||||
m := ApplicativeMonoid[Counter](intAddMonoid)
|
||||
|
||||
s := Of[Counter](42)
|
||||
initialState := Counter{count: 5}
|
||||
|
||||
// s • Empty() = s
|
||||
result := m.Concat(s, m.Empty())
|
||||
expected := s(initialState)
|
||||
actual := result(initialState)
|
||||
|
||||
assert.Equal(t, expected, actual)
|
||||
assert.Equal(t, 42, pair.Tail(actual))
|
||||
})
|
||||
|
||||
t.Run("associativity with string concatenation", func(t *testing.T) {
|
||||
strMonoid := S.Monoid
|
||||
m := ApplicativeMonoid[Counter](strMonoid)
|
||||
|
||||
s1 := Of[Counter]("a")
|
||||
s2 := Of[Counter]("b")
|
||||
s3 := Of[Counter]("c")
|
||||
|
||||
initialState := Counter{count: 0}
|
||||
|
||||
left := m.Concat(m.Concat(s1, s2), s3)
|
||||
leftResult := left(initialState)
|
||||
|
||||
right := m.Concat(s1, m.Concat(s2, s3))
|
||||
rightResult := right(initialState)
|
||||
|
||||
assert.Equal(t, leftResult, rightResult)
|
||||
assert.Equal(t, "abc", pair.Tail(leftResult))
|
||||
})
|
||||
|
||||
t.Run("left identity with string concatenation", func(t *testing.T) {
|
||||
strMonoid := S.Monoid
|
||||
m := ApplicativeMonoid[Counter](strMonoid)
|
||||
|
||||
s := Of[Counter]("test")
|
||||
initialState := Counter{count: 0}
|
||||
|
||||
result := m.Concat(m.Empty(), s)
|
||||
expected := s(initialState)
|
||||
actual := result(initialState)
|
||||
|
||||
assert.Equal(t, expected, actual)
|
||||
assert.Equal(t, "test", pair.Tail(actual))
|
||||
})
|
||||
|
||||
t.Run("right identity with string concatenation", func(t *testing.T) {
|
||||
strMonoid := S.Monoid
|
||||
m := ApplicativeMonoid[Counter](strMonoid)
|
||||
|
||||
s := Of[Counter]("test")
|
||||
initialState := Counter{count: 0}
|
||||
|
||||
result := m.Concat(s, m.Empty())
|
||||
expected := s(initialState)
|
||||
actual := result(initialState)
|
||||
|
||||
assert.Equal(t, expected, actual)
|
||||
assert.Equal(t, "test", pair.Tail(actual))
|
||||
})
|
||||
}
|
||||
|
||||
// TestApplicativeMonoidWithStateModification tests monoid with state-modifying computations
|
||||
func TestApplicativeMonoidWithStateModification(t *testing.T) {
|
||||
t.Run("state modification with integer addition", func(t *testing.T) {
|
||||
intAddMonoid := N.MonoidSum[int]()
|
||||
stateMonoid := ApplicativeMonoid[Counter](intAddMonoid)
|
||||
|
||||
// Computation that increments counter and returns a value
|
||||
incrementAndReturn := func(val int) State[Counter, int] {
|
||||
return func(s Counter) Pair[Counter, int] {
|
||||
return pair.MakePair(Counter{count: s.count + 1}, val)
|
||||
}
|
||||
}
|
||||
|
||||
s1 := incrementAndReturn(5)
|
||||
s2 := incrementAndReturn(3)
|
||||
|
||||
combined := stateMonoid.Concat(s1, s2)
|
||||
result := combined(Counter{count: 0})
|
||||
|
||||
// State should be incremented twice
|
||||
assert.Equal(t, Counter{count: 2}, pair.Head(result))
|
||||
// Values should be added
|
||||
assert.Equal(t, 8, pair.Tail(result))
|
||||
})
|
||||
|
||||
t.Run("state modification with string concatenation", func(t *testing.T) {
|
||||
strMonoid := S.Monoid
|
||||
stateMonoid := ApplicativeMonoid[Logger](strMonoid)
|
||||
|
||||
// Computation that logs a message and returns it
|
||||
logMessage := func(msg string) State[Logger, string] {
|
||||
return func(s Logger) Pair[Logger, string] {
|
||||
newLogs := append(s.logs, msg)
|
||||
return pair.MakePair(Logger{logs: newLogs}, msg)
|
||||
}
|
||||
}
|
||||
|
||||
s1 := logMessage("Hello")
|
||||
s2 := logMessage(" World")
|
||||
|
||||
combined := stateMonoid.Concat(s1, s2)
|
||||
result := combined(Logger{logs: []string{}})
|
||||
|
||||
// Both messages should be logged
|
||||
assert.Equal(t, []string{"Hello", " World"}, pair.Head(result).logs)
|
||||
// Messages should be concatenated
|
||||
assert.Equal(t, "Hello World", pair.Tail(result))
|
||||
})
|
||||
|
||||
t.Run("complex state transformation", func(t *testing.T) {
|
||||
intAddMonoid := N.MonoidSum[int]()
|
||||
stateMonoid := ApplicativeMonoid[Counter](intAddMonoid)
|
||||
|
||||
// Computation that doubles the counter and returns the old value
|
||||
doubleAndReturnOld := func(val int) State[Counter, int] {
|
||||
return func(s Counter) Pair[Counter, int] {
|
||||
return pair.MakePair(Counter{count: s.count * 2}, val)
|
||||
}
|
||||
}
|
||||
|
||||
s1 := doubleAndReturnOld(10)
|
||||
s2 := doubleAndReturnOld(20)
|
||||
|
||||
combined := stateMonoid.Concat(s1, s2)
|
||||
result := combined(Counter{count: 3})
|
||||
|
||||
// State: 3 -> 6 -> 12
|
||||
assert.Equal(t, Counter{count: 12}, pair.Head(result))
|
||||
// Values: 10 + 20 = 30
|
||||
assert.Equal(t, 30, pair.Tail(result))
|
||||
})
|
||||
}
|
||||
|
||||
// TestApplicativeMonoidMultipleConcatenations tests chaining multiple concatenations
|
||||
func TestApplicativeMonoidMultipleConcatenations(t *testing.T) {
|
||||
t.Run("chain of integer additions", func(t *testing.T) {
|
||||
intAddMonoid := N.MonoidSum[int]()
|
||||
m := ApplicativeMonoid[Counter](intAddMonoid)
|
||||
|
||||
states := []State[Counter, int]{
|
||||
Of[Counter](1),
|
||||
Of[Counter](2),
|
||||
Of[Counter](3),
|
||||
Of[Counter](4),
|
||||
Of[Counter](5),
|
||||
}
|
||||
|
||||
// Fold all states using the monoid
|
||||
result := m.Empty()
|
||||
for _, s := range states {
|
||||
result = m.Concat(result, s)
|
||||
}
|
||||
|
||||
finalResult := result(Counter{count: 0})
|
||||
assert.Equal(t, Counter{count: 0}, pair.Head(finalResult))
|
||||
assert.Equal(t, 15, pair.Tail(finalResult)) // 1+2+3+4+5
|
||||
})
|
||||
|
||||
t.Run("chain of string concatenations", func(t *testing.T) {
|
||||
strMonoid := S.Monoid
|
||||
m := ApplicativeMonoid[Counter](strMonoid)
|
||||
|
||||
words := []string{"The", " quick", " brown", " fox"}
|
||||
states := make([]State[Counter, string], len(words))
|
||||
for i, word := range words {
|
||||
states[i] = Of[Counter](word)
|
||||
}
|
||||
|
||||
result := m.Empty()
|
||||
for _, s := range states {
|
||||
result = m.Concat(result, s)
|
||||
}
|
||||
|
||||
finalResult := result(Counter{count: 0})
|
||||
assert.Equal(t, "The quick brown fox", pair.Tail(finalResult))
|
||||
})
|
||||
|
||||
t.Run("nested concatenations", func(t *testing.T) {
|
||||
intAddMonoid := N.MonoidSum[int]()
|
||||
m := ApplicativeMonoid[Counter](intAddMonoid)
|
||||
|
||||
s1 := Of[Counter](1)
|
||||
s2 := Of[Counter](2)
|
||||
s3 := Of[Counter](3)
|
||||
s4 := Of[Counter](4)
|
||||
|
||||
// ((s1 • s2) • s3) • s4
|
||||
result := m.Concat(
|
||||
m.Concat(
|
||||
m.Concat(s1, s2),
|
||||
s3,
|
||||
),
|
||||
s4,
|
||||
)
|
||||
|
||||
finalResult := result(Counter{count: 0})
|
||||
assert.Equal(t, 10, pair.Tail(finalResult))
|
||||
})
|
||||
}
|
||||
|
||||
// TestApplicativeMonoidEdgeCases tests edge cases
|
||||
func TestApplicativeMonoidEdgeCases(t *testing.T) {
|
||||
t.Run("concatenating empty with empty", func(t *testing.T) {
|
||||
intAddMonoid := N.MonoidSum[int]()
|
||||
m := ApplicativeMonoid[Counter](intAddMonoid)
|
||||
|
||||
result := m.Concat(m.Empty(), m.Empty())
|
||||
finalResult := result(Counter{count: 5})
|
||||
|
||||
assert.Equal(t, Counter{count: 5}, pair.Head(finalResult))
|
||||
assert.Equal(t, 0, pair.Tail(finalResult))
|
||||
})
|
||||
|
||||
t.Run("zero values with multiplication", func(t *testing.T) {
|
||||
intMulMonoid := N.MonoidProduct[int]()
|
||||
m := ApplicativeMonoid[Counter](intMulMonoid)
|
||||
|
||||
s1 := Of[Counter](0)
|
||||
s2 := Of[Counter](42)
|
||||
|
||||
result := m.Concat(s1, s2)
|
||||
finalResult := result(Counter{count: 0})
|
||||
|
||||
assert.Equal(t, 0, pair.Tail(finalResult))
|
||||
})
|
||||
|
||||
t.Run("empty strings", func(t *testing.T) {
|
||||
strMonoid := S.Monoid
|
||||
m := ApplicativeMonoid[Counter](strMonoid)
|
||||
|
||||
s1 := Of[Counter]("")
|
||||
s2 := Of[Counter]("test")
|
||||
|
||||
result := m.Concat(s1, s2)
|
||||
finalResult := result(Counter{count: 0})
|
||||
|
||||
assert.Equal(t, "test", pair.Tail(finalResult))
|
||||
})
|
||||
|
||||
t.Run("negative numbers with addition", func(t *testing.T) {
|
||||
intAddMonoid := N.MonoidSum[int]()
|
||||
m := ApplicativeMonoid[Counter](intAddMonoid)
|
||||
|
||||
s1 := Of[Counter](-5)
|
||||
s2 := Of[Counter](3)
|
||||
|
||||
result := m.Concat(s1, s2)
|
||||
finalResult := result(Counter{count: 0})
|
||||
|
||||
assert.Equal(t, -2, pair.Tail(finalResult))
|
||||
})
|
||||
}
|
||||
|
||||
// TestApplicativeMonoidWithDifferentTypes tests monoid with various type combinations
|
||||
func TestApplicativeMonoidWithDifferentTypes(t *testing.T) {
|
||||
t.Run("float64 addition", func(t *testing.T) {
|
||||
floatAddMonoid := N.MonoidSum[float64]()
|
||||
m := ApplicativeMonoid[Counter](floatAddMonoid)
|
||||
|
||||
s1 := Of[Counter](1.5)
|
||||
s2 := Of[Counter](2.5)
|
||||
|
||||
result := m.Concat(s1, s2)
|
||||
finalResult := result(Counter{count: 0})
|
||||
|
||||
assert.Equal(t, 4.0, pair.Tail(finalResult))
|
||||
})
|
||||
|
||||
t.Run("float64 multiplication", func(t *testing.T) {
|
||||
floatMulMonoid := N.MonoidProduct[float64]()
|
||||
m := ApplicativeMonoid[Counter](floatMulMonoid)
|
||||
|
||||
s1 := Of[Counter](2.0)
|
||||
s2 := Of[Counter](3.5)
|
||||
|
||||
result := m.Concat(s1, s2)
|
||||
finalResult := result(Counter{count: 0})
|
||||
|
||||
assert.Equal(t, 7.0, pair.Tail(finalResult))
|
||||
})
|
||||
|
||||
t.Run("boolean OR", func(t *testing.T) {
|
||||
boolOrMonoid := M.MakeMonoid(func(a, b bool) bool { return a || b }, false)
|
||||
m := ApplicativeMonoid[Counter](boolOrMonoid)
|
||||
|
||||
s1 := Of[Counter](false)
|
||||
s2 := Of[Counter](true)
|
||||
|
||||
result := m.Concat(s1, s2)
|
||||
finalResult := result(Counter{count: 0})
|
||||
|
||||
assert.Equal(t, true, pair.Tail(finalResult))
|
||||
})
|
||||
}
|
||||
|
||||
// TestApplicativeMonoidWithGets tests monoid with Gets operations
|
||||
func TestApplicativeMonoidWithGets(t *testing.T) {
|
||||
t.Run("combining state reads", func(t *testing.T) {
|
||||
intAddMonoid := N.MonoidSum[int]()
|
||||
m := ApplicativeMonoid[Counter](intAddMonoid)
|
||||
|
||||
// Read the count from state
|
||||
getCount := Gets(func(c Counter) int { return c.count })
|
||||
|
||||
// Combine two reads
|
||||
combined := m.Concat(getCount, getCount)
|
||||
result := combined(Counter{count: 5})
|
||||
|
||||
// State unchanged
|
||||
assert.Equal(t, Counter{count: 5}, pair.Head(result))
|
||||
// Value is doubled (5 + 5)
|
||||
assert.Equal(t, 10, pair.Tail(result))
|
||||
})
|
||||
|
||||
t.Run("combining different state projections", func(t *testing.T) {
|
||||
intAddMonoid := N.MonoidSum[int]()
|
||||
m := ApplicativeMonoid[Counter](intAddMonoid)
|
||||
|
||||
getCount := Gets(func(c Counter) int { return c.count })
|
||||
getDouble := Gets(func(c Counter) int { return c.count * 2 })
|
||||
|
||||
combined := m.Concat(getCount, getDouble)
|
||||
result := combined(Counter{count: 3})
|
||||
|
||||
assert.Equal(t, Counter{count: 3}, pair.Head(result))
|
||||
assert.Equal(t, 9, pair.Tail(result)) // 3 + 6
|
||||
})
|
||||
}
|
||||
|
||||
// TestApplicativeMonoidCommutativity tests behavior with non-commutative operations
|
||||
func TestApplicativeMonoidCommutativity(t *testing.T) {
|
||||
t.Run("string concatenation order matters", func(t *testing.T) {
|
||||
strMonoid := S.Monoid
|
||||
m := ApplicativeMonoid[Counter](strMonoid)
|
||||
|
||||
s1 := Of[Counter]("Hello")
|
||||
s2 := Of[Counter](" World")
|
||||
|
||||
result1 := m.Concat(s1, s2)
|
||||
result2 := m.Concat(s2, s1)
|
||||
|
||||
finalResult1 := result1(Counter{count: 0})
|
||||
finalResult2 := result2(Counter{count: 0})
|
||||
|
||||
assert.Equal(t, "Hello World", pair.Tail(finalResult1))
|
||||
assert.Equal(t, " WorldHello", pair.Tail(finalResult2))
|
||||
assert.NotEqual(t, pair.Tail(finalResult1), pair.Tail(finalResult2))
|
||||
})
|
||||
|
||||
t.Run("subtraction is not commutative", func(t *testing.T) {
|
||||
// Note: Subtraction doesn't form a proper monoid, but we can test it
|
||||
subMonoid := M.MakeMonoid(func(a, b int) int { return a - b }, 0)
|
||||
m := ApplicativeMonoid[Counter](subMonoid)
|
||||
|
||||
s1 := Of[Counter](10)
|
||||
s2 := Of[Counter](3)
|
||||
|
||||
result1 := m.Concat(s1, s2)
|
||||
result2 := m.Concat(s2, s1)
|
||||
|
||||
finalResult1 := result1(Counter{count: 0})
|
||||
finalResult2 := result2(Counter{count: 0})
|
||||
|
||||
assert.Equal(t, 7, pair.Tail(finalResult1)) // 10 - 3
|
||||
assert.Equal(t, -7, pair.Tail(finalResult2)) // 3 - 10
|
||||
assert.NotEqual(t, pair.Tail(finalResult1), pair.Tail(finalResult2))
|
||||
})
|
||||
}
|
||||
79
v2/state/profunctor.go
Normal file
79
v2/state/profunctor.go
Normal file
@@ -0,0 +1,79 @@
|
||||
// Copyright (c) 2025 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 state
|
||||
|
||||
import (
|
||||
F "github.com/IBM/fp-go/v2/function"
|
||||
"github.com/IBM/fp-go/v2/optics/iso"
|
||||
"github.com/IBM/fp-go/v2/pair"
|
||||
)
|
||||
|
||||
// IMap is a profunctor-like operation for State that transforms both the state and value using an isomorphism.
|
||||
// It applies an isomorphism f to the state (contravariantly via Get and covariantly via ReverseGet)
|
||||
// and a function g to the value (covariantly).
|
||||
//
|
||||
// See: https://github.com/fantasyland/fantasy-land?tab=readme-ov-file#profunctor
|
||||
//
|
||||
// This operation allows you to:
|
||||
// - Convert the input state from S2 to S1 using the isomorphism's Get function
|
||||
// - Run the State computation with S1
|
||||
// - Convert the output state back from S1 to S2 using the isomorphism's ReverseGet function
|
||||
// - Transform the result value from A to B using g
|
||||
//
|
||||
// Type Parameters:
|
||||
// - A: The original value type produced by the State
|
||||
// - S2: The new state type
|
||||
// - S1: The original state type expected by the State
|
||||
// - B: The new output value type
|
||||
//
|
||||
// Parameters:
|
||||
// - f: An isomorphism between S2 and S1
|
||||
// - g: Function to transform the output value from A to B
|
||||
//
|
||||
// Returns:
|
||||
// - A Kleisli arrow that takes a State[S1, A] and returns a State[S2, B]
|
||||
//
|
||||
//go:inline
|
||||
func IMap[A, S2, S1, B any](f iso.Iso[S2, S1], g func(A) B) Kleisli[S2, State[S1, A], B] {
|
||||
return F.Bind13of3(F.Flow3[func(s S2) S1, State[S1, A], func(pair.Pair[S1, A]) pair.Pair[S2, B]])(f.Get, pair.BiMap(f.ReverseGet, g))
|
||||
}
|
||||
|
||||
// MapState is a contravariant-like operation for State that transforms the state type using an isomorphism.
|
||||
// It applies an isomorphism f to convert between state types while preserving the value type.
|
||||
//
|
||||
// See: https://github.com/fantasyland/fantasy-land?tab=readme-ov-file#profunctor
|
||||
//
|
||||
// This operation allows you to:
|
||||
// - Convert the input state from S2 to S1 using the isomorphism's Get function
|
||||
// - Run the State computation with S1
|
||||
// - Convert the output state back from S1 to S2 using the isomorphism's ReverseGet function
|
||||
// - Keep the value type A unchanged
|
||||
//
|
||||
// Type Parameters:
|
||||
// - A: The value type (unchanged)
|
||||
// - S2: The new state type
|
||||
// - S1: The original state type expected by the State
|
||||
//
|
||||
// Parameters:
|
||||
// - f: An isomorphism between S2 and S1
|
||||
//
|
||||
// Returns:
|
||||
// - A Kleisli arrow that takes a State[S1, A] and returns a State[S2, A]
|
||||
//
|
||||
//go:inline
|
||||
func MapState[A, S2, S1 any](f iso.Iso[S2, S1]) Kleisli[S2, State[S1, A], A] {
|
||||
return F.Bind13of3(F.Flow3[func(S2) S1, State[S1, A], func(pair.Pair[S1, A]) pair.Pair[S2, A]])(f.Get, pair.MapHead[A](f.ReverseGet))
|
||||
}
|
||||
104
v2/state/profunctor_test.go
Normal file
104
v2/state/profunctor_test.go
Normal file
@@ -0,0 +1,104 @@
|
||||
// Copyright (c) 2025 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 state
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/IBM/fp-go/v2/optics/iso"
|
||||
P "github.com/IBM/fp-go/v2/pair"
|
||||
S "github.com/IBM/fp-go/v2/string"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// TestIMapBasic tests basic IMap functionality
|
||||
func TestIMapBasic(t *testing.T) {
|
||||
t.Run("transform state and value using isomorphism", func(t *testing.T) {
|
||||
// State that increments an int state and returns the old value
|
||||
increment := func(s int) P.Pair[int, int] {
|
||||
return P.MakePair(s+1, s)
|
||||
}
|
||||
|
||||
// Isomorphism between string and int (string length <-> int)
|
||||
stringIntIso := iso.MakeIso(
|
||||
S.Size,
|
||||
strconv.Itoa,
|
||||
)
|
||||
|
||||
// Transform int to string
|
||||
toString := strconv.Itoa
|
||||
|
||||
adapted := IMap(stringIntIso, toString)(increment)
|
||||
result := adapted("hello") // length is 5
|
||||
|
||||
// State should be "6" (5+1), value should be "5"
|
||||
assert.Equal(t, P.MakePair("6", "5"), result)
|
||||
})
|
||||
}
|
||||
|
||||
// TestMapStateBasic tests basic MapState functionality
|
||||
func TestMapStateBasic(t *testing.T) {
|
||||
t.Run("transform only state using isomorphism", func(t *testing.T) {
|
||||
// State that doubles the state and returns it
|
||||
double := func(s int) P.Pair[int, int] {
|
||||
doubled := s * 2
|
||||
return P.MakePair(doubled, doubled)
|
||||
}
|
||||
|
||||
// Isomorphism between string and int
|
||||
stringIntIso := iso.MakeIso(
|
||||
func(s string) int { n, _ := strconv.Atoi(s); return n },
|
||||
strconv.Itoa,
|
||||
)
|
||||
|
||||
adapted := MapState[int](stringIntIso)(double)
|
||||
result := adapted("5")
|
||||
|
||||
// State should be "10" (5*2), value should be 10
|
||||
assert.Equal(t, P.MakePair("10", 10), result)
|
||||
})
|
||||
}
|
||||
|
||||
// TestIMapComposition tests composing IMap transformations
|
||||
func TestIMapComposition(t *testing.T) {
|
||||
t.Run("compose two IMap transformations", func(t *testing.T) {
|
||||
// Simple state that returns the state unchanged
|
||||
identity := func(s int) P.Pair[int, int] {
|
||||
return P.MakePair(s, s)
|
||||
}
|
||||
|
||||
// First isomorphism: bool <-> int (false=0, true=1)
|
||||
boolIntIso := iso.MakeIso(
|
||||
func(b bool) int {
|
||||
if b {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
},
|
||||
func(n int) bool { return n != 0 },
|
||||
)
|
||||
|
||||
// Transform value
|
||||
addOne := func(n int) int { return n + 1 }
|
||||
|
||||
adapted := IMap(boolIntIso, addOne)(identity)
|
||||
result := adapted(true) // true -> 1
|
||||
|
||||
// State should be true (1 -> true), value should be 2 (1+1)
|
||||
assert.Equal(t, P.MakePair(true, 2), result)
|
||||
})
|
||||
}
|
||||
@@ -246,7 +246,7 @@ func TestExecute(t *testing.T) {
|
||||
return TestState{Counter: s.Counter + 1, Message: "new"}
|
||||
})
|
||||
|
||||
finalState := Execute[Void, TestState](initial)(computation)
|
||||
finalState := Execute[Void](initial)(computation)
|
||||
|
||||
assert.Equal(t, 6, finalState.Counter, "counter should be incremented")
|
||||
assert.Equal(t, "new", finalState.Message, "message should be updated")
|
||||
@@ -258,7 +258,7 @@ func TestEvaluate(t *testing.T) {
|
||||
|
||||
computation := Of[TestState](42)
|
||||
|
||||
value := Evaluate[int, TestState](initial)(computation)
|
||||
value := Evaluate[int](initial)(computation)
|
||||
|
||||
assert.Equal(t, 42, value, "value should be 42")
|
||||
}
|
||||
@@ -478,7 +478,7 @@ func TestExecuteWithComplexState(t *testing.T) {
|
||||
|
||||
computation := step2(step1)
|
||||
|
||||
finalState := Execute[Void, TestState](initial)(computation)
|
||||
finalState := Execute[Void](initial)(computation)
|
||||
|
||||
assert.Equal(t, 12, finalState.Counter, "counter should be (1*2)+10 = 12")
|
||||
assert.Equal(t, "end", finalState.Message, "message should be 'end'")
|
||||
@@ -496,9 +496,7 @@ func TestEvaluateWithChain(t *testing.T) {
|
||||
}),
|
||||
)
|
||||
|
||||
value := Evaluate[string, TestState](initial)(computation)
|
||||
value := Evaluate[string](initial)(computation)
|
||||
|
||||
assert.Equal(t, "result: 20", value, "value should be 'result: 20'")
|
||||
}
|
||||
|
||||
// Made with Bob
|
||||
|
||||
@@ -238,7 +238,7 @@ func BindL[ST, S, T any](
|
||||
// counterLens := lens.Prop[Result, int]("counter")
|
||||
// result := function.Pipe2(
|
||||
// Do[AppState](Result{counter: 5}),
|
||||
// LetL(counterLens, func(n int) int { return n * 2 }),
|
||||
// LetL(counterLens, N.Mul(2)),
|
||||
// )
|
||||
//
|
||||
//go:inline
|
||||
|
||||
152
v2/stateio/monoid.go
Normal file
152
v2/stateio/monoid.go
Normal file
@@ -0,0 +1,152 @@
|
||||
// Copyright (c) 2024 - 2025 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 stateio
|
||||
|
||||
import (
|
||||
M "github.com/IBM/fp-go/v2/monoid"
|
||||
)
|
||||
|
||||
// ApplicativeMonoid lifts a monoid into the StateIO applicative functor context.
|
||||
//
|
||||
// This function creates a monoid for StateIO[S, A] values given a monoid for the base type A.
|
||||
// It uses the StateIO monad's applicative operations (Of, MonadMap, MonadAp) to lift the
|
||||
// monoid operations into the StateIO context, allowing you to combine stateful computations
|
||||
// with side effects that produce monoidal values.
|
||||
//
|
||||
// The resulting monoid combines StateIO computations by:
|
||||
// 1. Threading the state through both computations sequentially
|
||||
// 2. Executing the IO effects of both computations in sequence
|
||||
// 3. Combining the produced values using the underlying monoid's Concat operation
|
||||
// 4. Returning a new StateIO computation with the combined value and final state
|
||||
//
|
||||
// The empty element is a StateIO computation that returns the underlying monoid's empty value
|
||||
// without modifying the state or performing any side effects.
|
||||
//
|
||||
// This is particularly useful for:
|
||||
// - Accumulating results across multiple stateful computations with side effects
|
||||
// - Building complex state transformations that aggregate values while performing IO
|
||||
// - Combining independent stateful operations that produce monoidal results
|
||||
//
|
||||
// Type Parameters:
|
||||
// - S: The state type that is threaded through the computations
|
||||
// - A: The value type that has a monoid structure
|
||||
//
|
||||
// Parameters:
|
||||
// - m: A monoid for the base type A that defines how to combine values
|
||||
//
|
||||
// Returns:
|
||||
// - A Monoid[StateIO[S, A]] that combines stateful IO computations using the base monoid
|
||||
//
|
||||
// The resulting monoid satisfies the standard monoid laws:
|
||||
// - Associativity: Concat(Concat(s1, s2), s3) = Concat(s1, Concat(s2, s3))
|
||||
// - Left identity: Concat(Empty(), s) = s
|
||||
// - Right identity: Concat(s, Empty()) = s
|
||||
//
|
||||
// Example with integer addition:
|
||||
//
|
||||
// import (
|
||||
// N "github.com/IBM/fp-go/v2/number"
|
||||
// "github.com/IBM/fp-go/v2/io"
|
||||
// "github.com/IBM/fp-go/v2/pair"
|
||||
// )
|
||||
//
|
||||
// type Counter struct {
|
||||
// count int
|
||||
// }
|
||||
//
|
||||
// // Create a monoid for StateIO[Counter, int] using integer addition
|
||||
// intAddMonoid := N.MonoidSum[int]()
|
||||
// stateIOMonoid := stateio.ApplicativeMonoid[Counter](intAddMonoid)
|
||||
//
|
||||
// // Create two stateful IO computations
|
||||
// s1 := stateio.Of[Counter](5) // Returns 5, state unchanged
|
||||
// s2 := stateio.Of[Counter](3) // Returns 3, state unchanged
|
||||
//
|
||||
// // Combine them using the monoid
|
||||
// combined := stateIOMonoid.Concat(s1, s2)
|
||||
// result := combined(Counter{count: 10})()
|
||||
// // result = Pair{head: Counter{count: 10}, tail: 8} // 5 + 3
|
||||
//
|
||||
// // Empty element
|
||||
// empty := stateIOMonoid.Empty()
|
||||
// emptyResult := empty(Counter{count: 10})()
|
||||
// // emptyResult = Pair{head: Counter{count: 10}, tail: 0}
|
||||
//
|
||||
// Example with string concatenation and state modification:
|
||||
//
|
||||
// import (
|
||||
// S "github.com/IBM/fp-go/v2/string"
|
||||
// "github.com/IBM/fp-go/v2/io"
|
||||
// )
|
||||
//
|
||||
// type Logger struct {
|
||||
// logs []string
|
||||
// }
|
||||
//
|
||||
// strMonoid := S.Monoid
|
||||
// stateIOMonoid := stateio.ApplicativeMonoid[Logger](strMonoid)
|
||||
//
|
||||
// // Stateful IO computation that logs and returns a message
|
||||
// logMessage := func(msg string) stateio.StateIO[Logger, string] {
|
||||
// return func(s Logger) io.IO[pair.Pair[Logger, string]] {
|
||||
// return func() pair.Pair[Logger, string] {
|
||||
// newState := Logger{logs: append(s.logs, msg)}
|
||||
// return pair.MakePair(newState, msg)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// s1 := logMessage("Hello")
|
||||
// s2 := logMessage(" World")
|
||||
//
|
||||
// // Combine the computations - both log entries are added, messages concatenated
|
||||
// combined := stateIOMonoid.Concat(s1, s2)
|
||||
// result := combined(Logger{logs: []string{}})()
|
||||
// // result.head.logs = ["Hello", " World"]
|
||||
// // result.tail = "Hello World"
|
||||
//
|
||||
// Example demonstrating monoid laws:
|
||||
//
|
||||
// intAddMonoid := N.MonoidSum[int]()
|
||||
// m := stateio.ApplicativeMonoid[Counter](intAddMonoid)
|
||||
//
|
||||
// s1 := stateio.Of[Counter](1)
|
||||
// s2 := stateio.Of[Counter](2)
|
||||
// s3 := stateio.Of[Counter](3)
|
||||
//
|
||||
// initialState := Counter{count: 0}
|
||||
//
|
||||
// // Associativity
|
||||
// left := m.Concat(m.Concat(s1, s2), s3)
|
||||
// right := m.Concat(s1, m.Concat(s2, s3))
|
||||
// // Both produce: Pair{head: Counter{count: 0}, tail: 6}
|
||||
//
|
||||
// // Left identity
|
||||
// leftId := m.Concat(m.Empty(), s1)
|
||||
// // Produces: Pair{head: Counter{count: 0}, tail: 1}
|
||||
//
|
||||
// // Right identity
|
||||
// rightId := m.Concat(s1, m.Empty())
|
||||
// // Produces: Pair{head: Counter{count: 0}, tail: 1}
|
||||
//
|
||||
//go:inline
|
||||
func ApplicativeMonoid[S, A any](m M.Monoid[A]) M.Monoid[StateIO[S, A]] {
|
||||
return M.ApplicativeMonoid(
|
||||
Of[S, A],
|
||||
MonadMap[S, A, func(A) A],
|
||||
MonadAp[A, S, A],
|
||||
m)
|
||||
}
|
||||
634
v2/stateio/monoid_test.go
Normal file
634
v2/stateio/monoid_test.go
Normal file
@@ -0,0 +1,634 @@
|
||||
// Copyright (c) 2024 - 2025 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 stateio
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/IBM/fp-go/v2/io"
|
||||
M "github.com/IBM/fp-go/v2/monoid"
|
||||
N "github.com/IBM/fp-go/v2/number"
|
||||
"github.com/IBM/fp-go/v2/pair"
|
||||
S "github.com/IBM/fp-go/v2/string"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// Counter is a simple state type for testing
|
||||
type Counter struct {
|
||||
count int
|
||||
}
|
||||
|
||||
// Logger is a state type that accumulates log messages
|
||||
type Logger struct {
|
||||
logs []string
|
||||
}
|
||||
|
||||
// TestApplicativeMonoidBasic tests basic monoid operations
|
||||
func TestApplicativeMonoidBasic(t *testing.T) {
|
||||
t.Run("integer addition", func(t *testing.T) {
|
||||
intAddMonoid := N.MonoidSum[int]()
|
||||
stateIOMonoid := ApplicativeMonoid[Counter](intAddMonoid)
|
||||
|
||||
s1 := Of[Counter](5)
|
||||
s2 := Of[Counter](3)
|
||||
|
||||
combined := stateIOMonoid.Concat(s1, s2)
|
||||
result := combined(Counter{count: 10})()
|
||||
|
||||
assert.Equal(t, Counter{count: 10}, pair.Head(result))
|
||||
assert.Equal(t, 8, pair.Tail(result))
|
||||
})
|
||||
|
||||
t.Run("integer multiplication", func(t *testing.T) {
|
||||
intMulMonoid := N.MonoidProduct[int]()
|
||||
stateIOMonoid := ApplicativeMonoid[Counter](intMulMonoid)
|
||||
|
||||
s1 := Of[Counter](4)
|
||||
s2 := Of[Counter](5)
|
||||
|
||||
combined := stateIOMonoid.Concat(s1, s2)
|
||||
result := combined(Counter{count: 0})()
|
||||
|
||||
assert.Equal(t, Counter{count: 0}, pair.Head(result))
|
||||
assert.Equal(t, 20, pair.Tail(result))
|
||||
})
|
||||
|
||||
t.Run("string concatenation", func(t *testing.T) {
|
||||
strMonoid := S.Monoid
|
||||
stateIOMonoid := ApplicativeMonoid[Counter](strMonoid)
|
||||
|
||||
s1 := Of[Counter]("Hello")
|
||||
s2 := Of[Counter](" World")
|
||||
|
||||
combined := stateIOMonoid.Concat(s1, s2)
|
||||
result := combined(Counter{count: 5})()
|
||||
|
||||
assert.Equal(t, Counter{count: 5}, pair.Head(result))
|
||||
assert.Equal(t, "Hello World", pair.Tail(result))
|
||||
})
|
||||
|
||||
t.Run("boolean AND", func(t *testing.T) {
|
||||
boolAndMonoid := M.MakeMonoid(func(a, b bool) bool { return a && b }, true)
|
||||
stateIOMonoid := ApplicativeMonoid[Counter](boolAndMonoid)
|
||||
|
||||
s1 := Of[Counter](true)
|
||||
s2 := Of[Counter](true)
|
||||
|
||||
combined := stateIOMonoid.Concat(s1, s2)
|
||||
result := combined(Counter{count: 0})()
|
||||
|
||||
assert.Equal(t, true, pair.Tail(result))
|
||||
|
||||
s3 := Of[Counter](false)
|
||||
combined2 := stateIOMonoid.Concat(s1, s3)
|
||||
result2 := combined2(Counter{count: 0})()
|
||||
|
||||
assert.Equal(t, false, pair.Tail(result2))
|
||||
})
|
||||
}
|
||||
|
||||
// TestApplicativeMonoidEmpty tests the empty element
|
||||
func TestApplicativeMonoidEmpty(t *testing.T) {
|
||||
t.Run("integer addition empty", func(t *testing.T) {
|
||||
intAddMonoid := N.MonoidSum[int]()
|
||||
stateIOMonoid := ApplicativeMonoid[Counter](intAddMonoid)
|
||||
|
||||
empty := stateIOMonoid.Empty()
|
||||
result := empty(Counter{count: 5})()
|
||||
|
||||
assert.Equal(t, Counter{count: 5}, pair.Head(result))
|
||||
assert.Equal(t, 0, pair.Tail(result))
|
||||
})
|
||||
|
||||
t.Run("integer multiplication empty", func(t *testing.T) {
|
||||
intMulMonoid := N.MonoidProduct[int]()
|
||||
stateIOMonoid := ApplicativeMonoid[Counter](intMulMonoid)
|
||||
|
||||
empty := stateIOMonoid.Empty()
|
||||
result := empty(Counter{count: 10})()
|
||||
|
||||
assert.Equal(t, Counter{count: 10}, pair.Head(result))
|
||||
assert.Equal(t, 1, pair.Tail(result))
|
||||
})
|
||||
|
||||
t.Run("string concatenation empty", func(t *testing.T) {
|
||||
strMonoid := S.Monoid
|
||||
stateIOMonoid := ApplicativeMonoid[Counter](strMonoid)
|
||||
|
||||
empty := stateIOMonoid.Empty()
|
||||
result := empty(Counter{count: 0})()
|
||||
|
||||
assert.Equal(t, Counter{count: 0}, pair.Head(result))
|
||||
assert.Equal(t, "", pair.Tail(result))
|
||||
})
|
||||
}
|
||||
|
||||
// TestApplicativeMonoidLaws verifies monoid laws
|
||||
func TestApplicativeMonoidLaws(t *testing.T) {
|
||||
t.Run("associativity with integer addition", func(t *testing.T) {
|
||||
intAddMonoid := N.MonoidSum[int]()
|
||||
m := ApplicativeMonoid[Counter](intAddMonoid)
|
||||
|
||||
s1 := Of[Counter](1)
|
||||
s2 := Of[Counter](2)
|
||||
s3 := Of[Counter](3)
|
||||
|
||||
initialState := Counter{count: 0}
|
||||
|
||||
// (s1 • s2) • s3
|
||||
left := m.Concat(m.Concat(s1, s2), s3)
|
||||
leftResult := left(initialState)()
|
||||
|
||||
// s1 • (s2 • s3)
|
||||
right := m.Concat(s1, m.Concat(s2, s3))
|
||||
rightResult := right(initialState)()
|
||||
|
||||
assert.Equal(t, leftResult, rightResult)
|
||||
assert.Equal(t, 6, pair.Tail(leftResult))
|
||||
})
|
||||
|
||||
t.Run("left identity with integer addition", func(t *testing.T) {
|
||||
intAddMonoid := N.MonoidSum[int]()
|
||||
m := ApplicativeMonoid[Counter](intAddMonoid)
|
||||
|
||||
s := Of[Counter](42)
|
||||
initialState := Counter{count: 5}
|
||||
|
||||
// Empty() • s = s
|
||||
result := m.Concat(m.Empty(), s)
|
||||
expected := s(initialState)()
|
||||
actual := result(initialState)()
|
||||
|
||||
assert.Equal(t, expected, actual)
|
||||
assert.Equal(t, 42, pair.Tail(actual))
|
||||
})
|
||||
|
||||
t.Run("right identity with integer addition", func(t *testing.T) {
|
||||
intAddMonoid := N.MonoidSum[int]()
|
||||
m := ApplicativeMonoid[Counter](intAddMonoid)
|
||||
|
||||
s := Of[Counter](42)
|
||||
initialState := Counter{count: 5}
|
||||
|
||||
// s • Empty() = s
|
||||
result := m.Concat(s, m.Empty())
|
||||
expected := s(initialState)()
|
||||
actual := result(initialState)()
|
||||
|
||||
assert.Equal(t, expected, actual)
|
||||
assert.Equal(t, 42, pair.Tail(actual))
|
||||
})
|
||||
|
||||
t.Run("associativity with string concatenation", func(t *testing.T) {
|
||||
strMonoid := S.Monoid
|
||||
m := ApplicativeMonoid[Counter](strMonoid)
|
||||
|
||||
s1 := Of[Counter]("a")
|
||||
s2 := Of[Counter]("b")
|
||||
s3 := Of[Counter]("c")
|
||||
|
||||
initialState := Counter{count: 0}
|
||||
|
||||
left := m.Concat(m.Concat(s1, s2), s3)
|
||||
leftResult := left(initialState)()
|
||||
|
||||
right := m.Concat(s1, m.Concat(s2, s3))
|
||||
rightResult := right(initialState)()
|
||||
|
||||
assert.Equal(t, leftResult, rightResult)
|
||||
assert.Equal(t, "abc", pair.Tail(leftResult))
|
||||
})
|
||||
|
||||
t.Run("left identity with string concatenation", func(t *testing.T) {
|
||||
strMonoid := S.Monoid
|
||||
m := ApplicativeMonoid[Counter](strMonoid)
|
||||
|
||||
s := Of[Counter]("test")
|
||||
initialState := Counter{count: 0}
|
||||
|
||||
result := m.Concat(m.Empty(), s)
|
||||
expected := s(initialState)()
|
||||
actual := result(initialState)()
|
||||
|
||||
assert.Equal(t, expected, actual)
|
||||
assert.Equal(t, "test", pair.Tail(actual))
|
||||
})
|
||||
|
||||
t.Run("right identity with string concatenation", func(t *testing.T) {
|
||||
strMonoid := S.Monoid
|
||||
m := ApplicativeMonoid[Counter](strMonoid)
|
||||
|
||||
s := Of[Counter]("test")
|
||||
initialState := Counter{count: 0}
|
||||
|
||||
result := m.Concat(s, m.Empty())
|
||||
expected := s(initialState)()
|
||||
actual := result(initialState)()
|
||||
|
||||
assert.Equal(t, expected, actual)
|
||||
assert.Equal(t, "test", pair.Tail(actual))
|
||||
})
|
||||
}
|
||||
|
||||
// TestApplicativeMonoidWithStateModification tests monoid with state-modifying computations
|
||||
func TestApplicativeMonoidWithStateModification(t *testing.T) {
|
||||
t.Run("state modification with integer addition", func(t *testing.T) {
|
||||
intAddMonoid := N.MonoidSum[int]()
|
||||
stateIOMonoid := ApplicativeMonoid[Counter](intAddMonoid)
|
||||
|
||||
// Computation that increments counter and returns a value
|
||||
incrementAndReturn := func(val int) StateIO[Counter, int] {
|
||||
return func(s Counter) IO[Pair[Counter, int]] {
|
||||
return func() Pair[Counter, int] {
|
||||
return pair.MakePair(Counter{count: s.count + 1}, val)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
s1 := incrementAndReturn(5)
|
||||
s2 := incrementAndReturn(3)
|
||||
|
||||
combined := stateIOMonoid.Concat(s1, s2)
|
||||
result := combined(Counter{count: 0})()
|
||||
|
||||
// State should be incremented twice
|
||||
assert.Equal(t, Counter{count: 2}, pair.Head(result))
|
||||
// Values should be added
|
||||
assert.Equal(t, 8, pair.Tail(result))
|
||||
})
|
||||
|
||||
t.Run("state modification with string concatenation", func(t *testing.T) {
|
||||
strMonoid := S.Monoid
|
||||
stateIOMonoid := ApplicativeMonoid[Logger](strMonoid)
|
||||
|
||||
// Computation that logs a message and returns it
|
||||
logMessage := func(msg string) StateIO[Logger, string] {
|
||||
return func(s Logger) IO[Pair[Logger, string]] {
|
||||
return func() Pair[Logger, string] {
|
||||
newLogs := append(s.logs, msg)
|
||||
return pair.MakePair(Logger{logs: newLogs}, msg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
s1 := logMessage("Hello")
|
||||
s2 := logMessage(" World")
|
||||
|
||||
combined := stateIOMonoid.Concat(s1, s2)
|
||||
result := combined(Logger{logs: []string{}})()
|
||||
|
||||
// Both messages should be logged
|
||||
assert.Equal(t, []string{"Hello", " World"}, pair.Head(result).logs)
|
||||
// Messages should be concatenated
|
||||
assert.Equal(t, "Hello World", pair.Tail(result))
|
||||
})
|
||||
|
||||
t.Run("complex state transformation", func(t *testing.T) {
|
||||
intAddMonoid := N.MonoidSum[int]()
|
||||
stateIOMonoid := ApplicativeMonoid[Counter](intAddMonoid)
|
||||
|
||||
// Computation that doubles the counter and returns the old value
|
||||
doubleAndReturnOld := func(val int) StateIO[Counter, int] {
|
||||
return func(s Counter) IO[Pair[Counter, int]] {
|
||||
return func() Pair[Counter, int] {
|
||||
return pair.MakePair(Counter{count: s.count * 2}, val)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
s1 := doubleAndReturnOld(10)
|
||||
s2 := doubleAndReturnOld(20)
|
||||
|
||||
combined := stateIOMonoid.Concat(s1, s2)
|
||||
result := combined(Counter{count: 3})()
|
||||
|
||||
// State: 3 -> 6 -> 12
|
||||
assert.Equal(t, Counter{count: 12}, pair.Head(result))
|
||||
// Values: 10 + 20 = 30
|
||||
assert.Equal(t, 30, pair.Tail(result))
|
||||
})
|
||||
}
|
||||
|
||||
// TestApplicativeMonoidMultipleConcatenations tests chaining multiple concatenations
|
||||
func TestApplicativeMonoidMultipleConcatenations(t *testing.T) {
|
||||
t.Run("chain of integer additions", func(t *testing.T) {
|
||||
intAddMonoid := N.MonoidSum[int]()
|
||||
m := ApplicativeMonoid[Counter](intAddMonoid)
|
||||
|
||||
states := []StateIO[Counter, int]{
|
||||
Of[Counter](1),
|
||||
Of[Counter](2),
|
||||
Of[Counter](3),
|
||||
Of[Counter](4),
|
||||
Of[Counter](5),
|
||||
}
|
||||
|
||||
// Fold all states using the monoid
|
||||
result := m.Empty()
|
||||
for _, s := range states {
|
||||
result = m.Concat(result, s)
|
||||
}
|
||||
|
||||
finalResult := result(Counter{count: 0})()
|
||||
assert.Equal(t, Counter{count: 0}, pair.Head(finalResult))
|
||||
assert.Equal(t, 15, pair.Tail(finalResult)) // 1+2+3+4+5
|
||||
})
|
||||
|
||||
t.Run("chain of string concatenations", func(t *testing.T) {
|
||||
strMonoid := S.Monoid
|
||||
m := ApplicativeMonoid[Counter](strMonoid)
|
||||
|
||||
words := []string{"The", " quick", " brown", " fox"}
|
||||
states := make([]StateIO[Counter, string], len(words))
|
||||
for i, word := range words {
|
||||
states[i] = Of[Counter](word)
|
||||
}
|
||||
|
||||
result := m.Empty()
|
||||
for _, s := range states {
|
||||
result = m.Concat(result, s)
|
||||
}
|
||||
|
||||
finalResult := result(Counter{count: 0})()
|
||||
assert.Equal(t, "The quick brown fox", pair.Tail(finalResult))
|
||||
})
|
||||
|
||||
t.Run("nested concatenations", func(t *testing.T) {
|
||||
intAddMonoid := N.MonoidSum[int]()
|
||||
m := ApplicativeMonoid[Counter](intAddMonoid)
|
||||
|
||||
s1 := Of[Counter](1)
|
||||
s2 := Of[Counter](2)
|
||||
s3 := Of[Counter](3)
|
||||
s4 := Of[Counter](4)
|
||||
|
||||
// ((s1 • s2) • s3) • s4
|
||||
result := m.Concat(
|
||||
m.Concat(
|
||||
m.Concat(s1, s2),
|
||||
s3,
|
||||
),
|
||||
s4,
|
||||
)
|
||||
|
||||
finalResult := result(Counter{count: 0})()
|
||||
assert.Equal(t, 10, pair.Tail(finalResult))
|
||||
})
|
||||
}
|
||||
|
||||
// TestApplicativeMonoidEdgeCases tests edge cases
|
||||
func TestApplicativeMonoidEdgeCases(t *testing.T) {
|
||||
t.Run("concatenating empty with empty", func(t *testing.T) {
|
||||
intAddMonoid := N.MonoidSum[int]()
|
||||
m := ApplicativeMonoid[Counter](intAddMonoid)
|
||||
|
||||
result := m.Concat(m.Empty(), m.Empty())
|
||||
finalResult := result(Counter{count: 5})()
|
||||
|
||||
assert.Equal(t, Counter{count: 5}, pair.Head(finalResult))
|
||||
assert.Equal(t, 0, pair.Tail(finalResult))
|
||||
})
|
||||
|
||||
t.Run("zero values with multiplication", func(t *testing.T) {
|
||||
intMulMonoid := N.MonoidProduct[int]()
|
||||
m := ApplicativeMonoid[Counter](intMulMonoid)
|
||||
|
||||
s1 := Of[Counter](0)
|
||||
s2 := Of[Counter](42)
|
||||
|
||||
result := m.Concat(s1, s2)
|
||||
finalResult := result(Counter{count: 0})()
|
||||
|
||||
assert.Equal(t, 0, pair.Tail(finalResult))
|
||||
})
|
||||
|
||||
t.Run("empty strings", func(t *testing.T) {
|
||||
strMonoid := S.Monoid
|
||||
m := ApplicativeMonoid[Counter](strMonoid)
|
||||
|
||||
s1 := Of[Counter]("")
|
||||
s2 := Of[Counter]("test")
|
||||
|
||||
result := m.Concat(s1, s2)
|
||||
finalResult := result(Counter{count: 0})()
|
||||
|
||||
assert.Equal(t, "test", pair.Tail(finalResult))
|
||||
})
|
||||
|
||||
t.Run("negative numbers with addition", func(t *testing.T) {
|
||||
intAddMonoid := N.MonoidSum[int]()
|
||||
m := ApplicativeMonoid[Counter](intAddMonoid)
|
||||
|
||||
s1 := Of[Counter](-5)
|
||||
s2 := Of[Counter](3)
|
||||
|
||||
result := m.Concat(s1, s2)
|
||||
finalResult := result(Counter{count: 0})()
|
||||
|
||||
assert.Equal(t, -2, pair.Tail(finalResult))
|
||||
})
|
||||
}
|
||||
|
||||
// TestApplicativeMonoidWithDifferentTypes tests monoid with various type combinations
|
||||
func TestApplicativeMonoidWithDifferentTypes(t *testing.T) {
|
||||
t.Run("float64 addition", func(t *testing.T) {
|
||||
floatAddMonoid := N.MonoidSum[float64]()
|
||||
m := ApplicativeMonoid[Counter](floatAddMonoid)
|
||||
|
||||
s1 := Of[Counter](1.5)
|
||||
s2 := Of[Counter](2.5)
|
||||
|
||||
result := m.Concat(s1, s2)
|
||||
finalResult := result(Counter{count: 0})()
|
||||
|
||||
assert.Equal(t, 4.0, pair.Tail(finalResult))
|
||||
})
|
||||
|
||||
t.Run("float64 multiplication", func(t *testing.T) {
|
||||
floatMulMonoid := N.MonoidProduct[float64]()
|
||||
m := ApplicativeMonoid[Counter](floatMulMonoid)
|
||||
|
||||
s1 := Of[Counter](2.0)
|
||||
s2 := Of[Counter](3.5)
|
||||
|
||||
result := m.Concat(s1, s2)
|
||||
finalResult := result(Counter{count: 0})()
|
||||
|
||||
assert.Equal(t, 7.0, pair.Tail(finalResult))
|
||||
})
|
||||
|
||||
t.Run("boolean OR", func(t *testing.T) {
|
||||
boolOrMonoid := M.MakeMonoid(func(a, b bool) bool { return a || b }, false)
|
||||
m := ApplicativeMonoid[Counter](boolOrMonoid)
|
||||
|
||||
s1 := Of[Counter](false)
|
||||
s2 := Of[Counter](true)
|
||||
|
||||
result := m.Concat(s1, s2)
|
||||
finalResult := result(Counter{count: 0})()
|
||||
|
||||
assert.Equal(t, true, pair.Tail(finalResult))
|
||||
})
|
||||
}
|
||||
|
||||
// TestApplicativeMonoidWithIO tests monoid with actual IO effects
|
||||
func TestApplicativeMonoidWithIO(t *testing.T) {
|
||||
t.Run("combining IO effects", func(t *testing.T) {
|
||||
intAddMonoid := N.MonoidSum[int]()
|
||||
m := ApplicativeMonoid[Counter](intAddMonoid)
|
||||
|
||||
// StateIO that performs IO and modifies state
|
||||
effectfulComputation := func(val int, increment int) StateIO[Counter, int] {
|
||||
return func(s Counter) IO[Pair[Counter, int]] {
|
||||
return func() Pair[Counter, int] {
|
||||
// Simulate IO effect
|
||||
newCount := s.count + increment
|
||||
return pair.MakePair(Counter{count: newCount}, val)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
s1 := effectfulComputation(10, 1)
|
||||
s2 := effectfulComputation(20, 2)
|
||||
|
||||
combined := m.Concat(s1, s2)
|
||||
result := combined(Counter{count: 0})()
|
||||
|
||||
// State should be incremented by 1 then by 2
|
||||
assert.Equal(t, Counter{count: 3}, pair.Head(result))
|
||||
// Values should be added
|
||||
assert.Equal(t, 30, pair.Tail(result))
|
||||
})
|
||||
|
||||
t.Run("IO effects with logging", func(t *testing.T) {
|
||||
strMonoid := S.Monoid
|
||||
m := ApplicativeMonoid[Logger](strMonoid)
|
||||
|
||||
// StateIO that logs and returns a message
|
||||
logAndReturn := func(msg string) StateIO[Logger, string] {
|
||||
return func(s Logger) IO[Pair[Logger, string]] {
|
||||
return func() Pair[Logger, string] {
|
||||
// Simulate IO logging effect
|
||||
newLogs := append(append([]string{}, s.logs...), msg)
|
||||
return pair.MakePair(Logger{logs: newLogs}, msg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
s1 := logAndReturn("First")
|
||||
s2 := logAndReturn("Second")
|
||||
s3 := logAndReturn("Third")
|
||||
|
||||
combined := m.Concat(m.Concat(s1, s2), s3)
|
||||
result := combined(Logger{logs: []string{}})()
|
||||
|
||||
assert.Equal(t, []string{"First", "Second", "Third"}, pair.Head(result).logs)
|
||||
assert.Equal(t, "FirstSecondThird", pair.Tail(result))
|
||||
})
|
||||
}
|
||||
|
||||
// TestApplicativeMonoidCommutativity tests behavior with non-commutative operations
|
||||
func TestApplicativeMonoidCommutativity(t *testing.T) {
|
||||
t.Run("string concatenation order matters", func(t *testing.T) {
|
||||
strMonoid := S.Monoid
|
||||
m := ApplicativeMonoid[Counter](strMonoid)
|
||||
|
||||
s1 := Of[Counter]("Hello")
|
||||
s2 := Of[Counter](" World")
|
||||
|
||||
result1 := m.Concat(s1, s2)
|
||||
result2 := m.Concat(s2, s1)
|
||||
|
||||
finalResult1 := result1(Counter{count: 0})()
|
||||
finalResult2 := result2(Counter{count: 0})()
|
||||
|
||||
assert.Equal(t, "Hello World", pair.Tail(finalResult1))
|
||||
assert.Equal(t, " WorldHello", pair.Tail(finalResult2))
|
||||
assert.NotEqual(t, pair.Tail(finalResult1), pair.Tail(finalResult2))
|
||||
})
|
||||
|
||||
t.Run("subtraction is not commutative", func(t *testing.T) {
|
||||
// Note: Subtraction doesn't form a proper monoid, but we can test it
|
||||
subMonoid := M.MakeMonoid(func(a, b int) int { return a - b }, 0)
|
||||
m := ApplicativeMonoid[Counter](subMonoid)
|
||||
|
||||
s1 := Of[Counter](10)
|
||||
s2 := Of[Counter](3)
|
||||
|
||||
result1 := m.Concat(s1, s2)
|
||||
result2 := m.Concat(s2, s1)
|
||||
|
||||
finalResult1 := result1(Counter{count: 0})()
|
||||
finalResult2 := result2(Counter{count: 0})()
|
||||
|
||||
assert.Equal(t, 7, pair.Tail(finalResult1)) // 10 - 3
|
||||
assert.Equal(t, -7, pair.Tail(finalResult2)) // 3 - 10
|
||||
assert.NotEqual(t, pair.Tail(finalResult1), pair.Tail(finalResult2))
|
||||
})
|
||||
|
||||
t.Run("state modification order matters", func(t *testing.T) {
|
||||
intAddMonoid := N.MonoidSum[int]()
|
||||
m := ApplicativeMonoid[Counter](intAddMonoid)
|
||||
|
||||
// Computation that uses current state value
|
||||
useState := func(multiplier int) StateIO[Counter, int] {
|
||||
return func(s Counter) IO[Pair[Counter, int]] {
|
||||
return func() Pair[Counter, int] {
|
||||
result := s.count * multiplier
|
||||
newState := Counter{count: s.count + 1}
|
||||
return pair.MakePair(newState, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
s1 := useState(2)
|
||||
s2 := useState(3)
|
||||
|
||||
// Order matters because state is threaded through
|
||||
result1 := m.Concat(s1, s2)
|
||||
result2 := m.Concat(s2, s1)
|
||||
|
||||
finalResult1 := result1(Counter{count: 5})()
|
||||
finalResult2 := result2(Counter{count: 5})()
|
||||
|
||||
// s1 then s2: (5*2) + ((5+1)*3) = 10 + 18 = 28
|
||||
assert.Equal(t, 28, pair.Tail(finalResult1))
|
||||
// s2 then s1: (5*3) + ((5+1)*2) = 15 + 12 = 27
|
||||
assert.Equal(t, 27, pair.Tail(finalResult2))
|
||||
assert.NotEqual(t, pair.Tail(finalResult1), pair.Tail(finalResult2))
|
||||
})
|
||||
}
|
||||
|
||||
// TestApplicativeMonoidWithFromIO tests monoid with FromIO
|
||||
func TestApplicativeMonoidWithFromIO(t *testing.T) {
|
||||
t.Run("combining FromIO computations", func(t *testing.T) {
|
||||
intAddMonoid := N.MonoidSum[int]()
|
||||
m := ApplicativeMonoid[Counter](intAddMonoid)
|
||||
|
||||
// Create StateIO from IO
|
||||
io1 := io.Of(5)
|
||||
io2 := io.Of(3)
|
||||
|
||||
s1 := FromIO[Counter](io1)
|
||||
s2 := FromIO[Counter](io2)
|
||||
|
||||
combined := m.Concat(s1, s2)
|
||||
result := combined(Counter{count: 10})()
|
||||
|
||||
assert.Equal(t, Counter{count: 10}, pair.Head(result))
|
||||
assert.Equal(t, 8, pair.Tail(result))
|
||||
})
|
||||
}
|
||||
@@ -13,6 +13,9 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// Package string provides functional programming utilities for working with strings.
|
||||
// It includes functions for string manipulation, comparison, conversion, and formatting,
|
||||
// following functional programming principles with curried functions and composable operations.
|
||||
package string
|
||||
|
||||
import (
|
||||
@@ -42,7 +45,7 @@ var (
|
||||
// Includes returns a predicate that tests for the existence of the search string
|
||||
Includes = F.Bind2of2(strings.Contains)
|
||||
|
||||
// HasPrefix returns a predicate that checks if the prefis is included in the string
|
||||
// HasPrefix returns a predicate that checks if the prefix is included in the string
|
||||
HasPrefix = F.Bind2of2(strings.HasPrefix)
|
||||
)
|
||||
|
||||
@@ -103,3 +106,31 @@ func Intersperse(middle string) func(string, string) string {
|
||||
return l + middle + r
|
||||
}
|
||||
}
|
||||
|
||||
// Prepend returns a function that prepends a prefix to a string.
|
||||
// This is a curried function that takes a prefix and returns a function
|
||||
// that prepends that prefix to any string passed to it.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// addHello := Prepend("Hello, ")
|
||||
// result := addHello("World") // "Hello, World"
|
||||
func Prepend(prefix string) func(string) string {
|
||||
return func(suffix string) string {
|
||||
return prefix + suffix
|
||||
}
|
||||
}
|
||||
|
||||
// Append returns a function that appends a suffix to a string.
|
||||
// This is a curried function that takes a suffix and returns a function
|
||||
// that appends that suffix to any string passed to it.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// addExclamation := Append("!")
|
||||
// result := addExclamation("Hello") // "Hello!"
|
||||
func Append(suffix string) func(string) string {
|
||||
return func(prefix string) string {
|
||||
return prefix + suffix
|
||||
}
|
||||
}
|
||||
|
||||
@@ -141,3 +141,99 @@ func TestOrd(t *testing.T) {
|
||||
assert.True(t, Ord.Compare("a", "a") == 0)
|
||||
assert.True(t, Ord.Compare("abc", "abd") < 0)
|
||||
}
|
||||
|
||||
func TestPrepend(t *testing.T) {
|
||||
t.Run("prepend to non-empty string", func(t *testing.T) {
|
||||
addHello := Prepend("Hello, ")
|
||||
result := addHello("World")
|
||||
assert.Equal(t, "Hello, World", result)
|
||||
})
|
||||
|
||||
t.Run("prepend to empty string", func(t *testing.T) {
|
||||
addPrefix := Prepend("prefix")
|
||||
result := addPrefix("")
|
||||
assert.Equal(t, "prefix", result)
|
||||
})
|
||||
|
||||
t.Run("prepend empty string", func(t *testing.T) {
|
||||
addNothing := Prepend("")
|
||||
result := addNothing("test")
|
||||
assert.Equal(t, "test", result)
|
||||
})
|
||||
|
||||
t.Run("prepend with special characters", func(t *testing.T) {
|
||||
addSymbols := Prepend(">>> ")
|
||||
result := addSymbols("message")
|
||||
assert.Equal(t, ">>> message", result)
|
||||
})
|
||||
|
||||
t.Run("prepend with unicode", func(t *testing.T) {
|
||||
addEmoji := Prepend("🎉 ")
|
||||
result := addEmoji("Party!")
|
||||
assert.Equal(t, "🎉 Party!", result)
|
||||
})
|
||||
|
||||
t.Run("multiple prepends", func(t *testing.T) {
|
||||
addA := Prepend("A")
|
||||
addB := Prepend("B")
|
||||
result := addB(addA("C"))
|
||||
assert.Equal(t, "BAC", result)
|
||||
})
|
||||
}
|
||||
|
||||
func TestAppend(t *testing.T) {
|
||||
t.Run("append to non-empty string", func(t *testing.T) {
|
||||
addExclamation := Append("!")
|
||||
result := addExclamation("Hello")
|
||||
assert.Equal(t, "Hello!", result)
|
||||
})
|
||||
|
||||
t.Run("append to empty string", func(t *testing.T) {
|
||||
addSuffix := Append("suffix")
|
||||
result := addSuffix("")
|
||||
assert.Equal(t, "suffix", result)
|
||||
})
|
||||
|
||||
t.Run("append empty string", func(t *testing.T) {
|
||||
addNothing := Append("")
|
||||
result := addNothing("test")
|
||||
assert.Equal(t, "test", result)
|
||||
})
|
||||
|
||||
t.Run("append with special characters", func(t *testing.T) {
|
||||
addEllipsis := Append("...")
|
||||
result := addEllipsis("To be continued")
|
||||
assert.Equal(t, "To be continued...", result)
|
||||
})
|
||||
|
||||
t.Run("append with unicode", func(t *testing.T) {
|
||||
addEmoji := Append(" 🎉")
|
||||
result := addEmoji("Party")
|
||||
assert.Equal(t, "Party 🎉", result)
|
||||
})
|
||||
|
||||
t.Run("multiple appends", func(t *testing.T) {
|
||||
addA := Append("A")
|
||||
addB := Append("B")
|
||||
result := addB(addA("C"))
|
||||
assert.Equal(t, "CAB", result)
|
||||
})
|
||||
}
|
||||
|
||||
func TestPrependAndAppend(t *testing.T) {
|
||||
t.Run("combine prepend and append", func(t *testing.T) {
|
||||
addPrefix := Prepend("[ ")
|
||||
addSuffix := Append(" ]")
|
||||
result := addSuffix(addPrefix("content"))
|
||||
assert.Equal(t, "[ content ]", result)
|
||||
})
|
||||
|
||||
t.Run("chain multiple operations", func(t *testing.T) {
|
||||
addQuotes := Prepend("\"")
|
||||
closeQuotes := Append("\"")
|
||||
addLabel := Prepend("Value: ")
|
||||
|
||||
result := addLabel(addQuotes(closeQuotes("test")))
|
||||
assert.Equal(t, "Value: \"test\"", result)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -62,14 +62,14 @@
|
||||
// t := tuple.MakeTuple2(5, "hello")
|
||||
// mapper := tuple.Map2(
|
||||
// func(n int) string { return fmt.Sprintf("%d", n) },
|
||||
// func(s string) int { return len(s) },
|
||||
// S.Size,
|
||||
// )
|
||||
// result := mapper(t) // Tuple2[string, int]{"5", 5}
|
||||
//
|
||||
// BiMap transforms both elements of a Tuple2:
|
||||
//
|
||||
// mapper := tuple.BiMap(
|
||||
// func(s string) int { return len(s) },
|
||||
// S.Size,
|
||||
// func(n int) string { return fmt.Sprintf("%d", n*2) },
|
||||
// )
|
||||
//
|
||||
|
||||
@@ -18,6 +18,7 @@ package tuple_test
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
S "github.com/IBM/fp-go/v2/string"
|
||||
"github.com/IBM/fp-go/v2/tuple"
|
||||
)
|
||||
|
||||
@@ -63,7 +64,7 @@ func ExampleOf2() {
|
||||
func ExampleBiMap() {
|
||||
t := tuple.MakeTuple2(5, "hello")
|
||||
mapper := tuple.BiMap(
|
||||
func(s string) int { return len(s) },
|
||||
S.Size,
|
||||
func(n int) string { return fmt.Sprintf("%d", n*2) },
|
||||
)
|
||||
result := mapper(t)
|
||||
|
||||
@@ -95,7 +95,7 @@ func Of2[T1, T2 any](e T2) func(T1) Tuple2[T1, T2] {
|
||||
//
|
||||
// t := tuple.MakeTuple2(5, "hello")
|
||||
// mapper := tuple.BiMap(
|
||||
// func(s string) int { return len(s) },
|
||||
// S.Size,
|
||||
// func(n int) string { return fmt.Sprintf("%d", n*2) },
|
||||
// )
|
||||
// result := mapper(t) // Returns Tuple2[string, int]{F1: "10", F2: 5}
|
||||
|
||||
@@ -89,7 +89,7 @@ func TestOf2(t *testing.T) {
|
||||
func TestBiMap(t *testing.T) {
|
||||
t2 := MakeTuple2(5, "hello")
|
||||
mapper := BiMap(
|
||||
func(s string) int { return len(s) },
|
||||
S.Size,
|
||||
func(n int) string { return fmt.Sprintf("%d", n*2) },
|
||||
)
|
||||
result := mapper(t2)
|
||||
@@ -135,7 +135,7 @@ func TestMap2(t *testing.T) {
|
||||
t2 := MakeTuple2(5, "hello")
|
||||
mapper := Map2(
|
||||
func(n int) string { return fmt.Sprintf("%d", n*2) },
|
||||
func(s string) int { return len(s) },
|
||||
S.Size,
|
||||
)
|
||||
result := mapper(t2)
|
||||
assert.Equal(t, MakeTuple2("10", 5), result)
|
||||
|
||||
Reference in New Issue
Block a user