1
0
mirror of https://github.com/IBM/fp-go.git synced 2025-08-24 19:29:11 +02:00

Compare commits

...

2 Commits

Author SHA1 Message Date
Dr. Carsten Leue
6d043d2752 fix: common functions for endomorphism
Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
2023-12-17 22:59:25 +01:00
Dr. Carsten Leue
1d02f21ff5 fix: rename FormEndomorphism and FormBuilder
Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
2023-12-17 22:38:55 +01:00
4 changed files with 134 additions and 14 deletions

60
endomorphism/endo.go Normal file
View File

@@ -0,0 +1,60 @@
// 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 endomorphism
import (
G "github.com/IBM/fp-go/endomorphism/generic"
)
func MonadAp[A any](fab Endomorphism[A], fa A) A {
return G.MonadAp[Endomorphism[A]](fab, fa)
}
func Ap[A any](fa A) func(Endomorphism[A]) A {
return G.Ap[Endomorphism[A]](fa)
}
func MonadFlap[A any](fab Endomorphism[A], a A) A {
return G.MonadFlap[Endomorphism[A]](fab, a)
}
func Flap[A any](a A) func(Endomorphism[A]) A {
return G.Flap[Endomorphism[A]](a)
}
func MonadMap[A any](fa A, f Endomorphism[A]) A {
return G.MonadMap[Endomorphism[A]](fa, f)
}
func Map[A any](f Endomorphism[A]) Endomorphism[A] {
return G.Map[Endomorphism[A]](f)
}
func MonadChain[A any](ma A, f Endomorphism[A]) A {
return G.MonadChain[Endomorphism[A]](ma, f)
}
func Chain[A any](f Endomorphism[A]) Endomorphism[A] {
return G.Chain[Endomorphism[A], A](f)
}
func MonadChainFirst[A any](fa A, f Endomorphism[A]) A {
return G.MonadChainFirst[Endomorphism[A]](fa, f)
}
func ChainFirst[A any](f Endomorphism[A]) Endomorphism[A] {
return G.ChainFirst[Endomorphism[A]](f)
}

View File

@@ -0,0 +1,60 @@
// 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 (
I "github.com/IBM/fp-go/identity/generic"
)
func MonadAp[GA ~func(A) A, A any](fab GA, fa A) A {
return I.MonadAp[GA, A, A](fab, fa)
}
func Ap[GA ~func(A) A, A any](fa A) func(GA) A {
return I.Ap[GA, A, A](fa)
}
func MonadFlap[GA ~func(A) A, A any](fab GA, a A) A {
return I.MonadFlap[GA, A, A](fab, a)
}
func Flap[GA ~func(A) A, A any](a A) func(GA) A {
return I.Flap[GA, A, A](a)
}
func MonadMap[GA ~func(A) A, A any](fa A, f GA) A {
return I.MonadMap[GA, A, A](fa, f)
}
func Map[GA ~func(A) A, A any](f GA) GA {
return I.Map[GA, A, A](f)
}
func MonadChain[GA ~func(A) A, A any](ma A, f GA) A {
return I.MonadChain[GA, A, A](ma, f)
}
func Chain[GA ~func(A) A, A any](f GA) GA {
return I.Chain[GA, A](f)
}
func MonadChainFirst[GA ~func(A) A, A any](fa A, f GA) A {
return I.MonadChainFirst[GA, A, A](fa, f)
}
func ChainFirst[GA ~func(A) A, A any](f GA) GA {
return I.ChainFirst[GA, A, A](f)
}

View File

@@ -28,8 +28,8 @@ import (
)
type (
// FormEndomorphism returns an [ENDO.Endomorphism] that transforms a form
FormEndomorphism = ENDO.Endomorphism[url.Values]
// Endomorphism returns an [ENDO.Endomorphism] that transforms a form
Endomorphism = ENDO.Endomorphism[url.Values]
)
var (
@@ -38,8 +38,8 @@ var (
noField = O.None[string]()
// FormMonoid is the [M.Monoid] for the [FormEndomorphism]
FormMonoid = ENDO.Monoid[url.Values]()
// FormMonoid is the [M.Monoid] for the [Endomorphism]
Monoid = ENDO.Monoid[url.Values]()
// AtValues is a [L.Lens] that focusses on the values of a form field
AtValues = LRG.AtRecord[url.Values, []string]
@@ -57,7 +57,7 @@ var (
)
// WithValue creates a [FormBuilder] for a certain field
func WithValue(name string) func(value string) FormEndomorphism {
func WithValue(name string) func(value string) Endomorphism {
return F.Flow3(
O.Of[string],
AtValue(name).Set,
@@ -66,6 +66,6 @@ func WithValue(name string) func(value string) FormEndomorphism {
}
// WithoutValue creates a [FormBuilder] that removes a field
func WithoutValue(name string) FormEndomorphism {
func WithoutValue(name string) Endomorphism {
return AtValue(name).Set(noField)
}

View File

@@ -40,8 +40,8 @@ type (
body O.Option[IOE.IOEither[error, []byte]]
}
// BuilderEndomorphism returns an [ENDO.Endomorphism] that transforms a builder
BuilderEndomorphism = ENDO.Endomorphism[*Builder]
// Endomorphism returns an [ENDO.Endomorphism] that transforms a builder
Endomorphism = ENDO.Endomorphism[*Builder]
)
var (
@@ -50,8 +50,8 @@ var (
defaultMethod = F.Constant(http.MethodGet)
// BuilderMonoid is the [M.Monoid] for the [BuilderEndomorphism]
BuilderMonoid = ENDO.Monoid[*Builder]()
// Monoid is the [M.Monoid] for the [Endomorphism]
Monoid = ENDO.Monoid[*Builder]()
// Url is a [L.Lens] for the URL
Url = L.MakeLensRef((*Builder).GetUrl, (*Builder).SetUrl)
@@ -226,7 +226,7 @@ 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) BuilderEndomorphism {
func WithHeader(name string) func(value string) Endomorphism {
return F.Flow3(
O.Of[string],
Header(name).Set,
@@ -235,12 +235,12 @@ func WithHeader(name string) func(value string) BuilderEndomorphism {
}
// WithoutHeader creates a [BuilderBuilder] to remove a certain header
func WithoutHeader(name string) BuilderEndomorphism {
func WithoutHeader(name string) Endomorphism {
return Header(name).Set(noHeader)
}
// WithFormData creates a [BuilderBuilder] to send form data payload
func WithFormData(value url.Values) BuilderEndomorphism {
func WithFormData(value url.Values) Endomorphism {
return F.Flow2(
F.Pipe4(
value,
@@ -254,7 +254,7 @@ func WithFormData(value url.Values) BuilderEndomorphism {
}
// WithJson creates a [BuilderBuilder] to send JSON payload
func WithJson[T any](data T) BuilderEndomorphism {
func WithJson[T any](data T) Endomorphism {
return F.Flow2(
F.Pipe3(
data,