Skip to content

Commit

Permalink
fix: add missing alt methods and semigroup
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 Sep 12, 2023
1 parent c5b1bae commit 865d9fe
Show file tree
Hide file tree
Showing 43 changed files with 11,938 additions and 11,133 deletions.
203 changes: 101 additions & 102 deletions context/readerioeither/gen.go

Large diffs are not rendered by default.

6,595 changes: 3,297 additions & 3,298 deletions context/readerioeither/generic/gen.go

Large diffs are not rendered by default.

56 changes: 56 additions & 0 deletions context/readerioeither/generic/monoid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// 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 (
"context"

ET "github.com/IBM/fp-go/either"
M "github.com/IBM/fp-go/monoid"
)

func ApplicativeMonoid[GRA ~func(context.Context) GIOA, GRFA ~func(context.Context) GIOFA, GIOA ~func() ET.Either[error, A], GIOFA ~func() ET.Either[error, func(A) A], A any](
m M.Monoid[A],
) M.Monoid[GRA] {
return M.ApplicativeMonoid(
Of[GRA],
MonadMap[GRA, GRFA],
MonadAp[GRA, GRA, GRFA],
m,
)
}

func ApplicativeMonoidSeq[GRA ~func(context.Context) GIOA, GRFA ~func(context.Context) GIOFA, GIOA ~func() ET.Either[error, A], GIOFA ~func() ET.Either[error, func(A) A], A any](
m M.Monoid[A],
) M.Monoid[GRA] {
return M.ApplicativeMonoid(
Of[GRA],
MonadMap[GRA, GRFA],
MonadApSeq[GRA, GRA, GRFA],
m,
)
}

func ApplicativeMonoidPar[GRA ~func(context.Context) GIOA, GRFA ~func(context.Context) GIOFA, GIOA ~func() ET.Either[error, A], GIOFA ~func() ET.Either[error, func(A) A], A any](
m M.Monoid[A],
) M.Monoid[GRA] {
return M.ApplicativeMonoid(
Of[GRA],
MonadMap[GRA, GRFA],
MonadApPar[GRA, GRA, GRFA],
m,
)
}
8 changes: 8 additions & 0 deletions context/readerioeither/generic/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,3 +543,11 @@ func TryCatch[
A any](f func(context.Context) func() (A, error)) GRA {
return RIE.TryCatch[GRA](f, ER.IdentityError)
}

func MonadAlt[LAZY ~func() GEA, GEA ~func(context.Context) GIOA, GIOA ~func() E.Either[error, A], A any](first GEA, second LAZY) GEA {
return RIE.MonadAlt(first, second)
}

func Alt[LAZY ~func() GEA, GEA ~func(context.Context) GIOA, GIOA ~func() E.Either[error, A], A any](second LAZY) func(GEA) GEA {
return RIE.Alt(second)
}
29 changes: 29 additions & 0 deletions context/readerioeither/generic/semigroup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// 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 (
"context"

ET "github.com/IBM/fp-go/either"
S "github.com/IBM/fp-go/semigroup"
)

func AltSemigroup[GRA ~func(context.Context) GIOA, GIOA ~func() ET.Either[error, A], A any]() S.Semigroup[GRA] {
return S.AltSemigroup(
MonadAlt[func() GRA],
)
}
36 changes: 36 additions & 0 deletions context/readerioeither/monoid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// 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 readerioeither

import (
G "github.com/IBM/fp-go/context/readerioeither/generic"
M "github.com/IBM/fp-go/monoid"
)

// ApplicativeMonoid returns a [Monoid] that concatenates [ReaderIOEither] instances via their applicative
func ApplicativeMonoid[A any](m M.Monoid[A]) M.Monoid[ReaderIOEither[A]] {
return G.ApplicativeMonoid[ReaderIOEither[A], ReaderIOEither[func(A) A]](m)
}

// ApplicativeMonoidSeq returns a [Monoid] that concatenates [ReaderIOEither] instances via their applicative
func ApplicativeMonoidSeq[A any](m M.Monoid[A]) M.Monoid[ReaderIOEither[A]] {
return G.ApplicativeMonoidSeq[ReaderIOEither[A], ReaderIOEither[func(A) A]](m)
}

// ApplicativeMonoidPar returns a [Monoid] that concatenates [ReaderIOEither] instances via their applicative
func ApplicativeMonoidPar[A any](m M.Monoid[A]) M.Monoid[ReaderIOEither[A]] {
return G.ApplicativeMonoidPar[ReaderIOEither[A], ReaderIOEither[func(A) A]](m)
}
13 changes: 12 additions & 1 deletion context/readerioeither/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
ET "github.com/IBM/fp-go/either"
IO "github.com/IBM/fp-go/io"
IOE "github.com/IBM/fp-go/ioeither"
L "github.com/IBM/fp-go/lazy"
O "github.com/IBM/fp-go/option"
)

Expand Down Expand Up @@ -182,11 +183,21 @@ func Timer(delay time.Duration) ReaderIOEither[time.Time] {
}

// Defer creates an IO by creating a brand new IO via a generator function, each time
func Defer[A any](gen func() ReaderIOEither[A]) ReaderIOEither[A] {
func Defer[A any](gen L.Lazy[ReaderIOEither[A]]) ReaderIOEither[A] {
return G.Defer[ReaderIOEither[A]](gen)
}

// TryCatch wraps a reader returning a tuple as an error into ReaderIOEither
func TryCatch[A any](f func(context.Context) func() (A, error)) ReaderIOEither[A] {
return G.TryCatch[ReaderIOEither[A]](f)
}

// MonadAlt identifies an associative operation on a type constructor
func MonadAlt[A any](first ReaderIOEither[A], second L.Lazy[ReaderIOEither[A]]) ReaderIOEither[A] {
return G.MonadAlt(first, second)
}

// Alt identifies an associative operation on a type constructor
func Alt[A any](second L.Lazy[ReaderIOEither[A]]) func(ReaderIOEither[A]) ReaderIOEither[A] {
return G.Alt(second)
}
26 changes: 26 additions & 0 deletions context/readerioeither/semigroup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 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 readerioeither

import (
G "github.com/IBM/fp-go/context/readerioeither/generic"
S "github.com/IBM/fp-go/semigroup"
)

// AltSemigroup is a [Semigroup] that tries the first item and then the second one using an alternative
func AltSemigroup[A any]() S.Semigroup[ReaderIOEither[A]] {
return G.AltSemigroup[ReaderIOEither[A]]()
}
2 changes: 2 additions & 0 deletions coverage.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@echo off
go tool cover -html=build/cover.out -o build/cover.html
1 change: 1 addition & 0 deletions either/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func ApplySemigroup[E, A any](s S.Semigroup[A]) S.Semigroup[Either[E, A]] {
return S.ApplySemigroup(MonadMap[E, A, func(A) A], MonadAp[A, E, A], s)
}

// ApplicativeMonoid returns a [Monoid] that concatenates [Either] instances via their applicative
func ApplicativeMonoid[E, A any](m M.Monoid[A]) M.Monoid[Either[E, A]] {
return M.ApplicativeMonoid(Of[E, A], MonadMap[E, A, func(A) A], MonadAp[A, E, A], m)
}
Loading

0 comments on commit 865d9fe

Please sign in to comment.