← Library Predicate Deterministic

IfEmptySliceFloat64Op

Returns true if the float64 slice is nil or has zero elements.

Inputs

FieldTypeDescription
Value*[]float64Slice to test

Outputs

FieldTypeDescription
MatchboolTrue if nil or zero-length

Typical Use Cases

  • Guard before SumOp, MinOp, or MaxOp
  • Fallback when scoring produced no results
  • Detecting empty parallel map output

Complete Runnable Example

Guards a downstream aggregation step by detecting when a scoring step returned no results.

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() {
	scores := []float64{}
	library.RegisterConst("scores", scores)

	g, err := graph.NewBuilder("empty_slice_float_demo").
		Vertex("src").Op("scores").Output("Result", "scores_wire").
		Vertex("check").Op("IfEmptySliceFloat64Op").
		Input("Value", "scores_wire").
		Output("Match", "is_empty").
		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{"scores": scores}
	if raw, ok := eng.GetOutput("is_empty"); ok {
		out["is_empty"] = *raw.(*bool)
	}
	enc := json.NewEncoder(os.Stdout)
	enc.SetIndent("", "  ")
	enc.Encode(out)
}