1package immutable
2
3// Immutable represents an immutable chain that, if passed to FastForward,
4// applies Fn() to every element of a chain, the first element of this chain is
5// represented by Base().
6type Immutable interface {
7	// Prev is the previous element on a chain.
8	Prev() Immutable
9	// Fn a function that is able to modify the passed element.
10	Fn(interface{}) error
11	// Base is the first element on a chain, there's no previous element before
12	// the Base element.
13	Base() interface{}
14}
15
16// FastForward applies all Fn methods in order on the given new Base.
17func FastForward(curr Immutable) (interface{}, error) {
18	prev := curr.Prev()
19	if prev == nil {
20		return curr.Base(), nil
21	}
22	in, err := FastForward(prev)
23	if err != nil {
24		return nil, err
25	}
26	err = curr.Fn(in)
27	return in, err
28}
29