cond

package
v2.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 25, 2022 License: MIT Imports: 0 Imported by: 0

Documentation

Overview

cond gives condition control flow

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type IfCond

type IfCond[T, R any] struct {
	// contains filtered or unexported fields
}

IfCond is a if condition control flow structure.

Example
package main

import (
	"fmt"

	"github.com/snowmerak/generics-for-go/v2/syntax/cond"
)

func main() {
	a := cond.If(func(t int) bool {
		return t == 0
	}, func(t int) string {
		return "zero"
	}).ElseIf(func(i int) bool {
		return i == 1
	}, func(i int) string {
		return "one"
	}).Else(func(i int) string {
		return "other"
	})
	fmt.Println(a.Run(0))
	fmt.Println(a.Run(1))
	fmt.Println(a.Run(2))
}
Output:

zero
one
other

func If

func If[T, R any](condition func(T) bool, result func(T) R) *IfCond[T, R]

If returns a new IfCond with given condition.

func (*IfCond[T, R]) Else

func (c *IfCond[T, R]) Else(defaultElse func(T) R) *IfCond[T, R]

Else adds a default result function to the IfCond.

func (*IfCond[T, R]) ElseIf

func (c *IfCond[T, R]) ElseIf(condition func(T) bool, result func(T) R) *IfCond[T, R]

ElseIf adds a new condition to the IfCond.

func (*IfCond[T, R]) Run

func (c *IfCond[T, R]) Run(t T) (rs R)

Run runs the IfCond with given value.

type SwitchCond

type SwitchCond[T comparable, R any] struct {
	// contains filtered or unexported fields
}

SwitchCond is a switch condition control flow structure.

Example
package main

import (
	"fmt"

	"github.com/snowmerak/generics-for-go/v2/syntax/cond"
)

func main() {
	a := cond.Switch[int, string]().Case(0, func(i int) string {
		return "zero"
	}).Case(1, func(i int) string {
		return "one"
	}).Default(func(i int) string {
		return "other"
	})
	fmt.Println(a.Run(0))
	fmt.Println(a.Run(1))
	fmt.Println(a.Run(2))
}
Output:

zero
one
other
Example (Result)
package main

import (
	"fmt"

	"github.com/snowmerak/generics-for-go/v2/syntax/cond"
	"github.com/snowmerak/generics-for-go/v2/types/result"
)

func main() {
	result := result.OK(100)

	resultCond := cond.Switch[bool, int]().Case(true, func(b bool) int {
		return result.Unwrap()
	}).Default(func(b bool) int {
		return -99
	})

	fmt.Println(resultCond.Run(result.IsOK()))
}
Output:

100

func Switch

func Switch[T comparable, R any]() *SwitchCond[T, R]

Switch returns a new SwitchCond.

func (*SwitchCond[T, R]) Case

func (s *SwitchCond[T, R]) Case(value T, result func(T) R) *SwitchCond[T, R]

Case adds a new condition to the SwitchCond.

func (*SwitchCond[T, R]) Default

func (s *SwitchCond[T, R]) Default(defaultElse func(T) R) *SwitchCond[T, R]

Default adds a default result function to the SwitchCond.

func (*SwitchCond[T, R]) Run

func (s *SwitchCond[T, R]) Run(t T) (rs R)

Run runs the SwitchCond with given value.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL