Skip to content

Commit

Permalink
fix: add basic implementation of StateReaderIOEither
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 Feb 17, 2024
1 parent 65e09e0 commit 045f4e8
Show file tree
Hide file tree
Showing 4 changed files with 297 additions and 0 deletions.
123 changes: 123 additions & 0 deletions internal/statet/state.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// Copyright (c) 2024 IBM Corp.
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package state

import (
F "github.com/IBM/fp-go/function"
P "github.com/IBM/fp-go/pair"
)

func Of[
HKTSA ~func(S) HKTA,
HKTA,
S, A any,
](
fof func(P.Pair[A, S]) HKTA,

a A) HKTSA {

return F.Flow2(
F.Bind1st(P.MakePair[A, S], a),
fof,
)
}

func MonadMap[
HKTSA ~func(S) HKTA,
HKTSB ~func(S) HKTB,
HKTA,
HKTB,
S, A, B any,
](
fmap func(HKTA, func(P.Pair[A, S]) P.Pair[B, S]) HKTB,

fa HKTSA,
f func(A) B,
) HKTSB {

return F.Flow2(
fa,
F.Bind2nd(fmap, func(a P.Pair[A, S]) P.Pair[B, S] {
return P.MakePair(f(P.Head(a)), P.Tail(a))
}),
)
}

func Map[
HKTSA ~func(S) HKTA,
HKTSB ~func(S) HKTB,
HKTA,
HKTB,
S, A, B any,
](
fmap func(func(P.Pair[A, S]) P.Pair[B, S]) func(HKTA) HKTB,

f func(A) B,
) func(HKTSA) HKTSB {
mp := fmap(func(a P.Pair[A, S]) P.Pair[B, S] {
return P.MakePair(f(P.Head(a)), P.Tail(a))
})

return func(fa HKTSA) HKTSB {
return F.Flow2(
fa,
mp,
)
}
}

func MonadChain[
HKTSA ~func(S) HKTA,
HKTSB ~func(S) HKTB,
HKTA,
HKTB,
S, A any,
](
fchain func(HKTA, func(P.Pair[A, S]) HKTB) HKTB,

fa HKTSA,
f func(A) HKTSB,
) HKTSB {
return F.Flow2(
fa,
F.Bind2nd(fchain, func(a P.Pair[A, S]) HKTB {
return f(P.Head(a))(P.Tail(a))
}),
)
}

func Chain[
HKTSA ~func(S) HKTA,
HKTSB ~func(S) HKTB,
HKTA,
HKTB,
S, A any,
](
fchain func(func(P.Pair[A, S]) HKTB) func(HKTA) HKTB,

f func(A) HKTSB,
) func(HKTSA) HKTSB {
mp := fchain(func(a P.Pair[A, S]) HKTB {
return f(P.Head(a))(P.Tail(a))
})

return func(fa HKTSA) HKTSB {
return F.Flow2(
fa,
mp,
)
}
}
106 changes: 106 additions & 0 deletions statereaderioeither/generic/state.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Copyright (c) 2024 IBM Corp.
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package generic

import (
ET "github.com/IBM/fp-go/either"
ST "github.com/IBM/fp-go/internal/statet"
P "github.com/IBM/fp-go/pair"
G "github.com/IBM/fp-go/readerioeither/generic"
)

func Right[
SRIOEA ~func(S) RIOEA,
RIOEA ~func(R) IOEA,
IOEA ~func() ET.Either[E, P.Pair[A, S]],
S, R, E, A any,
](a A) SRIOEA {
return ST.Of[SRIOEA](
G.Of[RIOEA],
a,
)
}

func Of[
SRIOEA ~func(S) RIOEA,
RIOEA ~func(R) IOEA,
IOEA ~func() ET.Either[E, P.Pair[A, S]],
S, R, E, A any,
](a A) SRIOEA {
return Right[SRIOEA](a)
}

func MonadMap[
SRIOEA ~func(S) RIOEA,
SRIOEB ~func(S) RIOEB,
RIOEA ~func(R) IOEA,
RIOEB ~func(R) IOEB,
IOEA ~func() ET.Either[E, P.Pair[A, S]],
IOEB ~func() ET.Either[E, P.Pair[B, S]],
S, R, E, A, B any,
](fa SRIOEA, f func(A) B) SRIOEB {
return ST.MonadMap[SRIOEA, SRIOEB](
G.MonadMap[RIOEA, RIOEB],
fa,
f,
)
}

func Map[
SRIOEA ~func(S) RIOEA,
SRIOEB ~func(S) RIOEB,
RIOEA ~func(R) IOEA,
RIOEB ~func(R) IOEB,
IOEA ~func() ET.Either[E, P.Pair[A, S]],
IOEB ~func() ET.Either[E, P.Pair[B, S]],
S, R, E, A, B any,
](f func(A) B) func(SRIOEA) SRIOEB {
return ST.Map[SRIOEA, SRIOEB](
G.Map[RIOEA, RIOEB],
f,
)
}

func MonadChain[
SRIOEA ~func(S) RIOEA,
SRIOEB ~func(S) RIOEB,
RIOEA ~func(R) IOEA,
RIOEB ~func(R) IOEB,
IOEA ~func() ET.Either[E, P.Pair[A, S]],
IOEB ~func() ET.Either[E, P.Pair[B, S]],
S, R, E, A, B any,
](fa SRIOEA, f func(A) SRIOEB) SRIOEB {
return ST.MonadChain[SRIOEA, SRIOEB](
G.MonadChain[RIOEA, RIOEB],
fa,
f,
)
}

func Chain[
SRIOEA ~func(S) RIOEA,
SRIOEB ~func(S) RIOEB,
RIOEA ~func(R) IOEA,
RIOEB ~func(R) IOEB,
IOEA ~func() ET.Either[E, P.Pair[A, S]],
IOEB ~func() ET.Either[E, P.Pair[B, S]],
S, R, E, A, B any,
](f func(A) SRIOEB) func(SRIOEA) SRIOEB {
return ST.Chain[SRIOEA, SRIOEB](
G.Chain[RIOEA, RIOEB],
f,
)
}
44 changes: 44 additions & 0 deletions statereaderioeither/state.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) 2024 IBM Corp.
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package statereaderioeither

import (
G "github.com/IBM/fp-go/statereaderioeither/generic"
)

func Right[S, R, E, A any](a A) StateReaderIOEither[S, R, E, A] {
return G.Right[StateReaderIOEither[S, R, E, A]](a)
}

func Of[S, R, E, A any](a A) StateReaderIOEither[S, R, E, A] {
return G.Of[StateReaderIOEither[S, R, E, A]](a)
}

func MonadMap[S, R, E, A, B any](fa StateReaderIOEither[S, R, E, A], f func(A) B) StateReaderIOEither[S, R, E, B] {
return G.MonadMap[StateReaderIOEither[S, R, E, A], StateReaderIOEither[S, R, E, B]](fa, f)
}

func Map[S, R, E, A, B any](f func(A) B) func(StateReaderIOEither[S, R, E, A]) StateReaderIOEither[S, R, E, B] {
return G.Map[StateReaderIOEither[S, R, E, A], StateReaderIOEither[S, R, E, B]](f)
}

func MonadChain[S, R, E, A, B any](fa StateReaderIOEither[S, R, E, A], f func(A) StateReaderIOEither[S, R, E, B]) StateReaderIOEither[S, R, E, B] {
return G.MonadChain[StateReaderIOEither[S, R, E, A], StateReaderIOEither[S, R, E, B]](fa, f)
}

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

package statereaderioeither

import (
P "github.com/IBM/fp-go/pair"
RD "github.com/IBM/fp-go/reader"
RIOE "github.com/IBM/fp-go/readerioeither"
)

type StateReaderIOEither[S, R, E, A any] RD.Reader[S, RIOE.ReaderIOEither[R, E, P.Pair[A, S]]]

0 comments on commit 045f4e8

Please sign in to comment.