machine

package
v0.0.0-...-d322e89 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2018 License: BSD-2-Clause Imports: 0 Imported by: 0

Documentation

Overview

Example
package main

import (
	"fmt"

	"github.com/cozely/cozely/x/machine"
)

func main() {
	counter := 0

	// Define the State Machine
	var s1, s2, s3 machine.State

	s1 = func() machine.State {
		fmt.Println("State 1")
		return s2
	}

	s2 = func() machine.State {
		fmt.Println("State 2")
		return s3
	}

	s3 = func() machine.State {
		fmt.Println("State 3")
		if counter > 6 {
			return nil
		}
		return s2
	}

	// Run the State Machine
	m := s1

	for m != nil {
		counter++
		m.Update()
	}
}
Output:

State 1
State 2
State 3
State 2
State 3
State 2
State 3
Example (NoAllocations)
package main

import (
	"fmt"

	"github.com/cozely/cozely/x/machine"
)

var counter int

func state1() machine.State {
	fmt.Println("State 1")
	return state2
}

func state2() machine.State {
	fmt.Println("State 2")
	return state3
}

func state3() machine.State {
	fmt.Println("State 3")
	if counter > 6 {
		return nil
	}
	return state2
}

func main() {
	m := machine.State(state1)

	for m != nil {
		counter++
		m.Update()
	}
}
Output:

State 1
State 2
State 3
State 2
State 3
State 2
State 3

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type State

type State func() State

State is used to implement a lightweight state machine. Each state is represented by a closure, that, when run, returns the next state (or nil to stop the machine).

func (*State) Update

func (s *State) Update()

Update runs s (i.e. the current state), and replaces it with the result of that call. It's safe to call Update on a nil target.

Jump to

Keyboard shortcuts

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