Code Monkey home page Code Monkey logo

predicate's Introduction

Predicate

Predicate package used to create interpreted mini languages with Go syntax - mostly to define various predicates for configuration, e.g.

Latency() > 40 || ErrorRate() > 0.5.

Here's an example of fully functional predicate language to deal with division remainders:

package main

import (
  "log"
	
  "github.com/vulcand/predicate"
)

// takes number and returns true or false
type numberPredicate func(v int) bool

// Converts one number to another
type numberMapper func(v int) int

// Function that creates predicate to test if the remainder is 0
func divisibleBy(divisor int) numberPredicate {
     return func(v int) bool {
         return v%divisor == 0
     }
}

// Function - logical operator AND that combines predicates
func numberAND(a, b numberPredicate) numberPredicate {
    return func(v int) bool {
        return a(v) && b(v)
    }
}

func main(){
    // Create a new parser and define the supported operators and methods
    p, err := predicate.NewParser(predicate.Def{
        Operators: predicate.Operators{
           AND: numberAND,
        },
        Functions: map[string]interface{}{
            "DivisibleBy": divisibleBy,
        },
    })

    pr, err := p.Parse("DivisibleBy(2) && DivisibleBy(3)")
    if err != nil {
		log.Fatalf("Error: %v", err)
	}

	fmt.Println(pr.(numberPredicate)(2)) // false
	fmt.Println(pr.(numberPredicate)(3)) // false
	fmt.Println(pr.(numberPredicate)(6)) // true
}

predicate's People

Contributors

klizhentas avatar ldez avatar kimlisa avatar pquerna avatar hxysayhi 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.