← Library Select Deterministic

SelectFloat64Op

Returns IfTrue if Cond is true, otherwise IfFalse.

Inputs

FieldTypeDescription
Cond*boolCondition to test
IfTrue*float64Value when Cond is true
IfFalse*float64Value when Cond is false

Outputs

FieldTypeDescription
Resultfloat64Selected value

Typical Use Cases

  • Conditional score assignment
  • Value switching based on a predicate
  • Selecting between two numeric paths

Complete Runnable Example

Selects a discount multiplier based on whether the user qualifies for a premium discount.

main.go
package main

import (
	"context"
	"encoding/json"
	"log"
	"os"
	"time"

	"github.com/akennis/sparsi-go/library"
	_ "github.com/akennis/dagor/operator/builtin"

	"github.com/panjf2000/ants/v2"
	"github.com/akennis/dagor"
	"github.com/akennis/dagor/graph"
)

func main() {
	cond := true
	ifTrue := 1.5
	ifFalse := 0.5
	library.RegisterConst("cond_val", cond)
	library.RegisterConst("premium_mult", ifTrue)
	library.RegisterConst("standard_mult", ifFalse)

	g, err := graph.NewBuilder("selectf64_demo").
		Vertex("src_c").Op("cond_val").Output("Result", "wire_c").
		Vertex("src_t").Op("premium_mult").Output("Result", "wire_t").
		Vertex("src_f").Op("standard_mult").Output("Result", "wire_f").
		Vertex("sel").Op("SelectFloat64Op").
		Input("Cond", "wire_c").Input("IfTrue", "wire_t").Input("IfFalse", "wire_f").
		Output("Result", "selected").
		Build()
	if err != nil {
		log.Fatalf("build: %v", err)
	}

	pool, err := ants.NewPool(4)
	if err != nil {
		log.Fatalf("pool: %v", err)
	}
	defer pool.Release()

	eng, err := dagor.NewEngine(g, pool)
	if err != nil {
		log.Fatalf("engine: %v", err)
	}

	ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
	defer cancel()

	if err := eng.Run(ctx); err != nil {
		log.Fatalf("run: %v", err)
	}

	out := map[string]any{"is_premium": cond}
	if raw, ok := eng.GetOutput("selected"); ok {
		out["multiplier"] = *raw.(*float64)
	}
	enc := json.NewEncoder(os.Stdout)
	enc.SetIndent("", "  ")
	enc.Encode(out)
}