Coder Social home page Coder Social logo

match's Introduction

Match

Match provides functions for completing strings based on comparison functions. A practical use for this package is completing command line arguments from substrings.

Usage

To start matching we need a haystack and needle. A haystack is a search area. A needle is a search key. In this package, both of these things are strings. For most of the examples below, we use this haystack:

	products := []string{
		"Apple",
		"Eggplant",
		"Pear",
		"Peach",
	}

For most matching situations, use match.Best()

	needles := []string{
		"App",
		"eg",
		"Peac",
	}

	// Simplified example: See next part for error checking
	matches, _ := match.Best(products, needles...)

	for _, v := range needles {
		fmt.Println(v, " -> ", matches[v])
	}
	// Prints:
	// App  ->  Apple
	// eg  ->  Eggplant
	// Peac  ->  Peach

Best Practices - Error checking

An error occurs if the needle isn't found, or an ambiguous needle is used

	needles := []string {
		"Egg",
		"Pea", // Ambiguous: matches Pear and Peach
	}

	matches, err := match.Best(products, needles...)
	if err != nil {
		// Expecting error
		fmt.Println(err)
		// Prints:
		// Pea matches 2 fields
	}

Pea matches 2 fields. So How do we enumerate them?

	// Match has its own Error type, and it contains diagnostic
	// information. Use a type assertion to access it.

	if e, ok := err.(*match.Error); ok && e.MultiMatch() {
		for _, v := range e.Matches {
			fmt.Println("\t", v)
		}
		// Prints:
		// Pear
		// Peach
	}

Best Behavior

An exact match removes ambiguity. The code below will always match "Apple" unambiguously.

	haystack := []string{ "Rock", "Apple", "Apples" }
	needle = "Apple"
	matches, _ := matches.Best(haystack, needle)

Primitive Functions

The basic functions used by Best are exposed in the match package. Use them for raw matching functionality. An example:

	needle := "tokyo"
	haystack := []string{ "new york", "murray hill", "kyoto", "bejing", "oykot" }
	
	matches := match.Needle(haystack, cmpAna, needle)

	fmt.Printf("There are %d anagram matches for %s:\n", len(matches), needle)
	for _, v := range matches {
		fmt.Println(v)
	}

	// ... boilerplate omitted

	// cmpAna returns true if a and b are anagrams
	func cmpAna(a, b string) bool {
		return sum(a) == sum(b)
	}

Full example

See example/example.go

match's People

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.