2023-07-23 22:05:54 +02:00
|
|
|
// 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.
|
|
|
|
|
2023-07-08 11:21:26 +02:00
|
|
|
package either
|
|
|
|
|
2023-07-14 13:20:00 +02:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
)
|
2023-07-08 11:21:26 +02:00
|
|
|
|
2023-07-09 10:21:56 +02:00
|
|
|
type (
|
2023-07-14 13:20:00 +02:00
|
|
|
// Either defines a data structure that logically holds either an E or an A. The flag discriminates the cases
|
2023-07-09 10:21:56 +02:00
|
|
|
Either[E, A any] struct {
|
|
|
|
isLeft bool
|
|
|
|
left E
|
|
|
|
right A
|
|
|
|
}
|
2023-07-08 11:21:26 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// String prints some debug info for the object
|
|
|
|
func (s Either[E, A]) String() string {
|
2023-07-09 10:21:56 +02:00
|
|
|
if s.isLeft {
|
|
|
|
return fmt.Sprintf("Left[%T, %T](%v)", s.left, s.right, s.left)
|
2023-07-08 11:21:26 +02:00
|
|
|
}
|
2023-07-09 10:21:56 +02:00
|
|
|
return fmt.Sprintf("Right[%T, %T](%v)", s.left, s.right, s.right)
|
2023-07-08 11:21:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Format prints some debug info for the object
|
|
|
|
func (s Either[E, A]) Format(f fmt.State, c rune) {
|
|
|
|
switch c {
|
|
|
|
case 's':
|
|
|
|
fmt.Fprint(f, s.String())
|
|
|
|
default:
|
|
|
|
fmt.Fprint(f, s.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-14 13:20:00 +02:00
|
|
|
// IsLeft tests if the either is a left value. Rather use [Fold] if you need to access the values. Inverse is [IsRight].
|
2023-07-08 11:21:26 +02:00
|
|
|
func IsLeft[E, A any](val Either[E, A]) bool {
|
2023-07-09 10:21:56 +02:00
|
|
|
return val.isLeft
|
2023-07-08 11:21:26 +02:00
|
|
|
}
|
|
|
|
|
2023-07-14 13:20:00 +02:00
|
|
|
// IsLeft tests if the either is a right value. Rather use [Fold] if you need to access the values. Inverse is [IsLeft].
|
2023-07-08 11:21:26 +02:00
|
|
|
func IsRight[E, A any](val Either[E, A]) bool {
|
2023-07-09 10:21:56 +02:00
|
|
|
return !val.isLeft
|
2023-07-08 11:21:26 +02:00
|
|
|
}
|
|
|
|
|
2023-07-14 13:20:00 +02:00
|
|
|
// Left creates a new instance of an [Either] representing the left value.
|
|
|
|
func Left[A, E any](value E) Either[E, A] {
|
2023-07-09 10:21:56 +02:00
|
|
|
return Either[E, A]{isLeft: true, left: value}
|
2023-07-08 11:21:26 +02:00
|
|
|
}
|
|
|
|
|
2023-07-14 13:20:00 +02:00
|
|
|
// Right creates a new instance of an [Either] representing the right value.
|
2023-07-08 11:21:26 +02:00
|
|
|
func Right[E, A any](value A) Either[E, A] {
|
2023-07-09 10:21:56 +02:00
|
|
|
return Either[E, A]{isLeft: false, right: value}
|
2023-07-08 11:21:26 +02:00
|
|
|
}
|
|
|
|
|
2023-07-14 13:20:00 +02:00
|
|
|
// MonadFold extracts the values from an [Either] by invoking the [onLeft] callback or the [onRight] callback depending on the case
|
2023-07-08 11:21:26 +02:00
|
|
|
func MonadFold[E, A, B any](ma Either[E, A], onLeft func(e E) B, onRight func(a A) B) B {
|
2023-07-14 13:20:00 +02:00
|
|
|
if ma.isLeft {
|
2023-07-08 11:21:26 +02:00
|
|
|
return onLeft(ma.left)
|
|
|
|
}
|
|
|
|
return onRight(ma.right)
|
|
|
|
}
|
|
|
|
|
2023-07-14 13:20:00 +02:00
|
|
|
// Unwrap converts an [Either] into the idiomatic tuple
|
2023-07-08 11:21:26 +02:00
|
|
|
func Unwrap[E, A any](ma Either[E, A]) (A, E) {
|
|
|
|
return ma.right, ma.left
|
|
|
|
}
|