Search Apps Documentation Source Content File Folder Download Copy Actions Download

cond_or.gno

1.19 Kb ยท 56 lines
 1package daocond
 2
 3import (
 4	"math"
 5	"strings"
 6)
 7
 8type orCond struct {
 9	conditions []Condition
10}
11
12func Or(conditions ...Condition) Condition {
13	if len(conditions) < 2 {
14		panic("at least two conditions are required")
15	}
16	return &orCond{conditions: conditions}
17}
18
19// Eval implements Condition.
20func (c *orCond) Eval(ballot Ballot) bool {
21	for _, condition := range c.conditions {
22		if condition.Eval(ballot) {
23			return true
24		}
25	}
26	return false
27}
28
29// Signal implements Condition.
30func (c *orCond) Signal(ballot Ballot) float64 {
31	maxSignal := 0.0
32	for _, condition := range c.conditions {
33		maxSignal = math.Max(maxSignal, condition.Signal(ballot))
34	}
35	return maxSignal
36}
37
38// Render implements Condition.
39func (c *orCond) Render() string {
40	renders := []string{}
41	for _, condition := range c.conditions {
42		renders = append(renders, condition.Render())
43	}
44	return "[" + strings.Join(renders, " OR ") + "]"
45}
46
47// RenderWithVotes implements Condition.
48func (c *orCond) RenderWithVotes(ballot Ballot) string {
49	renders := []string{}
50	for _, condition := range c.conditions {
51		renders = append(renders, condition.RenderWithVotes(ballot))
52	}
53	return "[" + strings.Join(renders, " OR ") + "]"
54}
55
56var _ Condition = (*orCond)(nil)