mirror of
https://github.com/IBM/fp-go.git
synced 2025-08-30 19:52:00 +02:00
Compare commits
15 Commits
cleue-add-
...
cleue-add-
Author | SHA1 | Date | |
---|---|---|---|
|
943ae8e009 | ||
|
44c8441b07 | ||
|
600aeae770 | ||
|
f74a407294 | ||
|
b15ab38861 | ||
|
6532a83e82 | ||
|
7c12b72db1 | ||
|
dc894ad643 | ||
|
c902058320 | ||
|
bf33f4fb66 | ||
|
b4d2a5c6be | ||
|
705b71d95c | ||
|
34844bcfc2 | ||
|
9a9d13b066 | ||
|
da1449e680 |
@@ -338,7 +338,7 @@ func generateUntupled(f *os.File, i int) {
|
||||
|
||||
func generateTupled(f *os.File, i int) {
|
||||
// Create the optionize version
|
||||
fmt.Fprintf(f, "\n// Tupled%d converts a function with %d parameters returning into a function taking a Tuple%d\n// The inverse function is [Untupled%d]\n", i, i, i, i)
|
||||
fmt.Fprintf(f, "\n// Tupled%d converts a function with %d parameters into a function taking a Tuple%d\n// The inverse function is [Untupled%d]\n", i, i, i, i)
|
||||
fmt.Fprintf(f, "func Tupled%d[F ~func(", i)
|
||||
for j := 0; j < i; j++ {
|
||||
if j > 0 {
|
||||
|
@@ -18,6 +18,7 @@ package readerio
|
||||
import (
|
||||
"context"
|
||||
|
||||
L "github.com/IBM/fp-go/lazy"
|
||||
R "github.com/IBM/fp-go/readerio/generic"
|
||||
)
|
||||
|
||||
@@ -54,6 +55,13 @@ func Ask() ReaderIO[context.Context] {
|
||||
}
|
||||
|
||||
// Defer creates an IO by creating a brand new IO via a generator function, each time
|
||||
func Defer[A any](gen func() ReaderIO[A]) ReaderIO[A] {
|
||||
func Defer[A any](gen L.Lazy[ReaderIO[A]]) ReaderIO[A] {
|
||||
return R.Defer[ReaderIO[A]](gen)
|
||||
}
|
||||
|
||||
// Memoize computes the value of the provided [ReaderIO] monad lazily but exactly once
|
||||
// The context used to compute the value is the context of the first call, so do not use this
|
||||
// method if the value has a functional dependency on the content of the context
|
||||
func Memoize[A any](rdr ReaderIO[A]) ReaderIO[A] {
|
||||
return R.Memoize[ReaderIO[A]](rdr)
|
||||
}
|
||||
|
@@ -25,25 +25,31 @@ import (
|
||||
F "github.com/IBM/fp-go/function"
|
||||
"github.com/IBM/fp-go/internal/file"
|
||||
IOE "github.com/IBM/fp-go/ioeither"
|
||||
IOEF "github.com/IBM/fp-go/ioeither/file"
|
||||
)
|
||||
|
||||
var (
|
||||
openIOE = IOE.Eitherize1(os.Open)
|
||||
// Open opens a file for reading within the given context
|
||||
Open = F.Flow3(
|
||||
openIOE,
|
||||
IOEF.Open,
|
||||
RIOE.FromIOEither[*os.File],
|
||||
RIOE.WithContext[*os.File],
|
||||
)
|
||||
|
||||
// Remove removes a file by name
|
||||
Remove = F.Flow2(
|
||||
IOEF.Remove,
|
||||
RIOE.FromIOEither[string],
|
||||
)
|
||||
)
|
||||
|
||||
// Close closes an object
|
||||
func Close[C io.Closer](c C) RIOE.ReaderIOEither[any] {
|
||||
return RIOE.FromIOEither(func() ET.Either[error, any] {
|
||||
return ET.TryCatchError(func() (any, error) {
|
||||
return c, c.Close()
|
||||
})
|
||||
})
|
||||
return F.Pipe2(
|
||||
c,
|
||||
IOEF.Close[C],
|
||||
RIOE.FromIOEither[any],
|
||||
)
|
||||
}
|
||||
|
||||
// ReadFile reads a file in the scope of a context
|
||||
|
53
context/readerioeither/file/tempfile.go
Normal file
53
context/readerioeither/file/tempfile.go
Normal file
@@ -0,0 +1,53 @@
|
||||
// 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 file
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
RIOE "github.com/IBM/fp-go/context/readerioeither"
|
||||
FL "github.com/IBM/fp-go/file"
|
||||
F "github.com/IBM/fp-go/function"
|
||||
IO "github.com/IBM/fp-go/io"
|
||||
IOF "github.com/IBM/fp-go/io/file"
|
||||
IOEF "github.com/IBM/fp-go/ioeither/file"
|
||||
)
|
||||
|
||||
var (
|
||||
// onCreateTempFile creates a temp file with sensible defaults
|
||||
onCreateTempFile = CreateTemp("", "*")
|
||||
// destroy handler
|
||||
onReleaseTempFile = F.Flow4(
|
||||
IOF.Close[*os.File],
|
||||
IO.Map(FL.GetName),
|
||||
RIOE.FromIO[string],
|
||||
RIOE.Chain(Remove),
|
||||
)
|
||||
)
|
||||
|
||||
// CreateTemp created a temp file with proper parametrization
|
||||
func CreateTemp(dir, pattern string) RIOE.ReaderIOEither[*os.File] {
|
||||
return F.Pipe2(
|
||||
IOEF.CreateTemp(dir, pattern),
|
||||
RIOE.FromIOEither[*os.File],
|
||||
RIOE.WithContext[*os.File],
|
||||
)
|
||||
}
|
||||
|
||||
// WithTempFile creates a temporary file, then invokes a callback to create a resource based on the file, then close and remove the temp file
|
||||
func WithTempFile[A any](f func(*os.File) RIOE.ReaderIOEither[A]) RIOE.ReaderIOEither[A] {
|
||||
return RIOE.WithResource[A](onCreateTempFile, onReleaseTempFile)(f)
|
||||
}
|
47
context/readerioeither/file/tempfile_test.go
Normal file
47
context/readerioeither/file/tempfile_test.go
Normal file
@@ -0,0 +1,47 @@
|
||||
// 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 file
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
RIOE "github.com/IBM/fp-go/context/readerioeither"
|
||||
E "github.com/IBM/fp-go/either"
|
||||
F "github.com/IBM/fp-go/function"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestWithTempFile(t *testing.T) {
|
||||
|
||||
res := WithTempFile(onWriteAll[*os.File]([]byte("Carsten")))
|
||||
|
||||
assert.Equal(t, E.Of[error]([]byte("Carsten")), res(context.Background())())
|
||||
}
|
||||
|
||||
func TestWithTempFileOnClosedFile(t *testing.T) {
|
||||
|
||||
res := WithTempFile(func(f *os.File) RIOE.ReaderIOEither[[]byte] {
|
||||
return F.Pipe2(
|
||||
f,
|
||||
onWriteAll[*os.File]([]byte("Carsten")),
|
||||
RIOE.ChainFirst(F.Constant1[[]byte](Close(f))),
|
||||
)
|
||||
})
|
||||
|
||||
assert.Equal(t, E.Of[error]([]byte("Carsten")), res(context.Background())())
|
||||
}
|
57
context/readerioeither/file/write.go
Normal file
57
context/readerioeither/file/write.go
Normal file
@@ -0,0 +1,57 @@
|
||||
// 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 file
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
|
||||
RIOE "github.com/IBM/fp-go/context/readerioeither"
|
||||
F "github.com/IBM/fp-go/function"
|
||||
)
|
||||
|
||||
func onWriteAll[W io.Writer](data []byte) func(w W) RIOE.ReaderIOEither[[]byte] {
|
||||
return func(w W) RIOE.ReaderIOEither[[]byte] {
|
||||
return F.Pipe1(
|
||||
RIOE.TryCatch(func(ctx context.Context) func() ([]byte, error) {
|
||||
return func() ([]byte, error) {
|
||||
_, err := w.Write(data)
|
||||
return data, err
|
||||
}
|
||||
}),
|
||||
RIOE.WithContext[[]byte],
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// WriteAll uses a generator function to create a stream, writes data to it and closes it
|
||||
func WriteAll[W io.WriteCloser](data []byte) func(acquire RIOE.ReaderIOEither[W]) RIOE.ReaderIOEither[[]byte] {
|
||||
onWrite := onWriteAll[W](data)
|
||||
return func(onCreate RIOE.ReaderIOEither[W]) RIOE.ReaderIOEither[[]byte] {
|
||||
return RIOE.WithResource[[]byte](
|
||||
onCreate,
|
||||
Close[W])(
|
||||
onWrite,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// Write uses a generator function to create a stream, writes data to it and closes it
|
||||
func Write[R any, W io.WriteCloser](acquire RIOE.ReaderIOEither[W]) func(use func(W) RIOE.ReaderIOEither[R]) RIOE.ReaderIOEither[R] {
|
||||
return RIOE.WithResource[R](
|
||||
acquire,
|
||||
Close[W])
|
||||
}
|
@@ -100,6 +100,28 @@ func Map[
|
||||
return RIE.Map[GRA, GRB](f)
|
||||
}
|
||||
|
||||
func MonadMapTo[
|
||||
GRA ~func(context.Context) GIOA,
|
||||
GRB ~func(context.Context) GIOB,
|
||||
|
||||
GIOA ~func() E.Either[error, A],
|
||||
GIOB ~func() E.Either[error, B],
|
||||
|
||||
A, B any](fa GRA, b B) GRB {
|
||||
return RIE.MonadMapTo[GRA, GRB](fa, b)
|
||||
}
|
||||
|
||||
func MapTo[
|
||||
GRA ~func(context.Context) GIOA,
|
||||
GRB ~func(context.Context) GIOB,
|
||||
|
||||
GIOA ~func() E.Either[error, A],
|
||||
GIOB ~func() E.Either[error, B],
|
||||
|
||||
A, B any](b B) func(GRA) GRB {
|
||||
return RIE.MapTo[GRA, GRB](b)
|
||||
}
|
||||
|
||||
func MonadChain[
|
||||
GRA ~func(context.Context) GIOA,
|
||||
GRB ~func(context.Context) GIOB,
|
||||
@@ -551,3 +573,13 @@ func MonadAlt[LAZY ~func() GEA, GEA ~func(context.Context) GIOA, GIOA ~func() E.
|
||||
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)
|
||||
}
|
||||
|
||||
// Memoize computes the value of the provided monad lazily but exactly once
|
||||
// The context used to compute the value is the context of the first call, so do not use this
|
||||
// method if the value has a functional dependency on the content of the context
|
||||
func Memoize[
|
||||
GRA ~func(context.Context) GIOA,
|
||||
GIOA ~func() E.Either[error, A],
|
||||
A any](rdr GRA) GRA {
|
||||
return RIE.Memoize[GRA](rdr)
|
||||
}
|
||||
|
@@ -61,6 +61,14 @@ func Map[A, B any](f func(A) B) func(ReaderIOEither[A]) ReaderIOEither[B] {
|
||||
return G.Map[ReaderIOEither[A], ReaderIOEither[B]](f)
|
||||
}
|
||||
|
||||
func MonadMapTo[A, B any](fa ReaderIOEither[A], b B) ReaderIOEither[B] {
|
||||
return G.MonadMapTo[ReaderIOEither[A], ReaderIOEither[B]](fa, b)
|
||||
}
|
||||
|
||||
func MapTo[A, B any](b B) func(ReaderIOEither[A]) ReaderIOEither[B] {
|
||||
return G.MapTo[ReaderIOEither[A], ReaderIOEither[B]](b)
|
||||
}
|
||||
|
||||
func MonadChain[A, B any](ma ReaderIOEither[A], f func(A) ReaderIOEither[B]) ReaderIOEither[B] {
|
||||
return G.MonadChain(ma, f)
|
||||
}
|
||||
@@ -147,6 +155,10 @@ func FromIO[A any](t IO.IO[A]) ReaderIOEither[A] {
|
||||
return G.FromIO[ReaderIOEither[A]](t)
|
||||
}
|
||||
|
||||
func FromLazy[A any](t L.Lazy[A]) ReaderIOEither[A] {
|
||||
return G.FromIO[ReaderIOEither[A]](t)
|
||||
}
|
||||
|
||||
// Never returns a 'ReaderIOEither' that never returns, except if its context gets canceled
|
||||
func Never[A any]() ReaderIOEither[A] {
|
||||
return G.Never[ReaderIOEither[A]]()
|
||||
@@ -201,3 +213,10 @@ func MonadAlt[A any](first ReaderIOEither[A], second L.Lazy[ReaderIOEither[A]])
|
||||
func Alt[A any](second L.Lazy[ReaderIOEither[A]]) func(ReaderIOEither[A]) ReaderIOEither[A] {
|
||||
return G.Alt(second)
|
||||
}
|
||||
|
||||
// Memoize computes the value of the provided [ReaderIOEither] monad lazily but exactly once
|
||||
// The context used to compute the value is the context of the first call, so do not use this
|
||||
// method if the value has a functional dependency on the content of the context
|
||||
func Memoize[A any](rdr ReaderIOEither[A]) ReaderIOEither[A] {
|
||||
return G.Memoize[ReaderIOEither[A]](rdr)
|
||||
}
|
||||
|
@@ -162,3 +162,125 @@ func TestRegularApply(t *testing.T) {
|
||||
res := applied(context.Background())()
|
||||
assert.Equal(t, E.Of[error]("CARSTEN"), res)
|
||||
}
|
||||
|
||||
func TestWithResourceNoErrors(t *testing.T) {
|
||||
var countAcquire, countBody, countRelease int
|
||||
|
||||
acquire := FromLazy(func() int {
|
||||
countAcquire++
|
||||
return countAcquire
|
||||
})
|
||||
|
||||
release := func(int) ReaderIOEither[int] {
|
||||
return FromLazy(func() int {
|
||||
countRelease++
|
||||
return countRelease
|
||||
})
|
||||
}
|
||||
|
||||
body := func(int) ReaderIOEither[int] {
|
||||
return FromLazy(func() int {
|
||||
countBody++
|
||||
return countBody
|
||||
})
|
||||
}
|
||||
|
||||
resRIOE := WithResource[int](acquire, release)(body)
|
||||
|
||||
res := resRIOE(context.Background())()
|
||||
|
||||
assert.Equal(t, 1, countAcquire)
|
||||
assert.Equal(t, 1, countBody)
|
||||
assert.Equal(t, 1, countRelease)
|
||||
assert.Equal(t, E.Of[error](1), res)
|
||||
}
|
||||
|
||||
func TestWithResourceErrorInBody(t *testing.T) {
|
||||
var countAcquire, countBody, countRelease int
|
||||
|
||||
acquire := FromLazy(func() int {
|
||||
countAcquire++
|
||||
return countAcquire
|
||||
})
|
||||
|
||||
release := func(int) ReaderIOEither[int] {
|
||||
return FromLazy(func() int {
|
||||
countRelease++
|
||||
return countRelease
|
||||
})
|
||||
}
|
||||
|
||||
err := fmt.Errorf("error in body")
|
||||
body := func(int) ReaderIOEither[int] {
|
||||
return Left[int](err)
|
||||
}
|
||||
|
||||
resRIOE := WithResource[int](acquire, release)(body)
|
||||
|
||||
res := resRIOE(context.Background())()
|
||||
|
||||
assert.Equal(t, 1, countAcquire)
|
||||
assert.Equal(t, 0, countBody)
|
||||
assert.Equal(t, 1, countRelease)
|
||||
assert.Equal(t, E.Left[int](err), res)
|
||||
}
|
||||
|
||||
func TestWithResourceErrorInAcquire(t *testing.T) {
|
||||
var countAcquire, countBody, countRelease int
|
||||
|
||||
err := fmt.Errorf("error in acquire")
|
||||
acquire := Left[int](err)
|
||||
|
||||
release := func(int) ReaderIOEither[int] {
|
||||
return FromLazy(func() int {
|
||||
countRelease++
|
||||
return countRelease
|
||||
})
|
||||
}
|
||||
|
||||
body := func(int) ReaderIOEither[int] {
|
||||
return FromLazy(func() int {
|
||||
countBody++
|
||||
return countBody
|
||||
})
|
||||
}
|
||||
|
||||
resRIOE := WithResource[int](acquire, release)(body)
|
||||
|
||||
res := resRIOE(context.Background())()
|
||||
|
||||
assert.Equal(t, 0, countAcquire)
|
||||
assert.Equal(t, 0, countBody)
|
||||
assert.Equal(t, 0, countRelease)
|
||||
assert.Equal(t, E.Left[int](err), res)
|
||||
}
|
||||
|
||||
func TestWithResourceErrorInRelease(t *testing.T) {
|
||||
var countAcquire, countBody, countRelease int
|
||||
|
||||
acquire := FromLazy(func() int {
|
||||
countAcquire++
|
||||
return countAcquire
|
||||
})
|
||||
|
||||
err := fmt.Errorf("error in release")
|
||||
release := func(int) ReaderIOEither[int] {
|
||||
return Left[int](err)
|
||||
}
|
||||
|
||||
body := func(int) ReaderIOEither[int] {
|
||||
return FromLazy(func() int {
|
||||
countBody++
|
||||
return countBody
|
||||
})
|
||||
}
|
||||
|
||||
resRIOE := WithResource[int](acquire, release)(body)
|
||||
|
||||
res := resRIOE(context.Background())()
|
||||
|
||||
assert.Equal(t, 1, countAcquire)
|
||||
assert.Equal(t, 1, countBody)
|
||||
assert.Equal(t, 0, countRelease)
|
||||
assert.Equal(t, E.Left[int](err), res)
|
||||
}
|
||||
|
@@ -27,7 +27,7 @@ import (
|
||||
var (
|
||||
// Command executes a command
|
||||
// use this version if the command does not produce any side effect, i.e. if the output is uniquely determined by by the input
|
||||
// typically you'd rather use the IOEither version of the command
|
||||
// typically you'd rather use the [IOEither] version of the command
|
||||
Command = F.Curry3(command)
|
||||
)
|
||||
|
||||
|
@@ -20,11 +20,14 @@ import (
|
||||
)
|
||||
|
||||
type (
|
||||
// command output
|
||||
// CommandOutput represents the output of executing a command. The first field in the [Tuple2] is
|
||||
// stdout, the second one is stderr. Use [StdOut] and [StdErr] to access these fields
|
||||
CommandOutput = T.Tuple2[[]byte, []byte]
|
||||
)
|
||||
|
||||
var (
|
||||
// StdOut returns the field of a [CommandOutput] representing `stdout`
|
||||
StdOut = T.First[[]byte, []byte]
|
||||
// StdErr returns the field of a [CommandOutput] representing `stderr`
|
||||
StdErr = T.Second[[]byte, []byte]
|
||||
)
|
||||
|
@@ -26,6 +26,7 @@ func Bind2nd[T1, T2, R any](f func(T1, T2) R, t2 T2) func(T1) R {
|
||||
}
|
||||
}
|
||||
|
||||
// SK function (SKI combinator calculus).
|
||||
func SK[T1, T2 any](_ T1, t2 T2) T2 {
|
||||
return t2
|
||||
}
|
||||
|
@@ -62,6 +62,7 @@ func First[T1, T2 any](t1 T1, _ T2) T1 {
|
||||
}
|
||||
|
||||
// Second returns the second out of two input values
|
||||
// Identical to [SK]
|
||||
func Second[T1, T2 any](_ T1, t2 T2) T2 {
|
||||
return t2
|
||||
}
|
||||
|
2
io/io.go
2
io/io.go
@@ -84,7 +84,7 @@ func Flatten[A any](mma IO[IO[A]]) IO[A] {
|
||||
return G.Flatten(mma)
|
||||
}
|
||||
|
||||
// Memoize computes the value of the provided IO monad lazily but exactly once
|
||||
// Memoize computes the value of the provided [IO] monad lazily but exactly once
|
||||
func Memoize[A any](ma IO[A]) IO[A] {
|
||||
return G.Memoize(ma)
|
||||
}
|
||||
|
@@ -1,29 +0,0 @@
|
||||
// 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 file
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
||||
IOE "github.com/IBM/fp-go/ioeither"
|
||||
)
|
||||
|
||||
// onClose closes a closeable resource
|
||||
func onClose[R io.Closer](r R) IOE.IOEither[error, R] {
|
||||
return IOE.TryCatchError(func() (R, error) {
|
||||
return r, r.Close()
|
||||
})
|
||||
}
|
@@ -16,6 +16,7 @@
|
||||
package file
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
|
||||
IOE "github.com/IBM/fp-go/ioeither"
|
||||
@@ -45,3 +46,10 @@ func Remove(name string) IOE.IOEither[error, string] {
|
||||
return name, os.Remove(name)
|
||||
})
|
||||
}
|
||||
|
||||
// Close closes an object
|
||||
func Close[C io.Closer](c C) IOE.IOEither[error, any] {
|
||||
return IOE.TryCatchError(func() (any, error) {
|
||||
return c, c.Close()
|
||||
})
|
||||
}
|
||||
|
@@ -31,7 +31,7 @@ func onReadAll[R io.Reader](r R) IOE.IOEither[error, []byte] {
|
||||
func ReadAll[R io.ReadCloser](acquire IOE.IOEither[error, R]) IOE.IOEither[error, []byte] {
|
||||
return IOE.WithResource[[]byte](
|
||||
acquire,
|
||||
onClose[R])(
|
||||
Close[R])(
|
||||
onReadAll[R],
|
||||
)
|
||||
}
|
||||
|
@@ -38,7 +38,7 @@ func TestWithTempFileOnClosedFile(t *testing.T) {
|
||||
return F.Pipe2(
|
||||
f,
|
||||
onWriteAll[*os.File]([]byte("Carsten")),
|
||||
IOE.ChainFirst(F.Constant1[[]byte](onClose(f))),
|
||||
IOE.ChainFirst(F.Constant1[[]byte](Close(f))),
|
||||
)
|
||||
})
|
||||
|
||||
|
@@ -36,7 +36,7 @@ func WriteAll[W io.WriteCloser](data []byte) func(acquire IOE.IOEither[error, W]
|
||||
return func(onCreate IOE.IOEither[error, W]) IOE.IOEither[error, []byte] {
|
||||
return IOE.WithResource[[]byte](
|
||||
onCreate,
|
||||
onClose[W])(
|
||||
Close[W])(
|
||||
onWrite,
|
||||
)
|
||||
}
|
||||
@@ -46,5 +46,5 @@ func WriteAll[W io.WriteCloser](data []byte) func(acquire IOE.IOEither[error, W]
|
||||
func Write[R any, W io.WriteCloser](acquire IOE.IOEither[error, W]) func(use func(W) IOE.IOEither[error, R]) IOE.IOEither[error, R] {
|
||||
return IOE.WithResource[R](
|
||||
acquire,
|
||||
onClose[W])
|
||||
Close[W])
|
||||
}
|
||||
|
@@ -75,10 +75,20 @@ func ChainIOK[E, A, B any](f func(A) I.IO[B]) func(IOEither[E, A]) IOEither[E, B
|
||||
return G.ChainIOK[IOEither[E, A], IOEither[E, B]](f)
|
||||
}
|
||||
|
||||
func ChainLazyK[E, A, B any](f func(A) L.Lazy[B]) func(IOEither[E, A]) IOEither[E, B] {
|
||||
return G.ChainIOK[IOEither[E, A], IOEither[E, B]](f)
|
||||
}
|
||||
|
||||
// FromIO creates an [IOEither] from an [IO] instance, invoking [IO] for each invocation of [IOEither]
|
||||
func FromIO[E, A any](mr I.IO[A]) IOEither[E, A] {
|
||||
return G.FromIO[IOEither[E, A]](mr)
|
||||
}
|
||||
|
||||
// FromLazy creates an [IOEither] from a [Lazy] instance, invoking [Lazy] for each invocation of [IOEither]
|
||||
func FromLazy[E, A any](mr L.Lazy[A]) IOEither[E, A] {
|
||||
return G.FromIO[IOEither[E, A]](mr)
|
||||
}
|
||||
|
||||
func MonadMap[E, A, B any](fa IOEither[E, A], f func(A) B) IOEither[E, B] {
|
||||
return G.MonadMap[IOEither[E, A], IOEither[E, B]](fa, f)
|
||||
}
|
||||
@@ -167,27 +177,27 @@ func MonadChainTo[E, A, B any](fa IOEither[E, A], fb IOEither[E, B]) IOEither[E,
|
||||
return G.MonadChainTo(fa, fb)
|
||||
}
|
||||
|
||||
// ChainTo composes to the second monad ignoring the return value of the first
|
||||
// ChainTo composes to the second [IOEither] monad ignoring the return value of the first
|
||||
func ChainTo[E, A, B any](fb IOEither[E, B]) func(IOEither[E, A]) IOEither[E, B] {
|
||||
return G.ChainTo[IOEither[E, A]](fb)
|
||||
}
|
||||
|
||||
// MonadChainFirst runs the monad returned by the function but returns the result of the original monad
|
||||
// MonadChainFirst runs the [IOEither] monad returned by the function but returns the result of the original monad
|
||||
func MonadChainFirst[E, A, B any](ma IOEither[E, A], f func(A) IOEither[E, B]) IOEither[E, A] {
|
||||
return G.MonadChainFirst(ma, f)
|
||||
}
|
||||
|
||||
// ChainFirst runs the monad returned by the function but returns the result of the original monad
|
||||
// ChainFirst runs the [IOEither] monad returned by the function but returns the result of the original monad
|
||||
func ChainFirst[E, A, B any](f func(A) IOEither[E, B]) func(IOEither[E, A]) IOEither[E, A] {
|
||||
return G.ChainFirst[IOEither[E, A]](f)
|
||||
}
|
||||
|
||||
// MonadChainFirstIOK runs the monad returned by the function but returns the result of the original monad
|
||||
// MonadChainFirstIOK runs [IO] the monad returned by the function but returns the result of the original monad
|
||||
func MonadChainFirstIOK[E, A, B any](ma IOEither[E, A], f func(A) I.IO[B]) IOEither[E, A] {
|
||||
return G.MonadChainFirstIOK(ma, f)
|
||||
}
|
||||
|
||||
// ChainFirsIOKt runs the monad returned by the function but returns the result of the original monad
|
||||
// ChainFirstIOK runs the [IO] monad returned by the function but returns the result of the original monad
|
||||
func ChainFirstIOK[E, A, B any](f func(A) I.IO[B]) func(IOEither[E, A]) IOEither[E, A] {
|
||||
return G.ChainFirstIOK[IOEither[E, A]](f)
|
||||
}
|
||||
|
@@ -18,6 +18,7 @@ package generic
|
||||
import (
|
||||
"time"
|
||||
|
||||
ET "github.com/IBM/fp-go/either"
|
||||
F "github.com/IBM/fp-go/function"
|
||||
FI "github.com/IBM/fp-go/internal/fromio"
|
||||
"github.com/IBM/fp-go/internal/optiont"
|
||||
@@ -55,6 +56,21 @@ func FromOption[GA ~func() O.Option[A], A any](o O.Option[A]) GA {
|
||||
return IO.Of[GA](o)
|
||||
}
|
||||
|
||||
func FromEither[GA ~func() O.Option[A], E, A any](e ET.Either[E, A]) GA {
|
||||
return F.Pipe2(
|
||||
e,
|
||||
ET.ToOption[E, A],
|
||||
FromOption[GA],
|
||||
)
|
||||
}
|
||||
|
||||
func FromIOEither[GA ~func() O.Option[A], GEA ~func() ET.Either[E, A], E, A any](ioe GEA) GA {
|
||||
return F.Pipe1(
|
||||
ioe,
|
||||
IO.Map[GEA, GA](ET.ToOption[E, A]),
|
||||
)
|
||||
}
|
||||
|
||||
func MonadMap[GA ~func() O.Option[A], GB ~func() O.Option[B], A, B any](fa GA, f func(A) B) GB {
|
||||
return optiont.MonadMap(IO.MonadMap[GA, GB, O.Option[A], O.Option[B]], fa, f)
|
||||
}
|
||||
|
@@ -16,7 +16,9 @@
|
||||
package iooption
|
||||
|
||||
import (
|
||||
ET "github.com/IBM/fp-go/either"
|
||||
I "github.com/IBM/fp-go/io"
|
||||
IOE "github.com/IBM/fp-go/ioeither"
|
||||
G "github.com/IBM/fp-go/iooption/generic"
|
||||
O "github.com/IBM/fp-go/option"
|
||||
)
|
||||
@@ -117,7 +119,7 @@ func Memoize[A any](ma IOOption[A]) IOOption[A] {
|
||||
return G.Memoize(ma)
|
||||
}
|
||||
|
||||
// Fold convers an IOOption into an IO
|
||||
// Fold convers an [IOOption] into an [IO]
|
||||
func Fold[A, B any](onNone func() I.IO[B], onSome func(A) I.IO[B]) func(IOOption[A]) I.IO[B] {
|
||||
return G.Fold[IOOption[A]](onNone, onSome)
|
||||
}
|
||||
@@ -126,3 +128,13 @@ func Fold[A, B any](onNone func() I.IO[B], onSome func(A) I.IO[B]) func(IOOption
|
||||
func Defer[A any](gen func() IOOption[A]) IOOption[A] {
|
||||
return G.Defer[IOOption[A]](gen)
|
||||
}
|
||||
|
||||
// FromIOEither converts an [IOEither] into an [IOOption]
|
||||
func FromIOEither[E, A any](ioe IOE.IOEither[E, A]) IOOption[A] {
|
||||
return G.FromIOEither[IOOption[A]](ioe)
|
||||
}
|
||||
|
||||
// FromEither converts an [Either] into an [IOOption]
|
||||
func FromEither[E, A any](e ET.Either[E, A]) IOOption[A] {
|
||||
return G.FromEither[IOOption[A]](e)
|
||||
}
|
||||
|
65
iterator/stateless/benchmark_test.go
Normal file
65
iterator/stateless/benchmark_test.go
Normal file
@@ -0,0 +1,65 @@
|
||||
// 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 stateless
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
F "github.com/IBM/fp-go/function"
|
||||
)
|
||||
|
||||
func BenchmarkMulti(b *testing.B) {
|
||||
// run the Fib function b.N times
|
||||
for n := 0; n < b.N; n++ {
|
||||
single()
|
||||
}
|
||||
}
|
||||
|
||||
func single() int64 {
|
||||
|
||||
length := 10000
|
||||
nums := make([]int, 0, length)
|
||||
for i := 0; i < length; i++ {
|
||||
nums = append(nums, i+1)
|
||||
}
|
||||
|
||||
return F.Pipe6(
|
||||
nums,
|
||||
FromArray[int],
|
||||
Filter(func(n int) bool {
|
||||
return n%2 == 0
|
||||
}),
|
||||
Map(func(t int) int64 {
|
||||
return int64(t)
|
||||
}),
|
||||
Filter(func(t int64) bool {
|
||||
n := t
|
||||
for n/10 != 0 {
|
||||
if n%10 == 4 {
|
||||
return false
|
||||
}
|
||||
n = n / 10
|
||||
}
|
||||
return true
|
||||
}),
|
||||
Map(func(t int64) int {
|
||||
return int(t)
|
||||
}),
|
||||
Reduce(func(n int64, r int) int64 {
|
||||
return n + int64(r)
|
||||
}, int64(0)),
|
||||
)
|
||||
}
|
@@ -82,6 +82,7 @@ func Map[GV ~func() O.Option[T.Tuple2[GV, V]], GU ~func() O.Option[T.Tuple2[GU,
|
||||
m,
|
||||
)
|
||||
}
|
||||
|
||||
m = O.Map(T.Map2(recurse, f))
|
||||
|
||||
return recurse
|
||||
|
@@ -16,6 +16,8 @@
|
||||
package generic
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
F "github.com/IBM/fp-go/function"
|
||||
FR "github.com/IBM/fp-go/internal/fromreader"
|
||||
"github.com/IBM/fp-go/internal/readert"
|
||||
@@ -99,3 +101,26 @@ func Defer[GEA ~func(E) GA, GA ~func() A, E, A any](gen func() GEA) GEA {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Memoize computes the value of the provided reader monad lazily but exactly once
|
||||
// The context used to compute the value is the context of the first call, so do not use this
|
||||
// method if the value has a functional dependency on the content of the context
|
||||
func Memoize[GEA ~func(E) GA, GA ~func() A, E, A any](rdr GEA) GEA {
|
||||
// synchronization primitives
|
||||
var once sync.Once
|
||||
var result A
|
||||
// callback
|
||||
gen := func(e E) func() {
|
||||
return func() {
|
||||
result = rdr(e)()
|
||||
}
|
||||
}
|
||||
// returns our memoized wrapper
|
||||
return func(e E) GA {
|
||||
io := gen(e)
|
||||
return func() A {
|
||||
once.Do(io)
|
||||
return result
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -75,3 +75,10 @@ func ChainIOK[E, A, B any](f func(A) IO.IO[B]) func(ReaderIO[E, A]) ReaderIO[E,
|
||||
func Defer[E, A any](gen func() ReaderIO[E, A]) ReaderIO[E, A] {
|
||||
return G.Defer[ReaderIO[E, A]](gen)
|
||||
}
|
||||
|
||||
// Memoize computes the value of the provided [ReaderIO] monad lazily but exactly once
|
||||
// The context used to compute the value is the context of the first call, so do not use this
|
||||
// method if the value has a functional dependency on the content of the context
|
||||
func Memoize[E, A any](rdr ReaderIO[E, A]) ReaderIO[E, A] {
|
||||
return G.Memoize[ReaderIO[E, A]](rdr)
|
||||
}
|
||||
|
@@ -406,3 +406,11 @@ func TryCatch[GEA ~func(R) GA, GA ~func() ET.Either[E, A], R, E, A any](f func(R
|
||||
return IOE.TryCatch[GA](f(r), onThrow)
|
||||
}
|
||||
}
|
||||
|
||||
// Memoize computes the value of the provided monad lazily but exactly once
|
||||
// The context used to compute the value is the context of the first call, so do not use this
|
||||
// method if the value has a functional dependency on the content of the context
|
||||
func Memoize[
|
||||
GEA ~func(R) GIOA, GIOA ~func() ET.Either[E, A], R, E, A any](rdr GEA) GEA {
|
||||
return G.Memoize[GEA](rdr)
|
||||
}
|
||||
|
@@ -264,3 +264,11 @@ func MonadAlt[R, E, A any](first ReaderIOEither[R, E, A], second L.Lazy[ReaderIO
|
||||
func Alt[R, E, A any](second L.Lazy[ReaderIOEither[R, E, A]]) func(ReaderIOEither[R, E, A]) ReaderIOEither[R, E, A] {
|
||||
return G.Alt(second)
|
||||
}
|
||||
|
||||
// Memoize computes the value of the provided [ReaderIOEither] monad lazily but exactly once
|
||||
// The context used to compute the value is the context of the first call, so do not use this
|
||||
// method if the value has a functional dependency on the content of the context
|
||||
func Memoize[
|
||||
R, E, A any](rdr ReaderIOEither[R, E, A]) ReaderIOEither[R, E, A] {
|
||||
return G.Memoize[ReaderIOEither[R, E, A]](rdr)
|
||||
}
|
||||
|
@@ -74,9 +74,7 @@ func TestMultipleHttpRequests(t *testing.T) {
|
||||
assert.Equal(t, E.Of[error](count), result())
|
||||
}
|
||||
|
||||
// TestHeterogeneousHttpRequests shows how to execute multiple HTTP requests in parallel when
|
||||
// the response structure of these requests is different. We use [R.TraverseTuple2] to account for the different types
|
||||
func TestHeterogeneousHttpRequests(t *testing.T) {
|
||||
func heterogeneousHttpRequests() R.ReaderIOEither[T.Tuple2[PostItem, CatFact]] {
|
||||
// prepare the http client
|
||||
client := H.MakeClient(HTTP.DefaultClient)
|
||||
// readSinglePost sends a GET request and parses the response as [PostItem]
|
||||
@@ -84,7 +82,7 @@ func TestHeterogeneousHttpRequests(t *testing.T) {
|
||||
// readSingleCatFact sends a GET request and parses the response as [CatFact]
|
||||
readSingleCatFact := H.ReadJson[CatFact](client)
|
||||
|
||||
data := F.Pipe3(
|
||||
return F.Pipe3(
|
||||
T.MakeTuple2("https://jsonplaceholder.typicode.com/posts/1", "https://catfact.ninja/fact"),
|
||||
T.Map2(H.MakeGetRequest, H.MakeGetRequest),
|
||||
R.TraverseTuple2(
|
||||
@@ -94,7 +92,21 @@ func TestHeterogeneousHttpRequests(t *testing.T) {
|
||||
R.ChainFirstIOK(IO.Logf[T.Tuple2[PostItem, CatFact]]("Log Result: %v")),
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
// TestHeterogeneousHttpRequests shows how to execute multiple HTTP requests in parallel when
|
||||
// the response structure of these requests is different. We use [R.TraverseTuple2] to account for the different types
|
||||
func TestHeterogeneousHttpRequests(t *testing.T) {
|
||||
data := heterogeneousHttpRequests()
|
||||
|
||||
result := data(context.Background())
|
||||
|
||||
fmt.Println(result())
|
||||
}
|
||||
|
||||
// BenchmarkHeterogeneousHttpRequests shows how to execute multiple HTTP requests in parallel when
|
||||
// the response structure of these requests is different. We use [R.TraverseTuple2] to account for the different types
|
||||
func BenchmarkHeterogeneousHttpRequests(b *testing.B) {
|
||||
|
||||
heterogeneousHttpRequests()(context.Background())
|
||||
}
|
||||
|
59
samples/tuples/option_test.go
Normal file
59
samples/tuples/option_test.go
Normal file
@@ -0,0 +1,59 @@
|
||||
// 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 tuples
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
F "github.com/IBM/fp-go/function"
|
||||
IOE "github.com/IBM/fp-go/ioeither"
|
||||
IOEF "github.com/IBM/fp-go/ioeither/file"
|
||||
IOEG "github.com/IBM/fp-go/ioeither/generic"
|
||||
IOO "github.com/IBM/fp-go/iooption"
|
||||
)
|
||||
|
||||
func TestIOEitherToOption1(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
content := []byte("abc")
|
||||
|
||||
resIOO := F.Pipe2(
|
||||
content,
|
||||
IOEF.WriteFile(filepath.Join(tmpDir, "test.txt"), os.ModePerm),
|
||||
IOEG.Fold[IOE.IOEither[error, []byte]](
|
||||
IOO.Of[error],
|
||||
F.Ignore1of1[[]byte](IOO.None[error]),
|
||||
),
|
||||
)
|
||||
|
||||
fmt.Println(resIOO())
|
||||
}
|
||||
|
||||
func TestIOEitherToOption2(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
content := []byte("abc")
|
||||
|
||||
resIOO := F.Pipe3(
|
||||
content,
|
||||
IOEF.WriteFile(filepath.Join(tmpDir, "test.txt"), os.ModePerm),
|
||||
IOE.Swap[error, []byte],
|
||||
IOO.FromIOEither[[]byte, error],
|
||||
)
|
||||
|
||||
fmt.Println(resIOO())
|
||||
}
|
1
samples/tuples/samples/data.txt
Normal file
1
samples/tuples/samples/data.txt
Normal file
@@ -0,0 +1 @@
|
||||
abc
|
130
samples/tuples/tuple_test.go
Normal file
130
samples/tuples/tuple_test.go
Normal file
@@ -0,0 +1,130 @@
|
||||
// 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 tuples
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
E "github.com/IBM/fp-go/either"
|
||||
F "github.com/IBM/fp-go/function"
|
||||
IOE "github.com/IBM/fp-go/ioeither"
|
||||
IOEF "github.com/IBM/fp-go/ioeither/file"
|
||||
T "github.com/IBM/fp-go/tuple"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func sampleConvertDocx(r io.Reader) (string, map[string]string, error) {
|
||||
content, err := io.ReadAll(r)
|
||||
return string(content), map[string]string{}, err
|
||||
}
|
||||
|
||||
func TestSampleConvertDocx1(t *testing.T) {
|
||||
// this conversion approach has the disadvantage that it exhausts the reader
|
||||
// so we cannot invoke the resulting IOEither multiple times
|
||||
convertDocx := func(r io.Reader) IOE.IOEither[error, T.Tuple2[string, map[string]string]] {
|
||||
return IOE.TryCatchError(func() (T.Tuple2[string, map[string]string], error) {
|
||||
text, meta, err := sampleConvertDocx(r)
|
||||
return T.MakeTuple2(text, meta), err
|
||||
})
|
||||
}
|
||||
|
||||
rdr := strings.NewReader("abc")
|
||||
resIOE := convertDocx(rdr)
|
||||
|
||||
resE := resIOE()
|
||||
|
||||
assert.True(t, E.IsRight(resE))
|
||||
}
|
||||
|
||||
func TestSampleConvertDocx2(t *testing.T) {
|
||||
// this approach assumes that `sampleConvertDocx` does not have any side effects
|
||||
// other than reading from a `Reader`. As a consequence it can be a pure function itself.
|
||||
// The disadvantage is that its input has to exist in memory which is probably not a good
|
||||
// idea for large inputs
|
||||
convertDocx := func(data []byte) E.Either[error, T.Tuple2[string, map[string]string]] {
|
||||
return E.TryCatchError(func() (T.Tuple2[string, map[string]string], error) {
|
||||
text, meta, err := sampleConvertDocx(bytes.NewReader(data))
|
||||
return T.MakeTuple2(text, meta), err
|
||||
})
|
||||
}
|
||||
|
||||
resE := convertDocx([]byte("abc"))
|
||||
|
||||
assert.True(t, E.IsRight(resE))
|
||||
}
|
||||
|
||||
// onClose closes a closeable resource
|
||||
func onClose[R io.Closer](r R) IOE.IOEither[error, R] {
|
||||
return IOE.TryCatchError(func() (R, error) {
|
||||
return r, r.Close()
|
||||
})
|
||||
}
|
||||
|
||||
// convertDocx3 takes an `acquire` function that creates an instance or a [ReaderCloser] whenever the resulting [IOEither] is invoked. Since
|
||||
// we return a [Closer] the instance will be closed after use, automatically. This design makes sure that the resulting [IOEither] can be invoked
|
||||
// as many times as necessary
|
||||
func convertDocx3[R io.ReadCloser](acquire IOE.IOEither[error, R]) IOE.IOEither[error, T.Tuple2[string, map[string]string]] {
|
||||
return IOE.WithResource[T.Tuple2[string, map[string]string]](
|
||||
acquire,
|
||||
onClose[R])(
|
||||
func(r R) IOE.IOEither[error, T.Tuple2[string, map[string]string]] {
|
||||
return IOE.TryCatchError(func() (T.Tuple2[string, map[string]string], error) {
|
||||
text, meta, err := sampleConvertDocx(r)
|
||||
return T.MakeTuple2(text, meta), err
|
||||
})
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// convertDocx4 takes an `acquire` function that creates an instance or a [Reader] whenever the resulting [IOEither] is invoked.
|
||||
// This design makes sure that the resulting [IOEither] can be invoked
|
||||
// as many times as necessary
|
||||
func convertDocx4[R io.Reader](acquire IOE.IOEither[error, R]) IOE.IOEither[error, T.Tuple2[string, map[string]string]] {
|
||||
return F.Pipe1(
|
||||
acquire,
|
||||
IOE.Chain(func(r R) IOE.IOEither[error, T.Tuple2[string, map[string]string]] {
|
||||
return IOE.TryCatchError(func() (T.Tuple2[string, map[string]string], error) {
|
||||
text, meta, err := sampleConvertDocx(r)
|
||||
return T.MakeTuple2(text, meta), err
|
||||
})
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
func TestSampleConvertDocx3(t *testing.T) {
|
||||
// IOEither that creates the reader
|
||||
acquire := IOEF.Open("./samples/data.txt")
|
||||
|
||||
resIOE := convertDocx3(acquire)
|
||||
resE := resIOE()
|
||||
|
||||
assert.True(t, E.IsRight(resE))
|
||||
}
|
||||
|
||||
func TestSampleConvertDocx4(t *testing.T) {
|
||||
// IOEither that creates the reader
|
||||
acquire := IOE.FromIO[error](func() *strings.Reader {
|
||||
return strings.NewReader("abc")
|
||||
})
|
||||
|
||||
resIOE := convertDocx4(acquire)
|
||||
resE := resIOE()
|
||||
|
||||
assert.True(t, E.IsRight(resE))
|
||||
}
|
20
tuple/gen.go
20
tuple/gen.go
@@ -111,7 +111,7 @@ func MakeTuple1[T1 any](t1 T1) Tuple1[T1] {
|
||||
return Tuple1[T1]{t1}
|
||||
}
|
||||
|
||||
// Tupled1 converts a function with 1 parameters returning into a function taking a Tuple1
|
||||
// Tupled1 converts a function with 1 parameters into a function taking a Tuple1
|
||||
// The inverse function is [Untupled1]
|
||||
func Tupled1[F ~func(T1) R, T1, R any](f F) func(Tuple1[T1]) R {
|
||||
return func(t Tuple1[T1]) R {
|
||||
@@ -217,7 +217,7 @@ func MakeTuple2[T1, T2 any](t1 T1, t2 T2) Tuple2[T1, T2] {
|
||||
return Tuple2[T1, T2]{t1, t2}
|
||||
}
|
||||
|
||||
// Tupled2 converts a function with 2 parameters returning into a function taking a Tuple2
|
||||
// Tupled2 converts a function with 2 parameters into a function taking a Tuple2
|
||||
// The inverse function is [Untupled2]
|
||||
func Tupled2[F ~func(T1, T2) R, T1, T2, R any](f F) func(Tuple2[T1, T2]) R {
|
||||
return func(t Tuple2[T1, T2]) R {
|
||||
@@ -334,7 +334,7 @@ func MakeTuple3[T1, T2, T3 any](t1 T1, t2 T2, t3 T3) Tuple3[T1, T2, T3] {
|
||||
return Tuple3[T1, T2, T3]{t1, t2, t3}
|
||||
}
|
||||
|
||||
// Tupled3 converts a function with 3 parameters returning into a function taking a Tuple3
|
||||
// Tupled3 converts a function with 3 parameters into a function taking a Tuple3
|
||||
// The inverse function is [Untupled3]
|
||||
func Tupled3[F ~func(T1, T2, T3) R, T1, T2, T3, R any](f F) func(Tuple3[T1, T2, T3]) R {
|
||||
return func(t Tuple3[T1, T2, T3]) R {
|
||||
@@ -462,7 +462,7 @@ func MakeTuple4[T1, T2, T3, T4 any](t1 T1, t2 T2, t3 T3, t4 T4) Tuple4[T1, T2, T
|
||||
return Tuple4[T1, T2, T3, T4]{t1, t2, t3, t4}
|
||||
}
|
||||
|
||||
// Tupled4 converts a function with 4 parameters returning into a function taking a Tuple4
|
||||
// Tupled4 converts a function with 4 parameters into a function taking a Tuple4
|
||||
// The inverse function is [Untupled4]
|
||||
func Tupled4[F ~func(T1, T2, T3, T4) R, T1, T2, T3, T4, R any](f F) func(Tuple4[T1, T2, T3, T4]) R {
|
||||
return func(t Tuple4[T1, T2, T3, T4]) R {
|
||||
@@ -601,7 +601,7 @@ func MakeTuple5[T1, T2, T3, T4, T5 any](t1 T1, t2 T2, t3 T3, t4 T4, t5 T5) Tuple
|
||||
return Tuple5[T1, T2, T3, T4, T5]{t1, t2, t3, t4, t5}
|
||||
}
|
||||
|
||||
// Tupled5 converts a function with 5 parameters returning into a function taking a Tuple5
|
||||
// Tupled5 converts a function with 5 parameters into a function taking a Tuple5
|
||||
// The inverse function is [Untupled5]
|
||||
func Tupled5[F ~func(T1, T2, T3, T4, T5) R, T1, T2, T3, T4, T5, R any](f F) func(Tuple5[T1, T2, T3, T4, T5]) R {
|
||||
return func(t Tuple5[T1, T2, T3, T4, T5]) R {
|
||||
@@ -751,7 +751,7 @@ func MakeTuple6[T1, T2, T3, T4, T5, T6 any](t1 T1, t2 T2, t3 T3, t4 T4, t5 T5, t
|
||||
return Tuple6[T1, T2, T3, T4, T5, T6]{t1, t2, t3, t4, t5, t6}
|
||||
}
|
||||
|
||||
// Tupled6 converts a function with 6 parameters returning into a function taking a Tuple6
|
||||
// Tupled6 converts a function with 6 parameters into a function taking a Tuple6
|
||||
// The inverse function is [Untupled6]
|
||||
func Tupled6[F ~func(T1, T2, T3, T4, T5, T6) R, T1, T2, T3, T4, T5, T6, R any](f F) func(Tuple6[T1, T2, T3, T4, T5, T6]) R {
|
||||
return func(t Tuple6[T1, T2, T3, T4, T5, T6]) R {
|
||||
@@ -912,7 +912,7 @@ func MakeTuple7[T1, T2, T3, T4, T5, T6, T7 any](t1 T1, t2 T2, t3 T3, t4 T4, t5 T
|
||||
return Tuple7[T1, T2, T3, T4, T5, T6, T7]{t1, t2, t3, t4, t5, t6, t7}
|
||||
}
|
||||
|
||||
// Tupled7 converts a function with 7 parameters returning into a function taking a Tuple7
|
||||
// Tupled7 converts a function with 7 parameters into a function taking a Tuple7
|
||||
// The inverse function is [Untupled7]
|
||||
func Tupled7[F ~func(T1, T2, T3, T4, T5, T6, T7) R, T1, T2, T3, T4, T5, T6, T7, R any](f F) func(Tuple7[T1, T2, T3, T4, T5, T6, T7]) R {
|
||||
return func(t Tuple7[T1, T2, T3, T4, T5, T6, T7]) R {
|
||||
@@ -1084,7 +1084,7 @@ func MakeTuple8[T1, T2, T3, T4, T5, T6, T7, T8 any](t1 T1, t2 T2, t3 T3, t4 T4,
|
||||
return Tuple8[T1, T2, T3, T4, T5, T6, T7, T8]{t1, t2, t3, t4, t5, t6, t7, t8}
|
||||
}
|
||||
|
||||
// Tupled8 converts a function with 8 parameters returning into a function taking a Tuple8
|
||||
// Tupled8 converts a function with 8 parameters into a function taking a Tuple8
|
||||
// The inverse function is [Untupled8]
|
||||
func Tupled8[F ~func(T1, T2, T3, T4, T5, T6, T7, T8) R, T1, T2, T3, T4, T5, T6, T7, T8, R any](f F) func(Tuple8[T1, T2, T3, T4, T5, T6, T7, T8]) R {
|
||||
return func(t Tuple8[T1, T2, T3, T4, T5, T6, T7, T8]) R {
|
||||
@@ -1267,7 +1267,7 @@ func MakeTuple9[T1, T2, T3, T4, T5, T6, T7, T8, T9 any](t1 T1, t2 T2, t3 T3, t4
|
||||
return Tuple9[T1, T2, T3, T4, T5, T6, T7, T8, T9]{t1, t2, t3, t4, t5, t6, t7, t8, t9}
|
||||
}
|
||||
|
||||
// Tupled9 converts a function with 9 parameters returning into a function taking a Tuple9
|
||||
// Tupled9 converts a function with 9 parameters into a function taking a Tuple9
|
||||
// The inverse function is [Untupled9]
|
||||
func Tupled9[F ~func(T1, T2, T3, T4, T5, T6, T7, T8, T9) R, T1, T2, T3, T4, T5, T6, T7, T8, T9, R any](f F) func(Tuple9[T1, T2, T3, T4, T5, T6, T7, T8, T9]) R {
|
||||
return func(t Tuple9[T1, T2, T3, T4, T5, T6, T7, T8, T9]) R {
|
||||
@@ -1461,7 +1461,7 @@ func MakeTuple10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10 any](t1 T1, t2 T2, t3 T
|
||||
return Tuple10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]{t1, t2, t3, t4, t5, t6, t7, t8, t9, t10}
|
||||
}
|
||||
|
||||
// Tupled10 converts a function with 10 parameters returning into a function taking a Tuple10
|
||||
// Tupled10 converts a function with 10 parameters into a function taking a Tuple10
|
||||
// The inverse function is [Untupled10]
|
||||
func Tupled10[F ~func(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10) R, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, R any](f F) func(Tuple10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]) R {
|
||||
return func(t Tuple10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]) R {
|
||||
|
@@ -21,10 +21,12 @@ func Of[T1 any](t T1) Tuple1[T1] {
|
||||
return MakeTuple1(t)
|
||||
}
|
||||
|
||||
// First returns the first element of a [Tuple2]
|
||||
func First[T1, T2 any](t Tuple2[T1, T2]) T1 {
|
||||
return t.F1
|
||||
}
|
||||
|
||||
// Second returns the second element of a [Tuple2]
|
||||
func Second[T1, T2 any](t Tuple2[T1, T2]) T2 {
|
||||
return t.F2
|
||||
}
|
||||
|
Reference in New Issue
Block a user