mirror of
https://github.com/IBM/fp-go.git
synced 2025-08-24 19:29:11 +02:00
Compare commits
29 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
1cdca552b2 | ||
|
73480ca030 | ||
|
734e2b0055 | ||
|
4c28859e89 | ||
|
a516849c07 | ||
|
29200d34dc | ||
|
7a3989989b | ||
|
6a6d53f025 | ||
|
078da752cd | ||
|
1a489fde27 | ||
|
a135b2acae | ||
|
9e9dfa1f5f | ||
|
dd87ea12b3 | ||
|
5fc0d18c97 | ||
|
76c1297576 | ||
|
68aeb4c725 | ||
|
53f3fa1828 | ||
|
97e1e4d92d | ||
|
ec57d5cd4a | ||
|
0ae5b43724 | ||
|
e73e14c0ae | ||
|
325bc376f9 | ||
|
f646ace9fe | ||
|
9f8161fbc1 | ||
|
1c4f2c0403 | ||
|
3b3b80aed0 | ||
|
fdff4e4735 | ||
|
391754e5a6 | ||
|
598a7b261b |
12
.github/workflows/build.yml
vendored
12
.github/workflows/build.yml
vendored
@@ -18,7 +18,7 @@ env:
|
||||
# Currently no way to detect automatically
|
||||
DEFAULT_BRANCH: main
|
||||
GO_VERSION: 1.21.6 # renovate: datasource=golang-version depName=golang
|
||||
NODE_VERSION: 20
|
||||
NODE_VERSION: 22
|
||||
DRY_RUN: true
|
||||
|
||||
jobs:
|
||||
@@ -26,10 +26,10 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
go-version: [ '1.20.x', '1.21.x', '1.22.x']
|
||||
go-version: [ '1.20.x', '1.21.x', '1.22.x', '1.23.x']
|
||||
steps:
|
||||
# full checkout for semantic-release
|
||||
- uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6
|
||||
- uses: actions/checkout@08eba0b27e820071cde6df949e0beb9ba4906955 # v4.3.0
|
||||
with:
|
||||
fetch-depth: 0
|
||||
- name: Set up go ${{ matrix.go-version }}
|
||||
@@ -55,12 +55,12 @@ jobs:
|
||||
steps:
|
||||
# full checkout for semantic-release
|
||||
- name: Full checkout
|
||||
uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6
|
||||
uses: actions/checkout@08eba0b27e820071cde6df949e0beb9ba4906955 # v4.3.0
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Set up Node.js ${{ env.NODE_VERSION }}
|
||||
uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4.0.2
|
||||
uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0
|
||||
with:
|
||||
node-version: ${{ env.NODE_VERSION }}
|
||||
|
||||
@@ -82,7 +82,7 @@ jobs:
|
||||
|
||||
- name: Semantic Release
|
||||
run: |
|
||||
npx -p "conventional-changelog-conventionalcommits@<8" -p semantic-release semantic-release --dry-run ${{env.DRY_RUN}}
|
||||
npx -p conventional-changelog-conventionalcommits -p semantic-release semantic-release --dry-run ${{env.DRY_RUN}}
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
|
||||
|
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,4 +1,5 @@
|
||||
fp-go.exe
|
||||
fp-go
|
||||
main.exe
|
||||
build/
|
||||
.idea
|
@@ -29,9 +29,9 @@ This library aims to provide a set of data types and functions that make it easy
|
||||
|
||||
### How does this play with the [🧘🏽 Zen Of Go](https://the-zen-of-go.netlify.app/)?
|
||||
|
||||
#### 🧘🏽 Each package fulfils a single purpose
|
||||
#### 🧘🏽 Each package fulfills a single purpose
|
||||
|
||||
✔️ Each of the top level packages (e.g. Option, Either, ReaderIOEither, ...) fulfils the purpose of defining the respective data type and implementing the set of common operations for this data type.
|
||||
✔️ Each of the top level packages (e.g. Option, Either, ReaderIOEither, ...) fulfills the purpose of defining the respective data type and implementing the set of common operations for this data type.
|
||||
|
||||
#### 🧘🏽 Handle errors explicitly
|
||||
|
||||
|
@@ -365,6 +365,70 @@ func generateContextReaderIOEitherEitherize(f, fg *os.File, i int) {
|
||||
fmt.Fprintln(fg, "}")
|
||||
}
|
||||
|
||||
func generateContextReaderIOEitherUneitherize(f, fg *os.File, i int) {
|
||||
// non generic version
|
||||
fmt.Fprintf(f, "\n// Uneitherize%d converts a function with %d parameters returning a [ReaderIOEither[R]] into a function with %d parameters returning a tuple.\n// The first parameter is considered to be the [context.Context].\n", i, i+1, i)
|
||||
fmt.Fprintf(f, "func Uneitherize%d[F ~func(", i)
|
||||
for j := 0; j < i; j++ {
|
||||
if j > 0 {
|
||||
fmt.Fprintf(f, ", ")
|
||||
}
|
||||
fmt.Fprintf(f, "T%d", j)
|
||||
}
|
||||
fmt.Fprintf(f, ") ReaderIOEither[R]")
|
||||
for j := 0; j < i; j++ {
|
||||
fmt.Fprintf(f, ", T%d", j)
|
||||
}
|
||||
fmt.Fprintf(f, ", R any](f F) func(context.Context")
|
||||
for j := 0; j < i; j++ {
|
||||
fmt.Fprintf(f, ", T%d", j)
|
||||
}
|
||||
fmt.Fprintf(f, ") (R, error) {\n")
|
||||
fmt.Fprintf(f, " return G.Uneitherize%d[ReaderIOEither[R]", i)
|
||||
|
||||
fmt.Fprintf(f, ", func(context.Context")
|
||||
for j := 0; j < i; j++ {
|
||||
fmt.Fprintf(f, ", T%d", j)
|
||||
}
|
||||
fmt.Fprintf(f, ")(R, error)](f)\n")
|
||||
fmt.Fprintln(f, "}")
|
||||
|
||||
// generic version
|
||||
fmt.Fprintf(fg, "\n// Uneitherize%d converts a function with %d parameters returning a [GRA] into a function with %d parameters returning a tuple.\n// The first parameter is considered to be the [context.Context].\n", i, i, i)
|
||||
fmt.Fprintf(fg, "func Uneitherize%d[GRA ~func(context.Context) GIOA, F ~func(context.Context", i)
|
||||
for j := 0; j < i; j++ {
|
||||
fmt.Fprintf(fg, ", T%d", j)
|
||||
}
|
||||
fmt.Fprintf(fg, ") (R, error), GIOA ~func() E.Either[error, R]")
|
||||
for j := 0; j < i; j++ {
|
||||
fmt.Fprintf(fg, ", T%d", j)
|
||||
}
|
||||
fmt.Fprintf(fg, ", R any](f func(")
|
||||
for j := 0; j < i; j++ {
|
||||
if j > 0 {
|
||||
fmt.Fprintf(fg, ", ")
|
||||
}
|
||||
fmt.Fprintf(fg, "T%d", j)
|
||||
}
|
||||
fmt.Fprintf(fg, ") GRA) F {\n")
|
||||
|
||||
fmt.Fprintf(fg, " return func(c context.Context")
|
||||
for j := 0; j < i; j++ {
|
||||
fmt.Fprintf(fg, ", t%d T%d", j, j)
|
||||
}
|
||||
fmt.Fprintf(fg, ") (R, error) {\n")
|
||||
fmt.Fprintf(fg, " return E.UnwrapError(f(")
|
||||
for j := 0; j < i; j++ {
|
||||
if j > 0 {
|
||||
fmt.Fprintf(fg, ", ")
|
||||
}
|
||||
fmt.Fprintf(fg, "t%d", j)
|
||||
}
|
||||
fmt.Fprintf(fg, ")(c)())\n")
|
||||
fmt.Fprintf(fg, " }\n")
|
||||
fmt.Fprintf(fg, "}\n")
|
||||
}
|
||||
|
||||
func generateContextReaderIOEitherHelpers(filename string, count int) error {
|
||||
dir, err := os.Getwd()
|
||||
if err != nil {
|
||||
@@ -420,10 +484,12 @@ import (
|
||||
`)
|
||||
|
||||
generateContextReaderIOEitherEitherize(f, fg, 0)
|
||||
generateContextReaderIOEitherUneitherize(f, fg, 0)
|
||||
|
||||
for i := 1; i <= count; i++ {
|
||||
// eitherize
|
||||
generateContextReaderIOEitherEitherize(f, fg, i)
|
||||
generateContextReaderIOEitherUneitherize(f, fg, i)
|
||||
// sequenceT
|
||||
generateContextReaderIOEitherSequenceT("")(f, fg, i)
|
||||
generateContextReaderIOEitherSequenceT("Seq")(f, fg, i)
|
||||
|
15
cli/templates/functions.go
Normal file
15
cli/templates/functions.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package templates
|
||||
|
||||
import (
|
||||
"text/template"
|
||||
|
||||
E "github.com/IBM/fp-go/either"
|
||||
)
|
||||
|
||||
var (
|
||||
templateFunctions = template.FuncMap{}
|
||||
)
|
||||
|
||||
func Parse(name, tmpl string) E.Either[error, *template.Template] {
|
||||
return E.TryCatchError(template.New(name).Funcs(templateFunctions).Parse(tmpl))
|
||||
}
|
@@ -2,7 +2,7 @@ package readerioeither
|
||||
|
||||
// Code generated by go generate; DO NOT EDIT.
|
||||
// This file was generated by robots at
|
||||
// 2024-02-29 16:18:46.1201552 +0100 CET m=+0.020664901
|
||||
// 2024-05-24 22:24:01.4250895 +0200 CEST m=+0.014515801
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -17,12 +17,24 @@ func Eitherize0[F ~func(context.Context) (R, error), R any](f F) func() ReaderIO
|
||||
return G.Eitherize0[ReaderIOEither[R]](f)
|
||||
}
|
||||
|
||||
// Uneitherize0 converts a function with 1 parameters returning a [ReaderIOEither[R]] into a function with 0 parameters returning a tuple.
|
||||
// The first parameter is considered to be the [context.Context].
|
||||
func Uneitherize0[F ~func() ReaderIOEither[R], R any](f F) func(context.Context) (R, error) {
|
||||
return G.Uneitherize0[ReaderIOEither[R], func(context.Context) (R, error)](f)
|
||||
}
|
||||
|
||||
// Eitherize1 converts a function with 1 parameters returning a tuple into a function with 1 parameters returning a [ReaderIOEither[R]]
|
||||
// The inverse function is [Uneitherize1]
|
||||
func Eitherize1[F ~func(context.Context, T0) (R, error), T0, R any](f F) func(T0) ReaderIOEither[R] {
|
||||
return G.Eitherize1[ReaderIOEither[R]](f)
|
||||
}
|
||||
|
||||
// Uneitherize1 converts a function with 2 parameters returning a [ReaderIOEither[R]] into a function with 1 parameters returning a tuple.
|
||||
// The first parameter is considered to be the [context.Context].
|
||||
func Uneitherize1[F ~func(T0) ReaderIOEither[R], T0, R any](f F) func(context.Context, T0) (R, error) {
|
||||
return G.Uneitherize1[ReaderIOEither[R], func(context.Context, T0) (R, error)](f)
|
||||
}
|
||||
|
||||
// SequenceT1 converts 1 [ReaderIOEither] into a [ReaderIOEither] of a [T.Tuple1].
|
||||
func SequenceT1[T1 any](t1 ReaderIOEither[T1]) ReaderIOEither[T.Tuple1[T1]] {
|
||||
return G.SequenceT1[ReaderIOEither[T.Tuple1[T1]]](t1)
|
||||
@@ -74,6 +86,12 @@ func Eitherize2[F ~func(context.Context, T0, T1) (R, error), T0, T1, R any](f F)
|
||||
return G.Eitherize2[ReaderIOEither[R]](f)
|
||||
}
|
||||
|
||||
// Uneitherize2 converts a function with 3 parameters returning a [ReaderIOEither[R]] into a function with 2 parameters returning a tuple.
|
||||
// The first parameter is considered to be the [context.Context].
|
||||
func Uneitherize2[F ~func(T0, T1) ReaderIOEither[R], T0, T1, R any](f F) func(context.Context, T0, T1) (R, error) {
|
||||
return G.Uneitherize2[ReaderIOEither[R], func(context.Context, T0, T1) (R, error)](f)
|
||||
}
|
||||
|
||||
// SequenceT2 converts 2 [ReaderIOEither] into a [ReaderIOEither] of a [T.Tuple2].
|
||||
func SequenceT2[T1, T2 any](t1 ReaderIOEither[T1], t2 ReaderIOEither[T2]) ReaderIOEither[T.Tuple2[T1, T2]] {
|
||||
return G.SequenceT2[ReaderIOEither[T.Tuple2[T1, T2]]](t1, t2)
|
||||
@@ -125,6 +143,12 @@ func Eitherize3[F ~func(context.Context, T0, T1, T2) (R, error), T0, T1, T2, R a
|
||||
return G.Eitherize3[ReaderIOEither[R]](f)
|
||||
}
|
||||
|
||||
// Uneitherize3 converts a function with 4 parameters returning a [ReaderIOEither[R]] into a function with 3 parameters returning a tuple.
|
||||
// The first parameter is considered to be the [context.Context].
|
||||
func Uneitherize3[F ~func(T0, T1, T2) ReaderIOEither[R], T0, T1, T2, R any](f F) func(context.Context, T0, T1, T2) (R, error) {
|
||||
return G.Uneitherize3[ReaderIOEither[R], func(context.Context, T0, T1, T2) (R, error)](f)
|
||||
}
|
||||
|
||||
// SequenceT3 converts 3 [ReaderIOEither] into a [ReaderIOEither] of a [T.Tuple3].
|
||||
func SequenceT3[T1, T2, T3 any](t1 ReaderIOEither[T1], t2 ReaderIOEither[T2], t3 ReaderIOEither[T3]) ReaderIOEither[T.Tuple3[T1, T2, T3]] {
|
||||
return G.SequenceT3[ReaderIOEither[T.Tuple3[T1, T2, T3]]](t1, t2, t3)
|
||||
@@ -176,6 +200,12 @@ func Eitherize4[F ~func(context.Context, T0, T1, T2, T3) (R, error), T0, T1, T2,
|
||||
return G.Eitherize4[ReaderIOEither[R]](f)
|
||||
}
|
||||
|
||||
// Uneitherize4 converts a function with 5 parameters returning a [ReaderIOEither[R]] into a function with 4 parameters returning a tuple.
|
||||
// The first parameter is considered to be the [context.Context].
|
||||
func Uneitherize4[F ~func(T0, T1, T2, T3) ReaderIOEither[R], T0, T1, T2, T3, R any](f F) func(context.Context, T0, T1, T2, T3) (R, error) {
|
||||
return G.Uneitherize4[ReaderIOEither[R], func(context.Context, T0, T1, T2, T3) (R, error)](f)
|
||||
}
|
||||
|
||||
// SequenceT4 converts 4 [ReaderIOEither] into a [ReaderIOEither] of a [T.Tuple4].
|
||||
func SequenceT4[T1, T2, T3, T4 any](t1 ReaderIOEither[T1], t2 ReaderIOEither[T2], t3 ReaderIOEither[T3], t4 ReaderIOEither[T4]) ReaderIOEither[T.Tuple4[T1, T2, T3, T4]] {
|
||||
return G.SequenceT4[ReaderIOEither[T.Tuple4[T1, T2, T3, T4]]](t1, t2, t3, t4)
|
||||
@@ -227,6 +257,12 @@ func Eitherize5[F ~func(context.Context, T0, T1, T2, T3, T4) (R, error), T0, T1,
|
||||
return G.Eitherize5[ReaderIOEither[R]](f)
|
||||
}
|
||||
|
||||
// Uneitherize5 converts a function with 6 parameters returning a [ReaderIOEither[R]] into a function with 5 parameters returning a tuple.
|
||||
// The first parameter is considered to be the [context.Context].
|
||||
func Uneitherize5[F ~func(T0, T1, T2, T3, T4) ReaderIOEither[R], T0, T1, T2, T3, T4, R any](f F) func(context.Context, T0, T1, T2, T3, T4) (R, error) {
|
||||
return G.Uneitherize5[ReaderIOEither[R], func(context.Context, T0, T1, T2, T3, T4) (R, error)](f)
|
||||
}
|
||||
|
||||
// SequenceT5 converts 5 [ReaderIOEither] into a [ReaderIOEither] of a [T.Tuple5].
|
||||
func SequenceT5[T1, T2, T3, T4, T5 any](t1 ReaderIOEither[T1], t2 ReaderIOEither[T2], t3 ReaderIOEither[T3], t4 ReaderIOEither[T4], t5 ReaderIOEither[T5]) ReaderIOEither[T.Tuple5[T1, T2, T3, T4, T5]] {
|
||||
return G.SequenceT5[ReaderIOEither[T.Tuple5[T1, T2, T3, T4, T5]]](t1, t2, t3, t4, t5)
|
||||
@@ -278,6 +314,12 @@ func Eitherize6[F ~func(context.Context, T0, T1, T2, T3, T4, T5) (R, error), T0,
|
||||
return G.Eitherize6[ReaderIOEither[R]](f)
|
||||
}
|
||||
|
||||
// Uneitherize6 converts a function with 7 parameters returning a [ReaderIOEither[R]] into a function with 6 parameters returning a tuple.
|
||||
// The first parameter is considered to be the [context.Context].
|
||||
func Uneitherize6[F ~func(T0, T1, T2, T3, T4, T5) ReaderIOEither[R], T0, T1, T2, T3, T4, T5, R any](f F) func(context.Context, T0, T1, T2, T3, T4, T5) (R, error) {
|
||||
return G.Uneitherize6[ReaderIOEither[R], func(context.Context, T0, T1, T2, T3, T4, T5) (R, error)](f)
|
||||
}
|
||||
|
||||
// SequenceT6 converts 6 [ReaderIOEither] into a [ReaderIOEither] of a [T.Tuple6].
|
||||
func SequenceT6[T1, T2, T3, T4, T5, T6 any](t1 ReaderIOEither[T1], t2 ReaderIOEither[T2], t3 ReaderIOEither[T3], t4 ReaderIOEither[T4], t5 ReaderIOEither[T5], t6 ReaderIOEither[T6]) ReaderIOEither[T.Tuple6[T1, T2, T3, T4, T5, T6]] {
|
||||
return G.SequenceT6[ReaderIOEither[T.Tuple6[T1, T2, T3, T4, T5, T6]]](t1, t2, t3, t4, t5, t6)
|
||||
@@ -329,6 +371,12 @@ func Eitherize7[F ~func(context.Context, T0, T1, T2, T3, T4, T5, T6) (R, error),
|
||||
return G.Eitherize7[ReaderIOEither[R]](f)
|
||||
}
|
||||
|
||||
// Uneitherize7 converts a function with 8 parameters returning a [ReaderIOEither[R]] into a function with 7 parameters returning a tuple.
|
||||
// The first parameter is considered to be the [context.Context].
|
||||
func Uneitherize7[F ~func(T0, T1, T2, T3, T4, T5, T6) ReaderIOEither[R], T0, T1, T2, T3, T4, T5, T6, R any](f F) func(context.Context, T0, T1, T2, T3, T4, T5, T6) (R, error) {
|
||||
return G.Uneitherize7[ReaderIOEither[R], func(context.Context, T0, T1, T2, T3, T4, T5, T6) (R, error)](f)
|
||||
}
|
||||
|
||||
// SequenceT7 converts 7 [ReaderIOEither] into a [ReaderIOEither] of a [T.Tuple7].
|
||||
func SequenceT7[T1, T2, T3, T4, T5, T6, T7 any](t1 ReaderIOEither[T1], t2 ReaderIOEither[T2], t3 ReaderIOEither[T3], t4 ReaderIOEither[T4], t5 ReaderIOEither[T5], t6 ReaderIOEither[T6], t7 ReaderIOEither[T7]) ReaderIOEither[T.Tuple7[T1, T2, T3, T4, T5, T6, T7]] {
|
||||
return G.SequenceT7[ReaderIOEither[T.Tuple7[T1, T2, T3, T4, T5, T6, T7]]](t1, t2, t3, t4, t5, t6, t7)
|
||||
@@ -380,6 +428,12 @@ func Eitherize8[F ~func(context.Context, T0, T1, T2, T3, T4, T5, T6, T7) (R, err
|
||||
return G.Eitherize8[ReaderIOEither[R]](f)
|
||||
}
|
||||
|
||||
// Uneitherize8 converts a function with 9 parameters returning a [ReaderIOEither[R]] into a function with 8 parameters returning a tuple.
|
||||
// The first parameter is considered to be the [context.Context].
|
||||
func Uneitherize8[F ~func(T0, T1, T2, T3, T4, T5, T6, T7) ReaderIOEither[R], T0, T1, T2, T3, T4, T5, T6, T7, R any](f F) func(context.Context, T0, T1, T2, T3, T4, T5, T6, T7) (R, error) {
|
||||
return G.Uneitherize8[ReaderIOEither[R], func(context.Context, T0, T1, T2, T3, T4, T5, T6, T7) (R, error)](f)
|
||||
}
|
||||
|
||||
// SequenceT8 converts 8 [ReaderIOEither] into a [ReaderIOEither] of a [T.Tuple8].
|
||||
func SequenceT8[T1, T2, T3, T4, T5, T6, T7, T8 any](t1 ReaderIOEither[T1], t2 ReaderIOEither[T2], t3 ReaderIOEither[T3], t4 ReaderIOEither[T4], t5 ReaderIOEither[T5], t6 ReaderIOEither[T6], t7 ReaderIOEither[T7], t8 ReaderIOEither[T8]) ReaderIOEither[T.Tuple8[T1, T2, T3, T4, T5, T6, T7, T8]] {
|
||||
return G.SequenceT8[ReaderIOEither[T.Tuple8[T1, T2, T3, T4, T5, T6, T7, T8]]](t1, t2, t3, t4, t5, t6, t7, t8)
|
||||
@@ -431,6 +485,12 @@ func Eitherize9[F ~func(context.Context, T0, T1, T2, T3, T4, T5, T6, T7, T8) (R,
|
||||
return G.Eitherize9[ReaderIOEither[R]](f)
|
||||
}
|
||||
|
||||
// Uneitherize9 converts a function with 10 parameters returning a [ReaderIOEither[R]] into a function with 9 parameters returning a tuple.
|
||||
// The first parameter is considered to be the [context.Context].
|
||||
func Uneitherize9[F ~func(T0, T1, T2, T3, T4, T5, T6, T7, T8) ReaderIOEither[R], T0, T1, T2, T3, T4, T5, T6, T7, T8, R any](f F) func(context.Context, T0, T1, T2, T3, T4, T5, T6, T7, T8) (R, error) {
|
||||
return G.Uneitherize9[ReaderIOEither[R], func(context.Context, T0, T1, T2, T3, T4, T5, T6, T7, T8) (R, error)](f)
|
||||
}
|
||||
|
||||
// SequenceT9 converts 9 [ReaderIOEither] into a [ReaderIOEither] of a [T.Tuple9].
|
||||
func SequenceT9[T1, T2, T3, T4, T5, T6, T7, T8, T9 any](t1 ReaderIOEither[T1], t2 ReaderIOEither[T2], t3 ReaderIOEither[T3], t4 ReaderIOEither[T4], t5 ReaderIOEither[T5], t6 ReaderIOEither[T6], t7 ReaderIOEither[T7], t8 ReaderIOEither[T8], t9 ReaderIOEither[T9]) ReaderIOEither[T.Tuple9[T1, T2, T3, T4, T5, T6, T7, T8, T9]] {
|
||||
return G.SequenceT9[ReaderIOEither[T.Tuple9[T1, T2, T3, T4, T5, T6, T7, T8, T9]]](t1, t2, t3, t4, t5, t6, t7, t8, t9)
|
||||
@@ -482,6 +542,12 @@ func Eitherize10[F ~func(context.Context, T0, T1, T2, T3, T4, T5, T6, T7, T8, T9
|
||||
return G.Eitherize10[ReaderIOEither[R]](f)
|
||||
}
|
||||
|
||||
// Uneitherize10 converts a function with 11 parameters returning a [ReaderIOEither[R]] into a function with 10 parameters returning a tuple.
|
||||
// The first parameter is considered to be the [context.Context].
|
||||
func Uneitherize10[F ~func(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9) ReaderIOEither[R], T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, R any](f F) func(context.Context, T0, T1, T2, T3, T4, T5, T6, T7, T8, T9) (R, error) {
|
||||
return G.Uneitherize10[ReaderIOEither[R], func(context.Context, T0, T1, T2, T3, T4, T5, T6, T7, T8, T9) (R, error)](f)
|
||||
}
|
||||
|
||||
// SequenceT10 converts 10 [ReaderIOEither] into a [ReaderIOEither] of a [T.Tuple10].
|
||||
func SequenceT10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10 any](t1 ReaderIOEither[T1], t2 ReaderIOEither[T2], t3 ReaderIOEither[T3], t4 ReaderIOEither[T4], t5 ReaderIOEither[T5], t6 ReaderIOEither[T6], t7 ReaderIOEither[T7], t8 ReaderIOEither[T8], t9 ReaderIOEither[T9], t10 ReaderIOEither[T10]) ReaderIOEither[T.Tuple10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]] {
|
||||
return G.SequenceT10[ReaderIOEither[T.Tuple10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]]](t1, t2, t3, t4, t5, t6, t7, t8, t9, t10)
|
||||
|
@@ -2,7 +2,7 @@ package generic
|
||||
|
||||
// Code generated by go generate; DO NOT EDIT.
|
||||
// This file was generated by robots at
|
||||
// 2024-02-29 16:18:46.1217523 +0100 CET m=+0.022262001
|
||||
// 2024-05-24 22:24:01.4250895 +0200 CEST m=+0.014515801
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -19,12 +19,28 @@ func Eitherize0[GRA ~func(context.Context) GIOA, F ~func(context.Context) (R, er
|
||||
return RE.Eitherize0[GRA](f)
|
||||
}
|
||||
|
||||
// Uneitherize0 converts a function with 0 parameters returning a [GRA] into a function with 0 parameters returning a tuple.
|
||||
// The first parameter is considered to be the [context.Context].
|
||||
func Uneitherize0[GRA ~func(context.Context) GIOA, F ~func(context.Context) (R, error), GIOA ~func() E.Either[error, R], R any](f func() GRA) F {
|
||||
return func(c context.Context) (R, error) {
|
||||
return E.UnwrapError(f()(c)())
|
||||
}
|
||||
}
|
||||
|
||||
// Eitherize1 converts a function with 1 parameters returning a tuple into a function with 1 parameters returning a [GRA]
|
||||
// The inverse function is [Uneitherize1]
|
||||
func Eitherize1[GRA ~func(context.Context) GIOA, F ~func(context.Context, T0) (R, error), GIOA ~func() E.Either[error, R], T0, R any](f F) func(T0) GRA {
|
||||
return RE.Eitherize1[GRA](f)
|
||||
}
|
||||
|
||||
// Uneitherize1 converts a function with 1 parameters returning a [GRA] into a function with 1 parameters returning a tuple.
|
||||
// The first parameter is considered to be the [context.Context].
|
||||
func Uneitherize1[GRA ~func(context.Context) GIOA, F ~func(context.Context, T0) (R, error), GIOA ~func() E.Either[error, R], T0, R any](f func(T0) GRA) F {
|
||||
return func(c context.Context, t0 T0) (R, error) {
|
||||
return E.UnwrapError(f(t0)(c)())
|
||||
}
|
||||
}
|
||||
|
||||
// SequenceT1 converts 1 readers into a reader of a [T.Tuple1].
|
||||
func SequenceT1[
|
||||
GR_TUPLE1 ~func(context.Context) GIO_TUPLE1,
|
||||
@@ -169,6 +185,14 @@ func Eitherize2[GRA ~func(context.Context) GIOA, F ~func(context.Context, T0, T1
|
||||
return RE.Eitherize2[GRA](f)
|
||||
}
|
||||
|
||||
// Uneitherize2 converts a function with 2 parameters returning a [GRA] into a function with 2 parameters returning a tuple.
|
||||
// The first parameter is considered to be the [context.Context].
|
||||
func Uneitherize2[GRA ~func(context.Context) GIOA, F ~func(context.Context, T0, T1) (R, error), GIOA ~func() E.Either[error, R], T0, T1, R any](f func(T0, T1) GRA) F {
|
||||
return func(c context.Context, t0 T0, t1 T1) (R, error) {
|
||||
return E.UnwrapError(f(t0, t1)(c)())
|
||||
}
|
||||
}
|
||||
|
||||
// SequenceT2 converts 2 readers into a reader of a [T.Tuple2].
|
||||
func SequenceT2[
|
||||
GR_TUPLE2 ~func(context.Context) GIO_TUPLE2,
|
||||
@@ -364,6 +388,14 @@ func Eitherize3[GRA ~func(context.Context) GIOA, F ~func(context.Context, T0, T1
|
||||
return RE.Eitherize3[GRA](f)
|
||||
}
|
||||
|
||||
// Uneitherize3 converts a function with 3 parameters returning a [GRA] into a function with 3 parameters returning a tuple.
|
||||
// The first parameter is considered to be the [context.Context].
|
||||
func Uneitherize3[GRA ~func(context.Context) GIOA, F ~func(context.Context, T0, T1, T2) (R, error), GIOA ~func() E.Either[error, R], T0, T1, T2, R any](f func(T0, T1, T2) GRA) F {
|
||||
return func(c context.Context, t0 T0, t1 T1, t2 T2) (R, error) {
|
||||
return E.UnwrapError(f(t0, t1, t2)(c)())
|
||||
}
|
||||
}
|
||||
|
||||
// SequenceT3 converts 3 readers into a reader of a [T.Tuple3].
|
||||
func SequenceT3[
|
||||
GR_TUPLE3 ~func(context.Context) GIO_TUPLE3,
|
||||
@@ -610,6 +642,14 @@ func Eitherize4[GRA ~func(context.Context) GIOA, F ~func(context.Context, T0, T1
|
||||
return RE.Eitherize4[GRA](f)
|
||||
}
|
||||
|
||||
// Uneitherize4 converts a function with 4 parameters returning a [GRA] into a function with 4 parameters returning a tuple.
|
||||
// The first parameter is considered to be the [context.Context].
|
||||
func Uneitherize4[GRA ~func(context.Context) GIOA, F ~func(context.Context, T0, T1, T2, T3) (R, error), GIOA ~func() E.Either[error, R], T0, T1, T2, T3, R any](f func(T0, T1, T2, T3) GRA) F {
|
||||
return func(c context.Context, t0 T0, t1 T1, t2 T2, t3 T3) (R, error) {
|
||||
return E.UnwrapError(f(t0, t1, t2, t3)(c)())
|
||||
}
|
||||
}
|
||||
|
||||
// SequenceT4 converts 4 readers into a reader of a [T.Tuple4].
|
||||
func SequenceT4[
|
||||
GR_TUPLE4 ~func(context.Context) GIO_TUPLE4,
|
||||
@@ -907,6 +947,14 @@ func Eitherize5[GRA ~func(context.Context) GIOA, F ~func(context.Context, T0, T1
|
||||
return RE.Eitherize5[GRA](f)
|
||||
}
|
||||
|
||||
// Uneitherize5 converts a function with 5 parameters returning a [GRA] into a function with 5 parameters returning a tuple.
|
||||
// The first parameter is considered to be the [context.Context].
|
||||
func Uneitherize5[GRA ~func(context.Context) GIOA, F ~func(context.Context, T0, T1, T2, T3, T4) (R, error), GIOA ~func() E.Either[error, R], T0, T1, T2, T3, T4, R any](f func(T0, T1, T2, T3, T4) GRA) F {
|
||||
return func(c context.Context, t0 T0, t1 T1, t2 T2, t3 T3, t4 T4) (R, error) {
|
||||
return E.UnwrapError(f(t0, t1, t2, t3, t4)(c)())
|
||||
}
|
||||
}
|
||||
|
||||
// SequenceT5 converts 5 readers into a reader of a [T.Tuple5].
|
||||
func SequenceT5[
|
||||
GR_TUPLE5 ~func(context.Context) GIO_TUPLE5,
|
||||
@@ -1255,6 +1303,14 @@ func Eitherize6[GRA ~func(context.Context) GIOA, F ~func(context.Context, T0, T1
|
||||
return RE.Eitherize6[GRA](f)
|
||||
}
|
||||
|
||||
// Uneitherize6 converts a function with 6 parameters returning a [GRA] into a function with 6 parameters returning a tuple.
|
||||
// The first parameter is considered to be the [context.Context].
|
||||
func Uneitherize6[GRA ~func(context.Context) GIOA, F ~func(context.Context, T0, T1, T2, T3, T4, T5) (R, error), GIOA ~func() E.Either[error, R], T0, T1, T2, T3, T4, T5, R any](f func(T0, T1, T2, T3, T4, T5) GRA) F {
|
||||
return func(c context.Context, t0 T0, t1 T1, t2 T2, t3 T3, t4 T4, t5 T5) (R, error) {
|
||||
return E.UnwrapError(f(t0, t1, t2, t3, t4, t5)(c)())
|
||||
}
|
||||
}
|
||||
|
||||
// SequenceT6 converts 6 readers into a reader of a [T.Tuple6].
|
||||
func SequenceT6[
|
||||
GR_TUPLE6 ~func(context.Context) GIO_TUPLE6,
|
||||
@@ -1654,6 +1710,14 @@ func Eitherize7[GRA ~func(context.Context) GIOA, F ~func(context.Context, T0, T1
|
||||
return RE.Eitherize7[GRA](f)
|
||||
}
|
||||
|
||||
// Uneitherize7 converts a function with 7 parameters returning a [GRA] into a function with 7 parameters returning a tuple.
|
||||
// The first parameter is considered to be the [context.Context].
|
||||
func Uneitherize7[GRA ~func(context.Context) GIOA, F ~func(context.Context, T0, T1, T2, T3, T4, T5, T6) (R, error), GIOA ~func() E.Either[error, R], T0, T1, T2, T3, T4, T5, T6, R any](f func(T0, T1, T2, T3, T4, T5, T6) GRA) F {
|
||||
return func(c context.Context, t0 T0, t1 T1, t2 T2, t3 T3, t4 T4, t5 T5, t6 T6) (R, error) {
|
||||
return E.UnwrapError(f(t0, t1, t2, t3, t4, t5, t6)(c)())
|
||||
}
|
||||
}
|
||||
|
||||
// SequenceT7 converts 7 readers into a reader of a [T.Tuple7].
|
||||
func SequenceT7[
|
||||
GR_TUPLE7 ~func(context.Context) GIO_TUPLE7,
|
||||
@@ -2104,6 +2168,14 @@ func Eitherize8[GRA ~func(context.Context) GIOA, F ~func(context.Context, T0, T1
|
||||
return RE.Eitherize8[GRA](f)
|
||||
}
|
||||
|
||||
// Uneitherize8 converts a function with 8 parameters returning a [GRA] into a function with 8 parameters returning a tuple.
|
||||
// The first parameter is considered to be the [context.Context].
|
||||
func Uneitherize8[GRA ~func(context.Context) GIOA, F ~func(context.Context, T0, T1, T2, T3, T4, T5, T6, T7) (R, error), GIOA ~func() E.Either[error, R], T0, T1, T2, T3, T4, T5, T6, T7, R any](f func(T0, T1, T2, T3, T4, T5, T6, T7) GRA) F {
|
||||
return func(c context.Context, t0 T0, t1 T1, t2 T2, t3 T3, t4 T4, t5 T5, t6 T6, t7 T7) (R, error) {
|
||||
return E.UnwrapError(f(t0, t1, t2, t3, t4, t5, t6, t7)(c)())
|
||||
}
|
||||
}
|
||||
|
||||
// SequenceT8 converts 8 readers into a reader of a [T.Tuple8].
|
||||
func SequenceT8[
|
||||
GR_TUPLE8 ~func(context.Context) GIO_TUPLE8,
|
||||
@@ -2605,6 +2677,14 @@ func Eitherize9[GRA ~func(context.Context) GIOA, F ~func(context.Context, T0, T1
|
||||
return RE.Eitherize9[GRA](f)
|
||||
}
|
||||
|
||||
// Uneitherize9 converts a function with 9 parameters returning a [GRA] into a function with 9 parameters returning a tuple.
|
||||
// The first parameter is considered to be the [context.Context].
|
||||
func Uneitherize9[GRA ~func(context.Context) GIOA, F ~func(context.Context, T0, T1, T2, T3, T4, T5, T6, T7, T8) (R, error), GIOA ~func() E.Either[error, R], T0, T1, T2, T3, T4, T5, T6, T7, T8, R any](f func(T0, T1, T2, T3, T4, T5, T6, T7, T8) GRA) F {
|
||||
return func(c context.Context, t0 T0, t1 T1, t2 T2, t3 T3, t4 T4, t5 T5, t6 T6, t7 T7, t8 T8) (R, error) {
|
||||
return E.UnwrapError(f(t0, t1, t2, t3, t4, t5, t6, t7, t8)(c)())
|
||||
}
|
||||
}
|
||||
|
||||
// SequenceT9 converts 9 readers into a reader of a [T.Tuple9].
|
||||
func SequenceT9[
|
||||
GR_TUPLE9 ~func(context.Context) GIO_TUPLE9,
|
||||
@@ -3157,6 +3237,14 @@ func Eitherize10[GRA ~func(context.Context) GIOA, F ~func(context.Context, T0, T
|
||||
return RE.Eitherize10[GRA](f)
|
||||
}
|
||||
|
||||
// Uneitherize10 converts a function with 10 parameters returning a [GRA] into a function with 10 parameters returning a tuple.
|
||||
// The first parameter is considered to be the [context.Context].
|
||||
func Uneitherize10[GRA ~func(context.Context) GIOA, F ~func(context.Context, T0, T1, T2, T3, T4, T5, T6, T7, T8, T9) (R, error), GIOA ~func() E.Either[error, R], T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, R any](f func(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9) GRA) F {
|
||||
return func(c context.Context, t0 T0, t1 T1, t2 T2, t3 T3, t4 T4, t5 T5, t6 T6, t7 T7, t8 T8, t9 T9) (R, error) {
|
||||
return E.UnwrapError(f(t0, t1, t2, t3, t4, t5, t6, t7, t8, t9)(c)())
|
||||
}
|
||||
}
|
||||
|
||||
// SequenceT10 converts 10 readers into a reader of a [T.Tuple10].
|
||||
func SequenceT10[
|
||||
GR_TUPLE10 ~func(context.Context) GIO_TUPLE10,
|
||||
|
@@ -1,2 +1,3 @@
|
||||
@echo off
|
||||
go tool cover -html=build/cover.out -o build/cover.html
|
||||
go tool cover -html=build/cover.out -o build/cover.html
|
||||
cov-report -ex ".*/cli/.*.go|.*/gen.go|.*/binds.go" build\cover.out
|
||||
|
@@ -1,8 +1,10 @@
|
||||
package either
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
TST "github.com/IBM/fp-go/internal/testing"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
@@ -16,3 +18,33 @@ func TestCompactArray(t *testing.T) {
|
||||
res := CompactArray(ar)
|
||||
assert.Equal(t, 2, len(res))
|
||||
}
|
||||
|
||||
func TestSequenceArray(t *testing.T) {
|
||||
|
||||
s := TST.SequenceArrayTest(
|
||||
FromStrictEquals[error, bool](),
|
||||
Pointed[error, string](),
|
||||
Pointed[error, bool](),
|
||||
Functor[error, []string, bool](),
|
||||
SequenceArray[error, string],
|
||||
)
|
||||
|
||||
for i := 0; i < 10; i++ {
|
||||
t.Run(fmt.Sprintf("TestSequenceArray %d", i), s(i))
|
||||
}
|
||||
}
|
||||
|
||||
func TestSequenceArrayError(t *testing.T) {
|
||||
|
||||
s := TST.SequenceArrayErrorTest(
|
||||
FromStrictEquals[error, bool](),
|
||||
Left[string, error],
|
||||
Left[bool, error],
|
||||
Pointed[error, string](),
|
||||
Pointed[error, bool](),
|
||||
Functor[error, []string, bool](),
|
||||
SequenceArray[error, string],
|
||||
)
|
||||
// run across four bits
|
||||
s(4)(t)
|
||||
}
|
||||
|
31
either/functor.go
Normal file
31
either/functor.go
Normal file
@@ -0,0 +1,31 @@
|
||||
// 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 either
|
||||
|
||||
import (
|
||||
"github.com/IBM/fp-go/internal/functor"
|
||||
)
|
||||
|
||||
type eitherFunctor[E, A, B any] struct{}
|
||||
|
||||
func (o *eitherFunctor[E, A, B]) Map(f func(A) B) func(Either[E, A]) Either[E, B] {
|
||||
return Map[E, A, B](f)
|
||||
}
|
||||
|
||||
// Functor implements the functoric operations for [Either]
|
||||
func Functor[E, A, B any]() functor.Functor[A, B, Either[E, A], Either[E, B]] {
|
||||
return &eitherFunctor[E, A, B]{}
|
||||
}
|
31
either/pointed.go
Normal file
31
either/pointed.go
Normal file
@@ -0,0 +1,31 @@
|
||||
// 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 either
|
||||
|
||||
import (
|
||||
"github.com/IBM/fp-go/internal/pointed"
|
||||
)
|
||||
|
||||
type eitherPointed[E, A any] struct{}
|
||||
|
||||
func (o *eitherPointed[E, A]) Of(a A) Either[E, A] {
|
||||
return Of[E, A](a)
|
||||
}
|
||||
|
||||
// Pointed implements the pointedic operations for [Either]
|
||||
func Pointed[E, A any]() pointed.Pointed[A, Either[E, A]] {
|
||||
return &eitherPointed[E, A]{}
|
||||
}
|
8
go.mod
8
go.mod
@@ -3,15 +3,15 @@ module github.com/IBM/fp-go
|
||||
go 1.20
|
||||
|
||||
require (
|
||||
github.com/stretchr/testify v1.9.0
|
||||
github.com/urfave/cli/v2 v2.27.2
|
||||
github.com/stretchr/testify v1.10.0
|
||||
github.com/urfave/cli/v2 v2.27.7
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.7 // indirect
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/russross/blackfriday/v2 v2.1.0 // indirect
|
||||
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 // indirect
|
||||
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
22
go.sum
22
go.sum
@@ -1,5 +1,9 @@
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lVfRxZq4=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.5 h1:ZtcqGrnekaHpVLArFSe4HK5DoKx1T0rq2DwVB0alcyc=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.5/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.7 h1:zbFlGlXEAKlwXpmvle3d8Oe3YnkKIK4xSRTd3sHPnBo=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.7/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
@@ -8,12 +12,18 @@ github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf
|
||||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/urfave/cli/v2 v2.27.1 h1:8xSQ6szndafKVRmfyeUMxkNUJQMjL1F2zmsZ+qHpfho=
|
||||
github.com/urfave/cli/v2 v2.27.1/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
|
||||
github.com/urfave/cli/v2 v2.27.2 h1:6e0H+AkS+zDckwPCUrZkKX38mRaau4nL2uipkJpbkcI=
|
||||
github.com/urfave/cli/v2 v2.27.2/go.mod h1:g0+79LmHHATl7DAcHO99smiR/T7uGLw84w8Y42x+4eM=
|
||||
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 h1:+qGGcbkzsfDQNPPe9UDgpxAWQrhbbBXOYJFQDq/dtJw=
|
||||
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913/go.mod h1:4aEEwZQutDLsQv2Deui4iYQ6DWTxR14g6m8Wv88+Xqk=
|
||||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/urfave/cli/v2 v2.27.4 h1:o1owoI+02Eb+K107p27wEX9Bb8eqIoZCfLXloLUSWJ8=
|
||||
github.com/urfave/cli/v2 v2.27.4/go.mod h1:m4QzxcD2qpra4z7WhzEGn74WZLViBnMpb1ToCAKdGRQ=
|
||||
github.com/urfave/cli/v2 v2.27.5 h1:WoHEJLdsXr6dDWoJgMq/CboDmyY/8HMMH1fTECbih+w=
|
||||
github.com/urfave/cli/v2 v2.27.5/go.mod h1:3Sevf16NykTbInEnD0yKkjDAeZDS0A6bzhBH5hrMvTQ=
|
||||
github.com/urfave/cli/v2 v2.27.6 h1:VdRdS98FNhKZ8/Az8B7MTyGQmpIr36O1EHybx/LaZ4g=
|
||||
github.com/urfave/cli/v2 v2.27.6/go.mod h1:3Sevf16NykTbInEnD0yKkjDAeZDS0A6bzhBH5hrMvTQ=
|
||||
github.com/urfave/cli/v2 v2.27.7 h1:bH59vdhbjLv3LAvIu6gd0usJHgoTTPhCFib8qqOwXYU=
|
||||
github.com/urfave/cli/v2 v2.27.7/go.mod h1:CyNAG/xg+iAOg0N4MPGZqVmv2rCoP267496AOXUZjA4=
|
||||
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 h1:gEOO8jv9F4OT7lGCjxCBTO/36wtF6j2nSip77qHd4x4=
|
||||
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1/go.mod h1:Ohn+xnUBiLI6FVj/9LpzZWtj1/D6lUovWYBkxHVV3aM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
|
183
internal/testing/sequence.go
Normal file
183
internal/testing/sequence.go
Normal file
@@ -0,0 +1,183 @@
|
||||
// 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 testing
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
EQ "github.com/IBM/fp-go/eq"
|
||||
F "github.com/IBM/fp-go/function"
|
||||
"github.com/IBM/fp-go/internal/functor"
|
||||
"github.com/IBM/fp-go/internal/pointed"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// SequenceArrayTest tests if the sequence operation works in case the operation cannot error
|
||||
func SequenceArrayTest[
|
||||
HKTA,
|
||||
HKTB,
|
||||
HKTAA any, // HKT[[]A]
|
||||
](
|
||||
eq EQ.Eq[HKTB],
|
||||
|
||||
pa pointed.Pointed[string, HKTA],
|
||||
pb pointed.Pointed[bool, HKTB],
|
||||
faa functor.Functor[[]string, bool, HKTAA, HKTB],
|
||||
seq func([]HKTA) HKTAA,
|
||||
) func(count int) func(t *testing.T) {
|
||||
|
||||
return func(count int) func(t *testing.T) {
|
||||
|
||||
exp := make([]string, count)
|
||||
good := make([]HKTA, count)
|
||||
for i := 0; i < count; i++ {
|
||||
val := fmt.Sprintf("TestData %d", i)
|
||||
exp[i] = val
|
||||
good[i] = pa.Of(val)
|
||||
}
|
||||
|
||||
return func(t *testing.T) {
|
||||
res := F.Pipe2(
|
||||
good,
|
||||
seq,
|
||||
faa.Map(func(act []string) bool {
|
||||
return assert.Equal(t, exp, act)
|
||||
}),
|
||||
)
|
||||
assert.True(t, eq.Equals(res, pb.Of(true)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// SequenceArrayErrorTest tests if the sequence operation works in case the operation can error
|
||||
func SequenceArrayErrorTest[
|
||||
HKTA,
|
||||
HKTB,
|
||||
HKTAA any, // HKT[[]A]
|
||||
](
|
||||
eq EQ.Eq[HKTB],
|
||||
|
||||
left func(error) HKTA,
|
||||
leftB func(error) HKTB,
|
||||
pa pointed.Pointed[string, HKTA],
|
||||
pb pointed.Pointed[bool, HKTB],
|
||||
faa functor.Functor[[]string, bool, HKTAA, HKTB],
|
||||
seq func([]HKTA) HKTAA,
|
||||
) func(count int) func(t *testing.T) {
|
||||
|
||||
return func(count int) func(t *testing.T) {
|
||||
|
||||
expGood := make([]string, count)
|
||||
good := make([]HKTA, count)
|
||||
expBad := make([]error, count)
|
||||
bad := make([]HKTA, count)
|
||||
|
||||
for i := 0; i < count; i++ {
|
||||
goodVal := fmt.Sprintf("TestData %d", i)
|
||||
badVal := fmt.Errorf("ErrorData %d", i)
|
||||
expGood[i] = goodVal
|
||||
good[i] = pa.Of(goodVal)
|
||||
expBad[i] = badVal
|
||||
bad[i] = left(badVal)
|
||||
}
|
||||
|
||||
total := 1 << count
|
||||
|
||||
return func(t *testing.T) {
|
||||
// test the good case
|
||||
res := F.Pipe2(
|
||||
good,
|
||||
seq,
|
||||
faa.Map(func(act []string) bool {
|
||||
return assert.Equal(t, expGood, act)
|
||||
}),
|
||||
)
|
||||
assert.True(t, eq.Equals(res, pb.Of(true)))
|
||||
// iterate and test the bad cases
|
||||
for i := 1; i < total; i++ {
|
||||
// run the test
|
||||
t.Run(fmt.Sprintf("Bitmask test %d", i), func(t1 *testing.T) {
|
||||
// the actual
|
||||
act := make([]HKTA, count)
|
||||
// the expected error
|
||||
var exp error
|
||||
// prepare the values bases on the bit mask
|
||||
mask := 1
|
||||
for j := 0; j < count; j++ {
|
||||
if (i & mask) == 0 {
|
||||
act[j] = good[j]
|
||||
} else {
|
||||
act[j] = bad[j]
|
||||
if exp == nil {
|
||||
exp = expBad[j]
|
||||
}
|
||||
}
|
||||
mask <<= 1
|
||||
}
|
||||
// test the good case
|
||||
res := F.Pipe2(
|
||||
act,
|
||||
seq,
|
||||
faa.Map(func(act []string) bool {
|
||||
return assert.Equal(t, expGood, act)
|
||||
}),
|
||||
)
|
||||
// validate the error
|
||||
assert.True(t, eq.Equals(res, leftB(exp)))
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// SequenceRecordTest tests if the sequence operation works in case the operation cannot error
|
||||
func SequenceRecordTest[
|
||||
HKTA,
|
||||
HKTB,
|
||||
HKTAA any, // HKT[map[string]string]
|
||||
](
|
||||
eq EQ.Eq[HKTB],
|
||||
|
||||
pa pointed.Pointed[string, HKTA],
|
||||
pb pointed.Pointed[bool, HKTB],
|
||||
faa functor.Functor[map[string]string, bool, HKTAA, HKTB],
|
||||
seq func(map[string]HKTA) HKTAA,
|
||||
) func(count int) func(t *testing.T) {
|
||||
|
||||
return func(count int) func(t *testing.T) {
|
||||
|
||||
exp := make(map[string]string)
|
||||
good := make(map[string]HKTA)
|
||||
for i := 0; i < count; i++ {
|
||||
key := fmt.Sprintf("KeyData %d", i)
|
||||
val := fmt.Sprintf("ValueData %d", i)
|
||||
exp[key] = val
|
||||
good[key] = pa.Of(val)
|
||||
}
|
||||
|
||||
return func(t *testing.T) {
|
||||
res := F.Pipe2(
|
||||
good,
|
||||
seq,
|
||||
faa.Map(func(act map[string]string) bool {
|
||||
return assert.Equal(t, exp, act)
|
||||
}),
|
||||
)
|
||||
assert.True(t, eq.Equals(res, pb.Of(true)))
|
||||
}
|
||||
}
|
||||
}
|
5
io/eq.go
5
io/eq.go
@@ -24,3 +24,8 @@ import (
|
||||
func Eq[A any](e EQ.Eq[A]) EQ.Eq[IO[A]] {
|
||||
return G.Eq[IO[A]](e)
|
||||
}
|
||||
|
||||
// FromStrictEquals constructs an [EQ.Eq] from the canonical comparison function
|
||||
func FromStrictEquals[A comparable]() EQ.Eq[IO[A]] {
|
||||
return G.FromStrictEquals[IO[A]]()
|
||||
}
|
||||
|
26
io/functor.go
Normal file
26
io/functor.go
Normal file
@@ -0,0 +1,26 @@
|
||||
// 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 io
|
||||
|
||||
import (
|
||||
"github.com/IBM/fp-go/internal/functor"
|
||||
G "github.com/IBM/fp-go/io/generic"
|
||||
)
|
||||
|
||||
// Functor returns the monadic operations for [IO]
|
||||
func Functor[A, B any]() functor.Functor[A, B, IO[A], IO[B]] {
|
||||
return G.Functor[A, B, IO[A], IO[B]]()
|
||||
}
|
@@ -33,3 +33,8 @@ func Eq[GA ~func() A, A any](e EQ.Eq[A]) EQ.Eq[GA] {
|
||||
return eq(l, r)()
|
||||
})
|
||||
}
|
||||
|
||||
// FromStrictEquals constructs an [EQ.Eq] from the canonical comparison function
|
||||
func FromStrictEquals[GA ~func() A, A comparable]() EQ.Eq[GA] {
|
||||
return Eq[GA](EQ.FromStrictEquals[A]())
|
||||
}
|
||||
|
31
io/generic/functor.go
Normal file
31
io/generic/functor.go
Normal file
@@ -0,0 +1,31 @@
|
||||
// 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 (
|
||||
"github.com/IBM/fp-go/internal/functor"
|
||||
)
|
||||
|
||||
type ioFunctor[A, B any, GA ~func() A, GB ~func() B] struct{}
|
||||
|
||||
func (o *ioFunctor[A, B, GA, GB]) Map(f func(A) B) func(GA) GB {
|
||||
return Map[GA, GB, A, B](f)
|
||||
}
|
||||
|
||||
// Functor implements the functoric operations for [IO]
|
||||
func Functor[A, B any, GA ~func() A, GB ~func() B]() functor.Functor[A, B, GA, GB] {
|
||||
return &ioFunctor[A, B, GA, GB]{}
|
||||
}
|
31
io/generic/pointed.go
Normal file
31
io/generic/pointed.go
Normal file
@@ -0,0 +1,31 @@
|
||||
// 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 (
|
||||
"github.com/IBM/fp-go/internal/pointed"
|
||||
)
|
||||
|
||||
type ioPointed[A any, GA ~func() A] struct{}
|
||||
|
||||
func (o *ioPointed[A, GA]) Of(a A) GA {
|
||||
return Of[GA, A](a)
|
||||
}
|
||||
|
||||
// Pointed implements the pointedic operations for [IO]
|
||||
func Pointed[A any, GA ~func() A]() pointed.Pointed[A, GA] {
|
||||
return &ioPointed[A, GA]{}
|
||||
}
|
26
io/pointed.go
Normal file
26
io/pointed.go
Normal file
@@ -0,0 +1,26 @@
|
||||
// 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 io
|
||||
|
||||
import (
|
||||
"github.com/IBM/fp-go/internal/pointed"
|
||||
G "github.com/IBM/fp-go/io/generic"
|
||||
)
|
||||
|
||||
// Pointed returns the monadic operations for [IO]
|
||||
func Pointed[A any]() pointed.Pointed[A, IO[A]] {
|
||||
return G.Pointed[A, IO[A]]()
|
||||
}
|
@@ -16,8 +16,11 @@
|
||||
package io
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
A "github.com/IBM/fp-go/array"
|
||||
F "github.com/IBM/fp-go/function"
|
||||
TST "github.com/IBM/fp-go/internal/testing"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"testing"
|
||||
@@ -45,3 +48,18 @@ func TestMapSeq(t *testing.T) {
|
||||
|
||||
assert.True(t, res())
|
||||
}
|
||||
|
||||
func TestSequenceArray(t *testing.T) {
|
||||
|
||||
s := TST.SequenceArrayTest(
|
||||
FromStrictEquals[bool](),
|
||||
Pointed[string](),
|
||||
Pointed[bool](),
|
||||
Functor[[]string, bool](),
|
||||
SequenceArray[string],
|
||||
)
|
||||
|
||||
for i := 0; i < 10; i++ {
|
||||
t.Run(fmt.Sprintf("TestSequenceArray %d", i), s(i))
|
||||
}
|
||||
}
|
||||
|
@@ -17,6 +17,7 @@ package generic
|
||||
|
||||
import (
|
||||
ET "github.com/IBM/fp-go/either"
|
||||
"github.com/IBM/fp-go/internal/functor"
|
||||
"github.com/IBM/fp-go/internal/monad"
|
||||
"github.com/IBM/fp-go/internal/pointed"
|
||||
)
|
||||
@@ -25,6 +26,8 @@ type ioEitherPointed[E, A any, GA ~func() ET.Either[E, A]] struct{}
|
||||
|
||||
type ioEitherMonad[E, A, B any, GA ~func() ET.Either[E, A], GB ~func() ET.Either[E, B], GAB ~func() ET.Either[E, func(A) B]] struct{}
|
||||
|
||||
type ioEitherFunctor[E, A, B any, GA ~func() ET.Either[E, A], GB ~func() ET.Either[E, B]] struct{}
|
||||
|
||||
func (o *ioEitherPointed[E, A, GA]) Of(a A) GA {
|
||||
return Of[GA, E, A](a)
|
||||
}
|
||||
@@ -45,11 +48,20 @@ func (o *ioEitherMonad[E, A, B, GA, GB, GAB]) Ap(fa GA) func(GAB) GB {
|
||||
return Ap[GB, GAB, GA, E, A, B](fa)
|
||||
}
|
||||
|
||||
func (o *ioEitherFunctor[E, A, B, GA, GB]) Map(f func(A) B) func(GA) GB {
|
||||
return Map[GA, GB, E, A, B](f)
|
||||
}
|
||||
|
||||
// Pointed implements the pointed operations for [IOEither]
|
||||
func Pointed[E, A any, GA ~func() ET.Either[E, A]]() pointed.Pointed[A, GA] {
|
||||
return &ioEitherPointed[E, A, GA]{}
|
||||
}
|
||||
|
||||
// Functor implements the monadic operations for [IOEither]
|
||||
func Functor[E, A, B any, GA ~func() ET.Either[E, A], GB ~func() ET.Either[E, B]]() functor.Functor[A, B, GA, GB] {
|
||||
return &ioEitherFunctor[E, A, B, GA, GB]{}
|
||||
}
|
||||
|
||||
// Monad implements the monadic operations for [IOEither]
|
||||
func Monad[E, A, B any, GA ~func() ET.Either[E, A], GB ~func() ET.Either[E, B], GAB ~func() ET.Either[E, func(A) B]]() monad.Monad[A, B, GA, GB, GAB] {
|
||||
return &ioEitherMonad[E, A, B, GA, GB, GAB]{}
|
||||
|
@@ -16,6 +16,7 @@
|
||||
package ioeither
|
||||
|
||||
import (
|
||||
"github.com/IBM/fp-go/internal/functor"
|
||||
"github.com/IBM/fp-go/internal/monad"
|
||||
"github.com/IBM/fp-go/internal/pointed"
|
||||
G "github.com/IBM/fp-go/ioeither/generic"
|
||||
@@ -26,6 +27,11 @@ func Pointed[E, A any]() pointed.Pointed[A, IOEither[E, A]] {
|
||||
return G.Pointed[E, A, IOEither[E, A]]()
|
||||
}
|
||||
|
||||
// Functor returns the functor operations for [IOEither]
|
||||
func Functor[E, A, B any]() functor.Functor[A, B, IOEither[E, A], IOEither[E, B]] {
|
||||
return G.Functor[E, A, B, IOEither[E, A], IOEither[E, B]]()
|
||||
}
|
||||
|
||||
// Monad returns the monadic operations for [IOEither]
|
||||
func Monad[E, A, B any]() monad.Monad[A, B, IOEither[E, A], IOEither[E, B], IOEither[E, func(A) B]] {
|
||||
return G.Monad[E, A, B, IOEither[E, A], IOEither[E, B], IOEither[E, func(A) B]]()
|
||||
|
@@ -16,11 +16,15 @@
|
||||
package ioeither
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
A "github.com/IBM/fp-go/array"
|
||||
E "github.com/IBM/fp-go/either"
|
||||
F "github.com/IBM/fp-go/function"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
TST "github.com/IBM/fp-go/internal/testing"
|
||||
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -46,3 +50,33 @@ func TestMapSeq(t *testing.T) {
|
||||
|
||||
assert.Equal(t, E.Of[error](true), res())
|
||||
}
|
||||
|
||||
func TestSequenceArray(t *testing.T) {
|
||||
|
||||
s := TST.SequenceArrayTest(
|
||||
FromStrictEquals[error, bool](),
|
||||
Pointed[error, string](),
|
||||
Pointed[error, bool](),
|
||||
Functor[error, []string, bool](),
|
||||
SequenceArray[error, string],
|
||||
)
|
||||
|
||||
for i := 0; i < 10; i++ {
|
||||
t.Run(fmt.Sprintf("TestSequenceArray %d", i), s(i))
|
||||
}
|
||||
}
|
||||
|
||||
func TestSequenceArrayError(t *testing.T) {
|
||||
|
||||
s := TST.SequenceArrayErrorTest(
|
||||
FromStrictEquals[error, bool](),
|
||||
Left[string, error],
|
||||
Left[bool, error],
|
||||
Pointed[error, string](),
|
||||
Pointed[error, bool](),
|
||||
Functor[error, []string, bool](),
|
||||
SequenceArray[error, string],
|
||||
)
|
||||
// run across four bits
|
||||
s(4)(t)
|
||||
}
|
||||
|
@@ -19,8 +19,7 @@ import (
|
||||
G "github.com/IBM/fp-go/iterator/stateless/generic"
|
||||
)
|
||||
|
||||
// DropWhile creates an [Iterator] that drops elements from the [Iterator] as long as the predicate is true; afterwards, returns every element.
|
||||
// Note, the [Iterator] does not produce any output until the predicate first becomes false
|
||||
// Cycle creates an [Iterator] containing an [Iterator] repeated an infinite number of times.
|
||||
func Cycle[U any](ma Iterator[U]) Iterator[U] {
|
||||
return G.Cycle[Iterator[U]](ma)
|
||||
}
|
||||
|
@@ -16,25 +16,13 @@
|
||||
package option
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
F "github.com/IBM/fp-go/function"
|
||||
TST "github.com/IBM/fp-go/internal/testing"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestSequenceArray(t *testing.T) {
|
||||
|
||||
one := Of(1)
|
||||
two := Of(2)
|
||||
|
||||
res := F.Pipe1(
|
||||
[]Option[int]{one, two},
|
||||
SequenceArray[int],
|
||||
)
|
||||
|
||||
assert.Equal(t, res, Of([]int{1, 2}))
|
||||
}
|
||||
|
||||
func TestCompactArray(t *testing.T) {
|
||||
ar := []Option[string]{
|
||||
Of("ok"),
|
||||
@@ -45,3 +33,18 @@ func TestCompactArray(t *testing.T) {
|
||||
res := CompactArray(ar)
|
||||
assert.Equal(t, 2, len(res))
|
||||
}
|
||||
|
||||
func TestSequenceArray(t *testing.T) {
|
||||
|
||||
s := TST.SequenceArrayTest(
|
||||
FromStrictEquals[bool](),
|
||||
Pointed[string](),
|
||||
Pointed[bool](),
|
||||
Functor[[]string, bool](),
|
||||
SequenceArray[string],
|
||||
)
|
||||
|
||||
for i := 0; i < 10; i++ {
|
||||
t.Run(fmt.Sprintf("TestSequenceArray %d", i), s(i))
|
||||
}
|
||||
}
|
||||
|
31
option/functor.go
Normal file
31
option/functor.go
Normal file
@@ -0,0 +1,31 @@
|
||||
// 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 option
|
||||
|
||||
import (
|
||||
"github.com/IBM/fp-go/internal/functor"
|
||||
)
|
||||
|
||||
type optionFunctor[A, B any] struct{}
|
||||
|
||||
func (o *optionFunctor[A, B]) Map(f func(A) B) func(Option[A]) Option[B] {
|
||||
return Map[A, B](f)
|
||||
}
|
||||
|
||||
// Functor implements the functoric operations for [Option]
|
||||
func Functor[A, B any]() functor.Functor[A, B, Option[A], Option[B]] {
|
||||
return &optionFunctor[A, B]{}
|
||||
}
|
31
option/pointed.go
Normal file
31
option/pointed.go
Normal file
@@ -0,0 +1,31 @@
|
||||
// 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 option
|
||||
|
||||
import (
|
||||
"github.com/IBM/fp-go/internal/pointed"
|
||||
)
|
||||
|
||||
type optionPointed[A any] struct{}
|
||||
|
||||
func (o *optionPointed[A]) Of(a A) Option[A] {
|
||||
return Of[A](a)
|
||||
}
|
||||
|
||||
// Pointed implements the Pointed operations for [Option]
|
||||
func Pointed[A any]() pointed.Pointed[A, Option[A]] {
|
||||
return &optionPointed[A]{}
|
||||
}
|
@@ -16,21 +16,13 @@
|
||||
package option
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
TST "github.com/IBM/fp-go/internal/testing"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestSequenceRecord(t *testing.T) {
|
||||
assert.Equal(t, Of(map[string]string{
|
||||
"a": "A",
|
||||
"b": "B",
|
||||
}), SequenceRecord(map[string]Option[string]{
|
||||
"a": Of("A"),
|
||||
"b": Of("B"),
|
||||
}))
|
||||
}
|
||||
|
||||
func TestCompactRecord(t *testing.T) {
|
||||
// make the map
|
||||
m := make(map[string]Option[int])
|
||||
@@ -45,3 +37,18 @@ func TestCompactRecord(t *testing.T) {
|
||||
|
||||
assert.Equal(t, exp, m1)
|
||||
}
|
||||
|
||||
func TestSequenceRecord(t *testing.T) {
|
||||
|
||||
s := TST.SequenceRecordTest(
|
||||
FromStrictEquals[bool](),
|
||||
Pointed[string](),
|
||||
Pointed[bool](),
|
||||
Functor[map[string]string, bool](),
|
||||
SequenceRecord[string, string],
|
||||
)
|
||||
|
||||
for i := 0; i < 10; i++ {
|
||||
t.Run(fmt.Sprintf("TestSequenceRecord %d", i), s(i))
|
||||
}
|
||||
}
|
||||
|
@@ -64,7 +64,7 @@ func Reverse[T any](o Ord[T]) Ord[T] {
|
||||
}, o.Equals)
|
||||
}
|
||||
|
||||
// Contramap creates an odering under a transformation function
|
||||
// Contramap creates an ordering under a transformation function
|
||||
func Contramap[A, B any](f func(B) A) func(Ord[A]) Ord[B] {
|
||||
return func(o Ord[A]) Ord[B] {
|
||||
return MakeOrd(func(x, y B) int {
|
||||
|
@@ -17,11 +17,13 @@ package readerioeither
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
A "github.com/IBM/fp-go/array"
|
||||
ET "github.com/IBM/fp-go/either"
|
||||
F "github.com/IBM/fp-go/function"
|
||||
TST "github.com/IBM/fp-go/internal/testing"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
@@ -37,3 +39,33 @@ func TestTraverseArray(t *testing.T) {
|
||||
assert.Equal(t, ET.Right[string]([]string{"aa", "bb"}), F.Pipe1([]string{"a", "b"}, f)(ctx)())
|
||||
assert.Equal(t, ET.Left[[]string]("e"), F.Pipe1([]string{"a", ""}, f)(ctx)())
|
||||
}
|
||||
|
||||
func TestSequenceArray(t *testing.T) {
|
||||
|
||||
s := TST.SequenceArrayTest(
|
||||
FromStrictEquals[context.Context, error, bool]()(context.Background()),
|
||||
Pointed[context.Context, error, string](),
|
||||
Pointed[context.Context, error, bool](),
|
||||
Functor[context.Context, error, []string, bool](),
|
||||
SequenceArray[context.Context, error, string],
|
||||
)
|
||||
|
||||
for i := 0; i < 10; i++ {
|
||||
t.Run(fmt.Sprintf("TestSequenceArray %d", i), s(i))
|
||||
}
|
||||
}
|
||||
|
||||
func TestSequenceArrayError(t *testing.T) {
|
||||
|
||||
s := TST.SequenceArrayErrorTest(
|
||||
FromStrictEquals[context.Context, error, bool]()(context.Background()),
|
||||
Left[context.Context, string, error],
|
||||
Left[context.Context, bool, error],
|
||||
Pointed[context.Context, error, string](),
|
||||
Pointed[context.Context, error, bool](),
|
||||
Functor[context.Context, error, []string, bool](),
|
||||
SequenceArray[context.Context, error, string],
|
||||
)
|
||||
// run across four bits
|
||||
s(4)(t)
|
||||
}
|
||||
|
68
readerioeither/generic/monad.go
Normal file
68
readerioeither/generic/monad.go
Normal file
@@ -0,0 +1,68 @@
|
||||
// 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"
|
||||
"github.com/IBM/fp-go/internal/functor"
|
||||
"github.com/IBM/fp-go/internal/monad"
|
||||
"github.com/IBM/fp-go/internal/pointed"
|
||||
)
|
||||
|
||||
type readerIOEitherPointed[R, E, A any, GRA ~func(R) GIOA, GIOA ~func() ET.Either[E, A]] struct{}
|
||||
|
||||
type readerIOEitherMonad[R, E, A, B any, GRA ~func(R) GIOA, GRB ~func(R) GIOB, GRAB ~func(R) GIOAB, GIOA ~func() ET.Either[E, A], GIOB ~func() ET.Either[E, B], GIOAB ~func() ET.Either[E, func(A) B]] struct{}
|
||||
|
||||
type readerIOEitherFunctor[R, E, A, B any, GRA ~func(R) GIOA, GRB ~func(R) GIOB, GIOA ~func() ET.Either[E, A], GIOB ~func() ET.Either[E, B]] struct{}
|
||||
|
||||
func (o *readerIOEitherPointed[R, E, A, GRA, GIOA]) Of(a A) GRA {
|
||||
return Of[GRA, GIOA, R, E, A](a)
|
||||
}
|
||||
|
||||
func (o *readerIOEitherMonad[R, E, A, B, GRA, GRB, GRAB, GIOA, GIOB, GIOAB]) Of(a A) GRA {
|
||||
return Of[GRA, GIOA, R, E, A](a)
|
||||
}
|
||||
|
||||
func (o *readerIOEitherMonad[R, E, A, B, GRA, GRB, GRAB, GIOA, GIOB, GIOAB]) Map(f func(A) B) func(GRA) GRB {
|
||||
return Map[GRA, GRB, GIOA, GIOB, R, E, A, B](f)
|
||||
}
|
||||
|
||||
func (o *readerIOEitherMonad[R, E, A, B, GRA, GRB, GRAB, GIOA, GIOB, GIOAB]) Chain(f func(A) GRB) func(GRA) GRB {
|
||||
return Chain[GRA, GRB, GIOA, GIOB, R, E, A, B](f)
|
||||
}
|
||||
|
||||
func (o *readerIOEitherMonad[R, E, A, B, GRA, GRB, GRAB, GIOA, GIOB, GIOAB]) Ap(fa GRA) func(GRAB) GRB {
|
||||
return Ap[GRA, GRB, GRAB, GIOA, GIOB, GIOAB, R, E, A, B](fa)
|
||||
}
|
||||
|
||||
func (o *readerIOEitherFunctor[R, E, A, B, GRA, GRB, GIOA, GIOB]) Map(f func(A) B) func(GRA) GRB {
|
||||
return Map[GRA, GRB, GIOA, GIOB, R, E, A, B](f)
|
||||
}
|
||||
|
||||
// Pointed implements the pointed operations for [ReaderIOEither]
|
||||
func Pointed[R, E, A any, GRA ~func(R) GIOA, GIOA ~func() ET.Either[E, A]]() pointed.Pointed[A, GRA] {
|
||||
return &readerIOEitherPointed[R, E, A, GRA, GIOA]{}
|
||||
}
|
||||
|
||||
// Functor implements the monadic operations for [ReaderIOEither]
|
||||
func Functor[R, E, A, B any, GRA ~func(R) GIOA, GRB ~func(R) GIOB, GIOA ~func() ET.Either[E, A], GIOB ~func() ET.Either[E, B]]() functor.Functor[A, B, GRA, GRB] {
|
||||
return &readerIOEitherFunctor[R, E, A, B, GRA, GRB, GIOA, GIOB]{}
|
||||
}
|
||||
|
||||
// Monad implements the monadic operations for [ReaderIOEither]
|
||||
func Monad[R, E, A, B any, GRA ~func(R) GIOA, GRB ~func(R) GIOB, GRAB ~func(R) GIOAB, GIOA ~func() ET.Either[E, A], GIOB ~func() ET.Either[E, B], GIOAB ~func() ET.Either[E, func(A) B]]() monad.Monad[A, B, GRA, GRB, GRAB] {
|
||||
return &readerIOEitherMonad[R, E, A, B, GRA, GRB, GRAB, GIOA, GIOB, GIOAB]{}
|
||||
}
|
38
readerioeither/monad.go
Normal file
38
readerioeither/monad.go
Normal file
@@ -0,0 +1,38 @@
|
||||
// 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 readerioeither
|
||||
|
||||
import (
|
||||
"github.com/IBM/fp-go/internal/functor"
|
||||
"github.com/IBM/fp-go/internal/monad"
|
||||
"github.com/IBM/fp-go/internal/pointed"
|
||||
G "github.com/IBM/fp-go/readerioeither/generic"
|
||||
)
|
||||
|
||||
// Pointed returns the pointed operations for [ReaderIOEither]
|
||||
func Pointed[R, E, A any]() pointed.Pointed[A, ReaderIOEither[R, E, A]] {
|
||||
return G.Pointed[R, E, A, ReaderIOEither[R, E, A]]()
|
||||
}
|
||||
|
||||
// Functor returns the functor operations for [ReaderIOEither]
|
||||
func Functor[R, E, A, B any]() functor.Functor[A, B, ReaderIOEither[R, E, A], ReaderIOEither[R, E, B]] {
|
||||
return G.Functor[R, E, A, B, ReaderIOEither[R, E, A], ReaderIOEither[R, E, B]]()
|
||||
}
|
||||
|
||||
// Monad returns the monadic operations for [ReaderIOEither]
|
||||
func Monad[R, E, A, B any]() monad.Monad[A, B, ReaderIOEither[R, E, A], ReaderIOEither[R, E, B], ReaderIOEither[R, E, func(A) B]] {
|
||||
return G.Monad[R, E, A, B, ReaderIOEither[R, E, A], ReaderIOEither[R, E, B], ReaderIOEither[R, E, func(A) B]]()
|
||||
}
|
@@ -23,3 +23,8 @@ import (
|
||||
func Eq[K comparable, V any](e E.Eq[V]) E.Eq[map[K]V] {
|
||||
return G.Eq[map[K]V, K, V](e)
|
||||
}
|
||||
|
||||
// FromStrictEquals constructs an [EQ.Eq] from the canonical comparison function
|
||||
func FromStrictEquals[K, V comparable]() E.Eq[map[K]V] {
|
||||
return G.FromStrictEquals[map[K]V]()
|
||||
}
|
||||
|
48
record/eq_test.go
Normal file
48
record/eq_test.go
Normal file
@@ -0,0 +1,48 @@
|
||||
// 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 record
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestFromStrictEquals(t *testing.T) {
|
||||
m1 := map[string]string{
|
||||
"a": "A",
|
||||
"b": "B",
|
||||
}
|
||||
m2 := map[string]string{
|
||||
"a": "A",
|
||||
"b": "C",
|
||||
}
|
||||
m3 := map[string]string{
|
||||
"a": "A",
|
||||
"b": "B",
|
||||
}
|
||||
m4 := map[string]string{
|
||||
"a": "A",
|
||||
"b": "B",
|
||||
"c": "C",
|
||||
}
|
||||
|
||||
e := FromStrictEquals[string, string]()
|
||||
assert.True(t, e.Equals(m1, m1))
|
||||
assert.True(t, e.Equals(m1, m3))
|
||||
assert.False(t, e.Equals(m1, m2))
|
||||
assert.False(t, e.Equals(m1, m4))
|
||||
}
|
@@ -37,3 +37,8 @@ func Eq[M ~map[K]V, K comparable, V any](e E.Eq[V]) E.Eq[M] {
|
||||
return equals(left, right, eq)
|
||||
})
|
||||
}
|
||||
|
||||
// FromStrictEquals constructs an [EQ.Eq] from the canonical comparison function
|
||||
func FromStrictEquals[M ~map[K]V, K, V comparable]() E.Eq[M] {
|
||||
return Eq[M](E.FromStrictEquals[V]())
|
||||
}
|
||||
|
@@ -54,3 +54,69 @@ func TestUnionMonoid(t *testing.T) {
|
||||
|
||||
assert.Equal(t, res, m.Concat(x, y))
|
||||
}
|
||||
|
||||
func TestUnionFirstMonoid(t *testing.T) {
|
||||
m := UnionFirstMonoid[string, string]()
|
||||
|
||||
e := Empty[string, string]()
|
||||
|
||||
x := map[string]string{
|
||||
"a": "a1",
|
||||
"b": "b1",
|
||||
"c": "c1",
|
||||
}
|
||||
|
||||
y := map[string]string{
|
||||
"b": "b2",
|
||||
"c": "c2",
|
||||
"d": "d2",
|
||||
}
|
||||
|
||||
res := map[string]string{
|
||||
"a": "a1",
|
||||
"b": "b1",
|
||||
"c": "c1",
|
||||
"d": "d2",
|
||||
}
|
||||
|
||||
assert.Equal(t, x, m.Concat(x, m.Empty()))
|
||||
assert.Equal(t, x, m.Concat(m.Empty(), x))
|
||||
|
||||
assert.Equal(t, x, m.Concat(x, e))
|
||||
assert.Equal(t, x, m.Concat(e, x))
|
||||
|
||||
assert.Equal(t, res, m.Concat(x, y))
|
||||
}
|
||||
|
||||
func TestUnionLastMonoid(t *testing.T) {
|
||||
m := UnionLastMonoid[string, string]()
|
||||
|
||||
e := Empty[string, string]()
|
||||
|
||||
x := map[string]string{
|
||||
"a": "a1",
|
||||
"b": "b1",
|
||||
"c": "c1",
|
||||
}
|
||||
|
||||
y := map[string]string{
|
||||
"b": "b2",
|
||||
"c": "c2",
|
||||
"d": "d2",
|
||||
}
|
||||
|
||||
res := map[string]string{
|
||||
"a": "a1",
|
||||
"b": "b2",
|
||||
"c": "c2",
|
||||
"d": "d2",
|
||||
}
|
||||
|
||||
assert.Equal(t, x, m.Concat(x, m.Empty()))
|
||||
assert.Equal(t, x, m.Concat(m.Empty(), x))
|
||||
|
||||
assert.Equal(t, x, m.Concat(x, e))
|
||||
assert.Equal(t, x, m.Concat(e, x))
|
||||
|
||||
assert.Equal(t, res, m.Concat(x, y))
|
||||
}
|
||||
|
@@ -176,3 +176,25 @@ func TestFromArrayMap(t *testing.T) {
|
||||
"C": "C",
|
||||
}, res2)
|
||||
}
|
||||
|
||||
func TestEmpty(t *testing.T) {
|
||||
nonEmpty := map[string]string{
|
||||
"a": "A",
|
||||
"b": "B",
|
||||
}
|
||||
empty := Empty[string, string]()
|
||||
|
||||
assert.True(t, IsEmpty(empty))
|
||||
assert.False(t, IsEmpty(nonEmpty))
|
||||
assert.False(t, IsNonEmpty(empty))
|
||||
assert.True(t, IsNonEmpty(nonEmpty))
|
||||
}
|
||||
|
||||
func TestHas(t *testing.T) {
|
||||
nonEmpty := map[string]string{
|
||||
"a": "A",
|
||||
"b": "B",
|
||||
}
|
||||
assert.True(t, Has("a", nonEmpty))
|
||||
assert.False(t, Has("c", nonEmpty))
|
||||
}
|
||||
|
Reference in New Issue
Block a user