All Projects → alexpantyukhin → go-pattern-match

alexpantyukhin / go-pattern-match

Licence: MIT license
Pattern matchings for Go.

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to go-pattern-match

jackliu-golang-notes
Jack Liu's Golang personal summary main points notes, notes to fast understand golang
Stars: ✭ 17 (-90.66%)
Mutual labels:  awesome-go, awesome-golang
CVparser
CVparser is software for parsing or extracting data out of CV/resumes.
Stars: ✭ 28 (-84.62%)
Mutual labels:  pattern-matching
grime
A language for matching two-dimensional patterns, based on Boolean grammars.
Stars: ✭ 13 (-92.86%)
Mutual labels:  pattern-matching
egison-haskell
Template Haskell Implementation of Egison Pattern Matching
Stars: ✭ 31 (-82.97%)
Mutual labels:  pattern-matching
regexm
A Rust macro for writing regex pattern matching.
Stars: ✭ 46 (-74.73%)
Mutual labels:  pattern-matching
pmatch
Pattern matching DSL for R
Stars: ✭ 21 (-88.46%)
Mutual labels:  pattern-matching
extractacy
Spacy pipeline object for extracting values that correspond to a named entity (e.g., birth dates, account numbers, laboratory results)
Stars: ✭ 47 (-74.18%)
Mutual labels:  pattern-matching
squire
The medieval language held together by twine.
Stars: ✭ 42 (-76.92%)
Mutual labels:  pattern-matching
wink-nlp
Developer friendly Natural Language Processing ✨
Stars: ✭ 312 (+71.43%)
Mutual labels:  pattern-matching
sweet-egison
Haskell library for non-deterministic pattern matching
Stars: ✭ 15 (-91.76%)
Mutual labels:  pattern-matching
awesome-backend
Curadoria de conteúdos relacionados à backend.
Stars: ✭ 158 (-13.19%)
Mutual labels:  awesome-go
skywalker
A package to allow one to concurrently go through a filesystem with ease
Stars: ✭ 87 (-52.2%)
Mutual labels:  awesome-go
simplematch
Minimal, super readable string pattern matching for python.
Stars: ✭ 147 (-19.23%)
Mutual labels:  pattern-matching
chemin
🥾 A type-safe pattern builder & route matching library written in TypeScript
Stars: ✭ 37 (-79.67%)
Mutual labels:  pattern-matching
froggit-go
Froggit-Go is a universal Go library, allowing to perform actions on VCS providers.
Stars: ✭ 19 (-89.56%)
Mutual labels:  awesome-go
when-switch
JavaScript functional implementation of switch/case
Stars: ✭ 20 (-89.01%)
Mutual labels:  pattern-matching
montre
The original timed regular expression matcher over temporal behaviors
Stars: ✭ 14 (-92.31%)
Mutual labels:  pattern-matching
awesome-latam
Colección de contenidos y recursos en Español para desarrolladores de Golang. Awesome oficial: https://awesome-go.com
Stars: ✭ 81 (-55.49%)
Mutual labels:  awesome-go
suitcase
Java Pattern Matching library
Stars: ✭ 21 (-88.46%)
Mutual labels:  pattern-matching
pattern-matching
full-featured pattern-matching in python, however it's more likely to be dynamic contracts.
Stars: ✭ 18 (-90.11%)
Mutual labels:  pattern-matching

Go pattern matching

Mentioned in Awesome Go Go Report Card Build Status codecov GoDoc LICENSE

It's just another implementation of pattern matching in Go. I have been inspired by Python pattern matching, that's why I wanted to try writing something similar in Go :) For now the following matching are implemented :

  • Simple types (like int, int64, float, float64, bool..).
  • Struct type.
  • Slices (with HEAD, TAIL, OneOf patterns).
  • Dictionary (with ANY, OneOf pattern).
  • Regexp.
  • Additional custom matching (ability to add special matching for some, structs for example).

Usages

Fibonacci example:

func fib(n int) int {
	_, res := match.Match(n).
		When(1, 1).
		When(2, 1).
		When(match.ANY, func() int { return fib(n-1) + fib(n-2) }).
		Result()

	return res.(int)
}

Simple types:

isMatched, mr := match.Match(42).
                When(42, 10).
                Result()
// isMatched - true, mr - 10

With Structs:

  • Simple check value by type
val := TestStruct{1}

isMatched, _ := Match(val).
    When(func(TestStruct) {},  1).
    Result()
  • Check value by type and condition
val := TestStruct{1}

isMatched, _ := Match(val).
	When(func(ts TestStruct) bool { return ts.value == 42 }, 1).
	When(func(ts AnotherStruct) bool { return ts.stringValue == "hello" }, 2).
	Result()

With Maps:

isMatched, mr := match.Match(map[string]int{
                	"rsc": 3711,
                	"r":   2138,
                	"gri": 1908,
                	"adg": 912,
                }).
        	    When(map[string]interface{}{
                	"rsc": 3711,
                	"r":   2138,
                	"gri": 1908,
                	"adg": match.ANY,
            	}, true).
            	Result()

With Slices:

isMatched, mr := match.Match([]int{1, 2, 3, 4, 5, 6}).
            	When([]interface{}{match.HEAD, 3, match.OneOf(3, 4), 5, 6}, 125).
            	Result()

With regexps:

isMatched, mr := match.Match("gophergopher").
            	When("gophergopher", func() interface{} { return true }).
            	Result()

Without result:

func main() {
	Match(val).
	When(42, func() { fmt.Println("You found the answer to life, universe and everything!") }).
	When(ANY, func() { fmt.Println("No.. It's not an answer.") }).
	Result()
}

Installation

Just go get this repository in the following way:

go get github.com/alexpantyukhin/go-pattern-match

Full example

package main

import (
    "fmt"
    "github.com/alexpantyukhin/go-pattern-match"
)

func main() {
    isMatched, mr := match.Match([]int{1, 2, 3}).
        When(42, false).
        When([]interface{}{match.HEAD, 2, 3}, true).
        Result()


    if isMatched {
        fmt.Println(mr)
    }
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].