patternmatch

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2023 License: MIT Imports: 3 Imported by: 0

README

Go Pattern Matching

Pattern Matching for Golang inspired by Hudo's "Just another pattern matching for C#"

Examples


result, err := patternmatch.ResultMatch[int, string](70).
    When(func(x int) bool { return x > 100 }, func() string { return "> 100" }).
    When(func(x int) bool { return x > 50 }, func() string { return "> 50" }).
    When(func(x int) bool { return x > 10 }, func() string { return "> 10" }).
    Result()
            
// Result will be "> 50", nil

result, err := patternmatch.ResultMatch[int, string](2).
    When(func(x int) bool { return x > 100 }, func() string { return "> 100" }).
    When(func(x int) bool { return x > 50 }, func() string { return "> 50" }).
    When(func(x int) bool { return x > 10 }, func() string { return "> 10" }).
    Result()

// result will be "", error{"pattern not matched"}

maybeResult := patternmatch.ResultMatch[int, string](11).
    When(func(x int) bool { return x > 100 }, func() string { return "> 100" }).
    When(func(x int) bool { return x > 50 }, func() string { return "> 50" }).
    When(func(x int) bool { return x > 10 }, func() string { return "> 10" }).
    MaybeResult()

// result will be Optional[string], error{"pattern not matched"}

Using defaults:


result, err := patternmatch.ResultMatch[int, string](2).
    When(func(x int) bool { return x.(int) > 100 }, func() string { return "> 100" }).
    When(func(x int) bool { return x.(int) > 50 }, func() string { return "> 50" }).
    When(func(x int) bool { return x.(int) > 10 }, func() string { return "> 10" }).
    ResultOrDefault("< 10")
            
// Result will be "< 10", nil

Non-conditional pattern matching:

result, err := patternmatch.ResultMatch[interface{}, string](5).
    WhenValue(5, func() string { return "is int 5" }).
    WhenValue("10", func() string { return "is string 10" }).
    WhenValue(true, func() string { return "is bool" }).
    Result()

// Result will be "is int 5", nil

Pattern matching that does not return a value but executes an action:


patternmatch.Match(5).
    When(func(i int) bool { return i == 5 }, func() { fmt.Println("is 5") }).
    When(func(i int) bool { return i == 6 }, func() { fmt.Println("is 6") })

patternmatch.Match(5).
    WhenValue(5, func() { fmt.Println("is 5") }).
    WhenValue(6, func() { fmt.Println("is 6") })

patternmatch.Match(10).
    WhenValue(5, func() { fmt.Println("is 5") }).
    WhenValue(6, func() { fmt.Println("is 6") }).
    OtherwisePanic() // will panic if not matched

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Match

func Match[T any](input T) match[T]

Match condition and run an action that does not return a type

func ResultMatch

func ResultMatch[T, K any](input T) matchResult[T, K]

ResultMatch matches conditions and run a function that returns a type

Types

This section is empty.

Jump to

Keyboard shortcuts

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