Module shell (3)

This commit is contained in:
Benjamin VAUDOUR 2023-10-07 21:13:39 +02:00
parent 8e5ad41b11
commit 75f0c1350b
13 changed files with 2644 additions and 0 deletions

46
option/choice.go Normal file
View file

@ -0,0 +1,46 @@
package option
// Option permet de définir des valeurs alternative.
type Choice[T1 any, T2 any] struct {
left Option[T1]
right Option[T2]
}
// Left retourne un choix avec la valeur à gauche.
func Left[T1 any, T2 any](v T1) (c Choice[T1, T2]) {
c.left = Some(v)
return
}
// Right retourne un choix avec la valeur à droite.
func Right[T1 any, T2 any](v T2) (c Choice[T1, T2]) {
c.right = Some(v)
return
}
// Left retourne la valeur à gauche, si elle existe.
func (c Choice[T1, T2]) Left() (T1, bool) {
return c.left.Get()
}
// Right retourne la valeur à droite, si elle existe.
func (c Choice[T1, T2]) Right() (T2, bool) {
return c.right.Get()
}
// IsLeft retourne vrai si la valeur à gauche est définie.
func (c Choice[T1, T2]) IsLeft() bool {
return c.left.IsDefined()
}
// IsRight retourne vrai si la valeur à droit est définie.
func (c Choice[T1, T2]) IsRight() bool {
return c.right.IsDefined()
}
// IsNil retourne vrai si aucune valeur nest définie.
func (c Choice[T1, T2]) IsNil() bool {
return !c.IsLeft() && !c.IsRight()
}