1
0
mirror of https://github.com/IBM/fp-go.git synced 2025-08-24 19:29:11 +02:00

Compare commits

...

4 Commits

Author SHA1 Message Date
Carsten Leue
89265eed7c Merge pull request #15 from IBM/cleue-implement-with-temp-file
fix: add WithTempFile
2023-07-30 16:05:00 +02:00
Dr. Carsten Leue
fbc6757f82 fix: add WithTempFile
Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
2023-07-30 16:04:37 +02:00
Carsten Leue
a12936a86c Merge pull request #14 from IBM/cleue-add-missing-methods
fix: add missing array traversal to either
2023-07-28 22:51:34 +02:00
Dr. Carsten Leue
94bcfde0d3 fix: add missing array traversal to either
Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
2023-07-28 22:50:52 +02:00
31 changed files with 268 additions and 32 deletions

View File

@@ -280,3 +280,11 @@ func IsNonNil[A any](as []A) bool {
func ConstNil[A any]() []A {
return array.ConstNil[[]A]()
}
func SliceRight[A any](start int) func([]A) []A {
return G.SliceRight[[]A](start)
}
func Copy[A any](b []A) []A {
return G.Copy(b)
}

View File

@@ -180,3 +180,21 @@ func MatchLeft[AS ~[]A, A, B any](onEmpty func() B, onNonEmpty func(A, AS) B) fu
return onNonEmpty(as[0], as[1:])
}
}
func Slice[AS ~[]A, A any](start int, end int) func(AS) AS {
return func(a AS) AS {
return a[start:end]
}
}
func SliceRight[AS ~[]A, A any](start int) func(AS) AS {
return func(a AS) AS {
return a[start:]
}
}
func Copy[AS ~[]A, A any](b AS) AS {
buf := make(AS, len(b))
copy(buf, b)
return buf
}

View File

@@ -17,7 +17,7 @@ package readerioeither
// Code generated by go generate; DO NOT EDIT.
// This file was generated by robots at
// 2023-07-28 15:42:22.8252325 +0200 CEST m=+0.010212601
// 2023-07-28 22:48:20.012425 +0200 CEST m=+0.019517901
import (
"context"

View File

@@ -17,7 +17,7 @@ package generic
// Code generated by go generate; DO NOT EDIT.
// This file was generated by robots at
// 2023-07-28 15:42:22.8252325 +0200 CEST m=+0.010212601
// 2023-07-28 22:48:20.012425 +0200 CEST m=+0.019517901
import (
"context"

46
either/array.go Normal file
View File

@@ -0,0 +1,46 @@
// 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 either
import (
F "github.com/IBM/fp-go/function"
RA "github.com/IBM/fp-go/internal/array"
)
// TraverseArray transforms an array
func TraverseArrayG[GA ~[]A, GB ~[]B, E, A, B any](f func(A) Either[E, B]) func(GA) Either[E, GB] {
return RA.Traverse[GA](
Of[E, GB],
Map[E, GB, func(B) GB],
Ap[GB, E, B],
f,
)
}
// TraverseArray transforms an array
func TraverseArray[E, A, B any](f func(A) Either[E, B]) func([]A) Either[E, []B] {
return TraverseArrayG[[]A, []B](f)
}
func SequenceArrayG[GA ~[]A, GOA ~[]Either[E, A], E, A any](ma GOA) Either[E, GA] {
return TraverseArrayG[GOA, GA](F.Identity[Either[E, A]])(ma)
}
// SequenceArray converts a homogeneous sequence of either into an either of sequence
func SequenceArray[E, A any](ma []Either[E, A]) Either[E, []A] {
return SequenceArrayG[[]A](ma)
}

View File

@@ -15,7 +15,7 @@
// Code generated by go generate; DO NOT EDIT.
// This file was generated by robots at
// 2023-07-28 15:42:24.2984201 +0200 CEST m=+0.008649601
// 2023-07-28 22:48:23.165015 +0200 CEST m=+0.038998801
package either

23
file/getters.go Normal file
View File

@@ -0,0 +1,23 @@
// 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"
// GetName is the getter for the `Name` property of [os.File]
func GetName(f *os.File) string {
return f.Name()
}

View File

@@ -15,7 +15,7 @@
// Code generated by go generate; DO NOT EDIT.
// This file was generated by robots at
// 2023-07-28 15:42:51.4021911 +0200 CEST m=+0.009058101
// 2023-07-28 22:48:40.161663 +0200 CEST m=+0.009458101
package function
// Combinations for a total of 1 arguments

View File

@@ -15,7 +15,7 @@
// Code generated by go generate; DO NOT EDIT.
// This file was generated by robots at
// 2023-07-28 15:42:43.1490717 +0200 CEST m=+0.010222401
// 2023-07-28 22:48:27.8029852 +0200 CEST m=+0.051432001
package function
// Pipe0 takes an initial value t0 and successively applies 0 functions where the input of a function is the return value of the previous function

View File

@@ -15,7 +15,7 @@
// Code generated by go generate; DO NOT EDIT.
// This file was generated by robots at
// 2023-07-28 15:42:58.8116745 +0200 CEST m=+0.008252901
// 2023-07-28 22:48:46.7920356 +0200 CEST m=+0.045177201
package identity

View File

@@ -15,7 +15,7 @@
// Code generated by go generate; DO NOT EDIT.
// This file was generated by robots at
// 2023-07-28 15:43:00.6284756 +0200 CEST m=+0.013494201
// 2023-07-28 22:48:49.4210571 +0200 CEST m=+0.040316501
package apply

39
io/file/file.go Normal file
View File

@@ -0,0 +1,39 @@
// 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"
"os"
IO "github.com/IBM/fp-go/io"
)
// Close closes a closeable resource and ignores a potential error
func Close[R io.Closer](r R) IO.IO[R] {
return IO.MakeIO[R](func() R {
r.Close() // #nosec: G104
return r
})
}
// Remove removes a resource and ignores a potential error
func Remove(name string) IO.IO[string] {
return IO.MakeIO[string](func() string {
os.Remove(name) // #nosec: G104
return name
})
}

View File

@@ -15,7 +15,7 @@
// Code generated by go generate; DO NOT EDIT.
// This file was generated by robots at
// 2023-07-28 15:43:07.1016885 +0200 CEST m=+0.039744901
// 2023-07-28 22:48:57.7378739 +0200 CEST m=+0.063371401
package io

View File

@@ -15,7 +15,7 @@
// Code generated by go generate; DO NOT EDIT.
// This file was generated by robots at
// 2023-07-28 15:43:07.1026882 +0200 CEST m=+0.040744601
// 2023-07-28 22:48:57.7499229 +0200 CEST m=+0.075420401
package generic

View File

@@ -21,6 +21,7 @@ import (
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()

View File

@@ -24,14 +24,24 @@ import (
var (
// Open opens a file for reading
Open = IOE.Eitherize1(os.Open)
// Create opens a file for writing
Create = IOE.Eitherize1(os.Create)
// ReadFile reads the context of a file
ReadFile = IOE.Eitherize1(os.ReadFile)
// WriteFile writes a data blob to a file
WriteFile = func(dstName string, perm os.FileMode) func([]byte) IOE.IOEither[error, []byte] {
return func(data []byte) IOE.IOEither[error, []byte] {
return IOE.TryCatchError(func() ([]byte, error) {
return data, os.WriteFile(dstName, data, perm)
})
}
}
)
// WriteFile writes a data blob to a file
func WriteFile(dstName string, perm os.FileMode) func([]byte) IOE.IOEither[error, []byte] {
return func(data []byte) IOE.IOEither[error, []byte] {
return IOE.TryCatchError(func() ([]byte, error) {
return data, os.WriteFile(dstName, data, perm)
})
}
}
// Remove removes a file by name
func Remove(name string) IOE.IOEither[error, string] {
return IOE.TryCatchError(func() (string, error) {
return name, os.Remove(name)
})
}

View File

@@ -29,7 +29,7 @@ func onReadAll[R io.Reader](r R) IOE.IOEither[error, []byte] {
// ReadAll uses a generator function to create a stream, reads it and closes it
func ReadAll[R io.ReadCloser](acquire IOE.IOEither[error, R]) IOE.IOEither[error, []byte] {
return IOE.WithResource[error, R, []byte](
return IOE.WithResource[[]byte](
acquire,
onClose[R])(
onReadAll[R],

45
ioeither/file/tempfile.go Normal file
View File

@@ -0,0 +1,45 @@
// 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"
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"
IOE "github.com/IBM/fp-go/ioeither"
)
var (
// CreateTemp created a temp file with proper parametrization
CreateTemp = IOE.Eitherize2(os.CreateTemp)
// onCreateTempFile creates a temp file with sensible defaults
onCreateTempFile = CreateTemp("", "*")
// destroy handler
onReleaseTempFile = F.Flow4(
IOF.Close[*os.File],
IO.Map(FL.GetName),
IOE.FromIO[error, string],
IOE.Chain(Remove),
)
)
// 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) IOE.IOEither[error, A]) IOE.IOEither[error, A] {
return IOE.WithResource[A](onCreateTempFile, onReleaseTempFile)(f)
}

View File

@@ -0,0 +1,46 @@
// 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"
"testing"
E "github.com/IBM/fp-go/either"
F "github.com/IBM/fp-go/function"
IOE "github.com/IBM/fp-go/ioeither"
"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())
}
func TestWithTempFileOnClosedFile(t *testing.T) {
res := WithTempFile(func(f *os.File) IOE.IOEither[error, []byte] {
return F.Pipe2(
f,
onWriteAll[*os.File]([]byte("Carsten")),
IOE.ChainFirst(F.Constant1[[]byte](onClose(f))),
)
})
assert.Equal(t, E.Of[error]([]byte("Carsten")), res())
}

View File

@@ -34,7 +34,7 @@ func onWriteAll[W io.Writer](data []byte) func(w W) IOE.IOEither[error, []byte]
func WriteAll[W io.WriteCloser](data []byte) func(acquire IOE.IOEither[error, W]) IOE.IOEither[error, []byte] {
onWrite := onWriteAll[W](data)
return func(onCreate IOE.IOEither[error, W]) IOE.IOEither[error, []byte] {
return IOE.WithResource[error, W, []byte](
return IOE.WithResource[[]byte](
onCreate,
onClose[W])(
onWrite,
@@ -43,8 +43,8 @@ func WriteAll[W io.WriteCloser](data []byte) func(acquire IOE.IOEither[error, W]
}
// Write uses a generator function to create a stream, writes data to it and closes it
func Write[W io.WriteCloser, R any](acquire IOE.IOEither[error, W]) func(use func(W) IOE.IOEither[error, R]) IOE.IOEither[error, R] {
return IOE.WithResource[error, W, R](
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])
}

View File

@@ -15,7 +15,7 @@
// Code generated by go generate; DO NOT EDIT.
// This file was generated by robots at
// 2023-07-28 15:43:09.9125541 +0200 CEST m=+0.083065801
// 2023-07-28 22:49:00.7618943 +0200 CEST m=+0.065441701
package ioeither

View File

@@ -15,7 +15,7 @@
// Code generated by go generate; DO NOT EDIT.
// This file was generated by robots at
// 2023-07-28 15:43:09.9180997 +0200 CEST m=+0.088611401
// 2023-07-28 22:49:00.7765028 +0200 CEST m=+0.080050201
package generic

View File

@@ -192,7 +192,7 @@ func ChainFirstIOK[E, A, B any](f func(A) I.IO[B]) func(IOEither[E, A]) IOEither
}
// WithResource constructs a function that creates a resource, then operates on it and then releases the resource
func WithResource[E, R, A, ANY any](onCreate IOEither[E, R], onRelease func(R) IOEither[E, ANY]) func(func(R) IOEither[E, A]) IOEither[E, A] {
func WithResource[A, E, R, ANY any](onCreate IOEither[E, R], onRelease func(R) IOEither[E, ANY]) func(func(R) IOEither[E, A]) IOEither[E, A] {
return G.WithResource[IOEither[E, A]](onCreate, onRelease)
}

View File

@@ -15,7 +15,7 @@
// Code generated by go generate; DO NOT EDIT.
// This file was generated by robots at
// 2023-07-28 15:43:14.520059 +0200 CEST m=+0.012057201
// 2023-07-28 22:49:03.8471425 +0200 CEST m=+0.087124201
package iooption

View File

@@ -15,7 +15,7 @@
// Code generated by go generate; DO NOT EDIT.
// This file was generated by robots at
// 2023-07-28 15:43:14.5210603 +0200 CEST m=+0.013058501
// 2023-07-28 22:49:03.8541587 +0200 CEST m=+0.094140401
package generic

View File

@@ -15,7 +15,7 @@
// Code generated by go generate; DO NOT EDIT.
// This file was generated by robots at
// 2023-07-28 15:43:17.2891664 +0200 CEST m=+0.022524801
// 2023-07-28 22:49:06.6236747 +0200 CEST m=+0.039283301
package option

View File

@@ -15,7 +15,7 @@
// Code generated by go generate; DO NOT EDIT.
// This file was generated by robots at
// 2023-07-28 15:43:24.9416992 +0200 CEST m=+0.030259001
// 2023-07-28 22:49:14.4173227 +0200 CEST m=+0.013163201
package reader

View File

@@ -15,7 +15,7 @@
// Code generated by go generate; DO NOT EDIT.
// This file was generated by robots at
// 2023-07-28 15:43:24.9574807 +0200 CEST m=+0.046040501
// 2023-07-28 22:49:14.4173227 +0200 CEST m=+0.013163201
package generic

View File

@@ -15,7 +15,7 @@
// Code generated by go generate; DO NOT EDIT.
// This file was generated by robots at
// 2023-07-28 15:43:27.7019865 +0200 CEST m=+0.066240401
// 2023-07-28 22:49:16.5272334 +0200 CEST m=+0.043108701
package readerioeither

View File

@@ -15,7 +15,7 @@
// Code generated by go generate; DO NOT EDIT.
// This file was generated by robots at
// 2023-07-28 15:43:27.7039855 +0200 CEST m=+0.068239401
// 2023-07-28 22:49:16.5292332 +0200 CEST m=+0.045108501
package generic

View File

@@ -15,7 +15,7 @@
// Code generated by go generate; DO NOT EDIT.
// This file was generated by robots at
// 2023-07-28 15:43:31.1018593 +0200 CEST m=+0.128090501
// 2023-07-28 22:49:18.7141124 +0200 CEST m=+0.109464201
package tuple