rollup

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 1, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package rollup is the holder for package bom. It turns a named bill of materials into the call the machine is, and its loop is the trampoline that evaluates the call until it returns. The walk, the stack and the numbers are the machine's; nothing here recurses.

Example

A bicycle: the frame and seat are bought in, each wheel is built from a rim, a tyre, 32 spokes and a hub, and each hub holds two bearings. Values are in cents, and an assembly's value is the labour to put it together.

package main

import (
	"fmt"

	"code.eitanity.com/merilo/examples/bom/rollup"
)

func main() {
	const (
		bicycle = iota
		frame
		seat
		wheel
		rim
		tyre
		spoke
		hub
		bearing
	)
	bill := rollup.Bill{Top: bicycle, Parts: []rollup.Part{
		bicycle: {Name: "bicycle", Value: 4000, Lines: []rollup.Line{{Part: frame, Quantity: 1}, {Part: seat, Quantity: 1}, {Part: wheel, Quantity: 2}}},
		frame:   {Name: "frame", Value: 12000},
		seat:    {Name: "seat", Value: 2500},
		wheel:   {Name: "wheel", Value: 1500, Lines: []rollup.Line{{Part: rim, Quantity: 1}, {Part: tyre, Quantity: 1}, {Part: spoke, Quantity: 32}, {Part: hub, Quantity: 1}}},
		rim:     {Name: "rim", Value: 3000},
		tyre:    {Name: "tyre", Value: 1800},
		spoke:   {Name: "spoke", Value: 40},
		hub:     {Name: "hub", Value: 600, Lines: []rollup.Line{{Part: bearing, Quantity: 2}}},
		bearing: {Name: "bearing", Value: 350},
	}}
	r, err := rollup.Run(bill, 1000)
	if err != nil {
		fmt.Println(err)
		return
	}
	for i, p := range bill.Parts {
		fmt.Printf("%-8s need %3v  subtotal %6v\n", p.Name, r.Need[i], r.Subtotal[i])
	}
	fmt.Println("total", r.Total)
}
Output:
bicycle  need   1  subtotal  36260
frame    need   1  subtotal  12000
seat     need   1  subtotal   2500
wheel    need   2  subtotal   8880
rim      need   2  subtotal   3000
tyre     need   2  subtotal   1800
spoke    need  64  subtotal     40
hub      need   2  subtotal   1300
bearing  need   4  subtotal    350
total 36260

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bill

type Bill struct {
	Parts []Part
	Top   int // the system, an index into Parts
}

Bill is a bill of materials: every part, and the one the system is.

type Line

type Line struct {
	Part     int   // the part, an index into Bill.Parts
	Quantity int64 // how many, at least one
}

Line is a quantity of one part in another.

type Part

type Part struct {
	Name  string
	Value int64 // at least zero
	Lines []Line
}

Part is a part or an assembly. Its value is its own cost, such as the labour to assemble it, in the smallest unit of currency.

type Refused

type Refused struct{ State bom.State }

Refused is the error for a walk the machine stopped.

func (Refused) Error

func (r Refused) Error() string

type Result

type Result struct {
	// Subtotal is, for each part, the cost of one: its value plus each
	// line's quantity times that part's subtotal. It is zero for a part
	// the system does not use.
	Subtotal []int64
	// Need is, for each part, how many one system takes in all: the
	// quantities multiplied down every path to it and summed. The system
	// itself needs one.
	Need []int64
	// Total is the cost of one system, its subtotal.
	Total int64
}

Result is a finished rollup.

func Run

func Run(b Bill, calls int64) (Result, error)

Run rolls up b, making at most calls tail calls.

type Walker

type Walker struct {
	// contains filtered or unexported fields
}

Walker is one rollup in progress: the call, the bill's size and its top part.

func New

func New(b Bill, calls int64) (*Walker, error)

New returns the call on b, or an error when b or calls is out of range.

func (*Walker) Result

func (w *Walker) Result() (Result, error)

Result returns the rollup once the walk has returned.

func (*Walker) Step

func (w *Walker) Step() error

Step evaluates the call once. Once the walk has returned, it does nothing; a refused walk is an error.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL