-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Dr. Carsten Leue <[email protected]>
- Loading branch information
1 parent
8c58e8d
commit 1713de0
Showing
51 changed files
with
2,076 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// 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 bounded | ||
|
||
import ( | ||
O "github.com/IBM/fp-go/ord" | ||
) | ||
|
||
type Bounded[T any] interface { | ||
O.Ord[T] | ||
Top() T | ||
Bottom() T | ||
} | ||
|
||
type bounded[T any] struct { | ||
c func(x, y T) int | ||
e func(x, y T) bool | ||
t T | ||
b T | ||
} | ||
|
||
func (self bounded[T]) Equals(x, y T) bool { | ||
return self.e(x, y) | ||
} | ||
|
||
func (self bounded[T]) Compare(x, y T) int { | ||
return self.c(x, y) | ||
} | ||
|
||
func (self bounded[T]) Top() T { | ||
return self.t | ||
} | ||
|
||
func (self bounded[T]) Bottom() T { | ||
return self.b | ||
} | ||
|
||
// MakeBounded creates an instance of a bounded type | ||
func MakeBounded[T any](o O.Ord[T], t, b T) Bounded[T] { | ||
return bounded[T]{c: o.Compare, e: o.Equals, t: t, b: b} | ||
} | ||
|
||
// Clamp returns a function that clamps against the bounds defined in the bounded type | ||
func Clamp[T any](b Bounded[T]) func(T) T { | ||
return O.Clamp[T](b)(b.Bottom(), b.Top()) | ||
} | ||
|
||
// Reverse reverses the ordering and swaps the bounds | ||
func Reverse[T any](b Bounded[T]) Bounded[T] { | ||
return MakeBounded(O.Reverse[T](b), b.Bottom(), b.Top()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,3 +18,7 @@ package bytes | |
func ToString(a []byte) string { | ||
return string(a) | ||
} | ||
|
||
func Size(as []byte) int { | ||
return len(as) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Carsten |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// 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 readerioeither | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"os" | ||
|
||
B "github.com/IBM/fp-go/bytes" | ||
F "github.com/IBM/fp-go/function" | ||
IO "github.com/IBM/fp-go/io" | ||
IOE "github.com/IBM/fp-go/ioeither" | ||
) | ||
|
||
var ( | ||
openFile = F.Flow3( | ||
IOE.Eitherize1(os.Open), | ||
FromIOEither[*os.File], | ||
ChainFirstIOK(F.Flow2( | ||
(*os.File).Name, | ||
IO.Logf[string]("Opened file [%s]"), | ||
)), | ||
) | ||
) | ||
|
||
func closeFile(f *os.File) ReaderIOEither[string] { | ||
return F.Pipe1( | ||
TryCatch(func(_ context.Context) func() (string, error) { | ||
return func() (string, error) { | ||
return f.Name(), f.Close() | ||
} | ||
}), | ||
ChainFirstIOK(IO.Logf[string]("Closed file [%s]")), | ||
) | ||
} | ||
|
||
func ExampleWithResource() { | ||
|
||
stringReader := WithResource[string](openFile("data/file.txt"), closeFile) | ||
|
||
rdr := stringReader(func(f *os.File) ReaderIOEither[string] { | ||
return F.Pipe2( | ||
TryCatch(func(_ context.Context) func() ([]byte, error) { | ||
return func() ([]byte, error) { | ||
return io.ReadAll(f) | ||
} | ||
}), | ||
ChainFirstIOK(F.Flow2( | ||
B.Size, | ||
IO.Logf[int]("Read content of length [%d]"), | ||
)), | ||
Map(B.ToString), | ||
) | ||
}) | ||
|
||
contentIOE := F.Pipe2( | ||
context.Background(), | ||
rdr, | ||
IOE.ChainFirstIOK[error](IO.Printf[string]("Content: %s")), | ||
) | ||
|
||
contentIOE() | ||
|
||
// Output: Content: Carsten | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// 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 erasure | ||
|
||
import ( | ||
F "github.com/IBM/fp-go/function" | ||
) | ||
|
||
// Erase converts a variable of type T to an any by returning a pointer to that variable | ||
func Erase[T any](t T) any { | ||
return &t | ||
} | ||
|
||
// Unerase converts an erased variable back to its original value | ||
func Unerase[T any](t any) T { | ||
return *t.(*T) | ||
} | ||
|
||
// Erase0 converts a type safe function into an erased function | ||
func Erase0[T1 any](f func() T1) func() any { | ||
return F.Nullary2(f, Erase[T1]) | ||
} | ||
|
||
// Erase1 converts a type safe function into an erased function | ||
func Erase1[T1, T2 any](f func(T1) T2) func(any) any { | ||
return F.Flow3( | ||
Unerase[T1], | ||
f, | ||
Erase[T2], | ||
) | ||
} | ||
|
||
// Erase2 converts a type safe function into an erased function | ||
func Erase2[T1, T2, T3 any](f func(T1, T2) T3) func(any, any) any { | ||
return func(t1, t2 any) any { | ||
return Erase(f(Unerase[T1](t1), Unerase[T2](t2))) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 erasure | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
E "github.com/IBM/fp-go/either" | ||
F "github.com/IBM/fp-go/function" | ||
) | ||
|
||
func TestEither(t *testing.T) { | ||
|
||
e1 := F.Pipe3( | ||
E.Of[error](Erase("Carsten")), | ||
E.Map[error](Erase1(strings.ToUpper)), | ||
E.GetOrElse(func(e error) any { | ||
return Erase("Error") | ||
}), | ||
Unerase[string], | ||
) | ||
|
||
fmt.Println(e1) | ||
} |
Oops, something went wrong.