Skip to content

Commit

Permalink
fix: use endomorphism
Browse files Browse the repository at this point in the history
Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
  • Loading branch information
CarstenLeue committed Dec 17, 2023
1 parent 5caabf4 commit 5fcd0b1
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 17 deletions.
6 changes: 6 additions & 0 deletions array/array.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,10 +308,16 @@ func SliceRight[A any](start int) func([]A) []A {
return G.SliceRight[[]A](start)
}

// Copy creates a shallow copy of the array
func Copy[A any](b []A) []A {
return G.Copy(b)
}

// Clone creates a deep copy of the array using the provided endomorphism to clone the values
func Clone[A any](f func(A) A) func(as []A) []A {
return G.Clone[[]A](f)
}

// FoldMap maps and folds an array. Map the Array passing each value to the iterating function. Then fold the results using the provided Monoid.
func FoldMap[A, B any](m M.Monoid[B]) func(func(A) B) func([]A) B {
return G.FoldMap[[]A](m)
Expand Down
5 changes: 5 additions & 0 deletions array/generic/array.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,11 @@ func Copy[AS ~[]A, A any](b AS) AS {
return buf
}

func Clone[AS ~[]A, A any](f func(A) A) func(as AS) AS {
// implementation assumes that map does not optimize for the empty array
return Map[AS, AS](f)
}

func FoldMap[AS ~[]A, A, B any](m M.Monoid[B]) func(func(A) B) func(AS) B {
return func(f func(A) B) func(AS) B {
return func(as AS) B {
Expand Down
51 changes: 51 additions & 0 deletions endomorphism/generic/monoid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) 2023 IBM Corp.
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package generic

import (
F "github.com/IBM/fp-go/function"
M "github.com/IBM/fp-go/monoid"
S "github.com/IBM/fp-go/semigroup"
)

// Of converts any function to an [Endomorphism]
func Of[ENDO ~func(A) A, F ~func(A) A, A any](f F) ENDO {
return func(a A) A {
return f(a)
}
}

func Identity[ENDO ~func(A) A, A any]() ENDO {
return func(a A) A {
return a
}
}

func Compose[ENDO ~func(A) A, A any](f1, f2 ENDO) ENDO {
return func(a A) A {
return F.Pipe2(a, f1, f2)
}
}

// Semigroup for the Endomorphism where the `concat` operation is the usual function composition.
func Semigroup[ENDO ~func(A) A, A any]() S.Semigroup[ENDO] {
return S.MakeSemigroup(Compose[ENDO])
}

// Monoid for the Endomorphism where the `concat` operation is the usual function composition.
func Monoid[ENDO ~func(A) A, A any]() M.Monoid[ENDO] {
return M.MakeMonoid(Compose[ENDO], Identity[ENDO]())
}
23 changes: 18 additions & 5 deletions endomorphism/monoid.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,30 @@
package endomorphism

import (
F "github.com/IBM/fp-go/function"
G "github.com/IBM/fp-go/endomorphism/generic"
M "github.com/IBM/fp-go/monoid"
S "github.com/IBM/fp-go/semigroup"
)

// Endomorphism is a function that
type Endomorphism[A any] func(A) A

// Of converts any function to an [Endomorphism]
func Of[F ~func(A) A, A any](f F) Endomorphism[A] {
return G.Of[Endomorphism[A]](f)
}

// Identity returns the identity [Endomorphism]
func Identity[A any]() Endomorphism[A] {
return G.Identity[Endomorphism[A]]()
}

// Semigroup for the Endomorphism where the `concat` operation is the usual function composition.
func Semigroup[A any]() S.Semigroup[func(A) A] {
return S.MakeSemigroup(F.Flow2[func(A) A, func(A) A])
func Semigroup[A any]() S.Semigroup[Endomorphism[A]] {
return G.Semigroup[Endomorphism[A]]()
}

// Monoid for the Endomorphism where the `concat` operation is the usual function composition.
func Monoid[A any]() M.Monoid[func(A) A] {
return M.MakeMonoid(F.Flow2[func(A) A, func(A) A], F.Identity[A])
func Monoid[A any]() M.Monoid[Endomorphism[A]] {
return G.Monoid[Endomorphism[A]]()
}
12 changes: 7 additions & 5 deletions http/form/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"net/url"

A "github.com/IBM/fp-go/array"
ENDO "github.com/IBM/fp-go/endomorphism"
F "github.com/IBM/fp-go/function"
L "github.com/IBM/fp-go/optics/lens"
LA "github.com/IBM/fp-go/optics/lens/array"
Expand All @@ -27,8 +28,8 @@ import (
)

type (
// FormBuilder returns a function that transforms a form
FormBuilder = func(url.Values) url.Values
// FormEndomorphism returns an [ENDO.Endomorphism] that transforms a form
FormEndomorphism = ENDO.Endomorphism[url.Values]
)

var (
Expand All @@ -53,14 +54,15 @@ var (
)

// WithValue creates a [FormBuilder] for a certain field
func WithValue(name string) func(value string) FormBuilder {
return F.Flow2(
func WithValue(name string) func(value string) FormEndomorphism {
return F.Flow3(
O.Of[string],
AtValue(name).Set,
ENDO.Of[func(url.Values) url.Values],
)
}

// WithoutValue creates a [FormBuilder] that removes a field
func WithoutValue(name string) FormBuilder {
func WithoutValue(name string) FormEndomorphism {
return AtValue(name).Set(noField)
}
16 changes: 9 additions & 7 deletions ioeither/http/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"net/url"
"strconv"

ENDO "github.com/IBM/fp-go/endomorphism"
F "github.com/IBM/fp-go/function"
IOE "github.com/IBM/fp-go/ioeither"
IOEH "github.com/IBM/fp-go/ioeither/http"
Expand All @@ -39,8 +40,8 @@ type (
body O.Option[IOE.IOEither[error, []byte]]
}

// BuilderBuilder returns a function that transforms a builder
BuilderBuilder = func(*Builder) *Builder
// BuilderEndomorphism returns an [ENDO.Endomorphism] that transforms a builder
BuilderEndomorphism = ENDO.Endomorphism[*Builder]
)

var (
Expand Down Expand Up @@ -208,20 +209,21 @@ func Header(name string) L.Lens[*Builder, O.Option[string]] {
}

// WithHeader creates a [BuilderBuilder] for a certain header
func WithHeader(name string) func(value string) BuilderBuilder {
return F.Flow2(
func WithHeader(name string) func(value string) BuilderEndomorphism {
return F.Flow3(
O.Of[string],
Header(name).Set,
ENDO.Of[func(*Builder) *Builder],
)
}

// WithoutHeader creates a [BuilderBuilder] to remove a certain header
func WithoutHeader(name string) BuilderBuilder {
func WithoutHeader(name string) BuilderEndomorphism {
return Header(name).Set(noHeader)
}

// WithFormData creates a [BuilderBuilder] to send form data payload
func WithFormData(value url.Values) BuilderBuilder {
func WithFormData(value url.Values) BuilderEndomorphism {
return F.Flow2(
F.Pipe4(
value,
Expand All @@ -235,7 +237,7 @@ func WithFormData(value url.Values) BuilderBuilder {
}

// WithJson creates a [BuilderBuilder] to send JSON payload
func WithJson[T any](data T) BuilderBuilder {
func WithJson[T any](data T) BuilderEndomorphism {
return F.Flow2(
F.Pipe3(
data,
Expand Down
9 changes: 9 additions & 0 deletions record/generic/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,3 +531,12 @@ func MonadFlap[GFAB ~map[K]func(A) B, GB ~map[K]B, K comparable, A, B any](fab G
func Flap[GFAB ~map[K]func(A) B, GB ~map[K]B, K comparable, A, B any](a A) func(GFAB) GB {
return FC.Flap(MonadMap[GFAB, GB], a)
}

func Copy[M ~map[K]V, K comparable, V any](m M) M {
return duplicate(m)
}

func Clone[M ~map[K]V, K comparable, V any](f func(V) V) func(m M) M {
// impementation assumes that map does not optimize for the empty map
return Map[M, M](f)
}
10 changes: 10 additions & 0 deletions record/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,3 +284,13 @@ func MonadFlap[B any, K comparable, A any](fab map[K]func(A) B, a A) map[K]B {
func Flap[B any, K comparable, A any](a A) func(map[K]func(A) B) map[K]B {
return G.Flap[map[K]func(A) B, map[K]B](a)
}

// Copy creates a shallow copy of the map
func Copy[K comparable, V any](m map[K]V) map[K]V {
return G.Copy[map[K]V](m)
}

// Clone creates a deep copy of the map using the provided endomorphism to clone the values
func Clone[K comparable, V any](f func(V) V) func(m map[K]V) map[K]V {
return G.Clone[map[K]V](f)
}
18 changes: 18 additions & 0 deletions record/record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"strings"
"testing"

A "github.com/IBM/fp-go/array"
"github.com/IBM/fp-go/internal/utils"
O "github.com/IBM/fp-go/option"
S "github.com/IBM/fp-go/string"
Expand Down Expand Up @@ -131,3 +132,20 @@ func ExampleValuesOrd() {
// Output: [c b a]

}

func TestCopyVsClone(t *testing.T) {
slc := []string{"b", "c"}
src := map[string][]string{
"a": slc,
}
// make a shallow copy
cpy := Copy(src)
// make a deep copy
cln := Clone[string](A.Copy[string])(src)

assert.Equal(t, cpy, cln)
// make a modification to the original slice
slc[0] = "d"
assert.NotEqual(t, cpy, cln)
assert.Equal(t, src, cpy)
}

0 comments on commit 5fcd0b1

Please sign in to comment.