Skip to content

Commit

Permalink
feat: add LiftFunc
Browse files Browse the repository at this point in the history
  • Loading branch information
Primetalk committed Oct 16, 2022
1 parent bc53e26 commit 857e9b4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 22 deletions.
15 changes: 15 additions & 0 deletions io/common_test.go
Expand Up @@ -2,9 +2,11 @@ package io_test

import (
"errors"
"log"
"testing"

"github.com/primetalk/goio/io"
"github.com/primetalk/goio/slice"
"github.com/stretchr/testify/assert"
)

Expand All @@ -14,6 +16,10 @@ var errExpected = errors.New(errorMessage)

var failure = io.Fail[string](errExpected)

func inc(i int) int {
return i + 1
}

func UnsafeIO[A any](t *testing.T, ioa io.IO[A]) A {
res, err1 := io.UnsafeRunSync(ioa)
assert.NoError(t, err1)
Expand All @@ -26,3 +32,12 @@ func UnsafeIOExpectError[A any](t *testing.T, expected error, ioa io.IO[A]) {
assert.Equal(t, expected, err1)
}
}

func Nats(count int) (ios []io.IO[int]) {
return slice.Map(slice.Range(0, count), func(i int) io.IO[int] {
return io.Pure(func() int {
log.Printf("executing %v\n", i)
return i
})
})
}
7 changes: 7 additions & 0 deletions io/io.go
Expand Up @@ -157,6 +157,13 @@ func Lift[A any](a A) IO[A] {
return LiftPair(a, nil)
}

// LiftFunc wraps the result of function into IO.
func LiftFunc[A any, B any](f func(A) B) func(A) IO[B] {
return func(a A) IO[B] {
return Lift(f(a))
}
}

// Fail[A] constructs an IO[A] that fails with the given error.
func Fail[A any](err error) IO[A] {
var a A
Expand Down
33 changes: 11 additions & 22 deletions io/io_test.go
@@ -1,7 +1,6 @@
package io_test

import (
"log"
"testing"

"github.com/primetalk/goio/fun"
Expand All @@ -17,19 +16,22 @@ func TestIO(t *testing.T) {
return i + j, nil
})
})
res, err := io.UnsafeRunSync(io30)
assert.Equal(t, err, nil)
assert.Equal(t, res, 30)
res := UnsafeIO(t, io30)
assert.Equal(t, 30, res)
}

func TestLiftFunc(t *testing.T) {
f := io.LiftFunc(inc)
assert.Equal(t, 11, UnsafeIO(t, f(10)))
}

func TestErr(t *testing.T) {
var ptr *string = nil
ptrio := io.Lift(ptr)
uptr := io.FlatMap(ptrio, io.Unptr[string])
_, err := io.UnsafeRunSync(uptr)
assert.Equal(t, io.ErrorNPE, err)
UnsafeIOExpectError(t, io.ErrorNPE, uptr)
wrappedUptr := io.Wrapf(uptr, "my message %d", 10)
_, err = io.UnsafeRunSync(wrappedUptr)
_, err := io.UnsafeRunSync(wrappedUptr)
assert.Equal(t, "my message 10: nil pointer", err.Error())
}

Expand All @@ -40,28 +42,15 @@ func TestFinally(t *testing.T) {
oe := io.OnError(fin, func(err error) io.IO[fun.Unit] {
return io.FromPureEffect(func() { onErrorExecuted = true })
})
_, err := io.UnsafeRunSync(oe)
assert.Error(t, err, errorMessage)
UnsafeIOExpectError(t, errExpected, oe)
assert.True(t, finalizerExecuted)
assert.True(t, onErrorExecuted)
}

func Nats(count int) (ios []io.IO[int]) {
for i := 0; i < count; i += 1 {
j := i
ios = append(ios, io.Pure(func() int {
log.Printf("executing %v\n", j)
return j
}))
}
return
}

func TestSequence(t *testing.T) {
ios := Nats(10)
for i, io1 := range ios {
res, err := io.UnsafeRunSync(io1)
assert.NoError(t, err)
res := UnsafeIO(t, io1)
assert.Equal(t, i, res)
}
ioseq := io.Sequence(ios)
Expand Down

0 comments on commit 857e9b4

Please sign in to comment.