fold

package
v0.0.0-...-917641f Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2019 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package fold implements a simple scheme for folding.

Folding is similar to branching in that a set of changes are made but not pushed up to the original stream. But unlike branching, folds allow more *unfolded* changes to be made on top, correctly transferring these upstream (after transforming them against the folded changes). Similarly, upstream changes are pulled in, also correctly transforming them.

TODO: It is a bit tricky to remove folded changes. In an ideal setup, removing folds should have no effect on any streams derived from the fold while having the right effect upstream. This is not yet implemented.

Example (AppendFolded)
package main

import (
	"fmt"

	"github.com/dotchain/dot/changes"
	"github.com/dotchain/dot/streams"
	"github.com/dotchain/dot/x/fold"
)

func main() {
	upstream := streams.New()

	// move [0 - 5] to the right by 10
	folded := fold.New(changes.Move{Offset: 0, Count: 5, Distance: 10}, upstream)

	// move [1 - 2] to the right by 20 and see it on upstream
	folded = folded.Append(changes.Move{Offset: 1, Count: 1, Distance: 20})
	if x, _ := folded.Next(); x != nil {
		fmt.Println("Unexpected Next() behavior", x)
	}

	_, c := upstream.Next()

	cx, _ := fold.Unfold(folded)
	fmt.Printf("%#v\n%#v\n", c, cx)

}
Output:

changes.ChangeSet{changes.Change(nil), changes.Move{Offset:6, Count:1, Distance:15}}
changes.Move{Offset:0, Count:5, Distance:9}
Example (AppendUpstream)
package main

import (
	"fmt"

	"github.com/dotchain/dot/changes"
	"github.com/dotchain/dot/streams"
	"github.com/dotchain/dot/x/fold"
)

func main() {
	upstream := streams.New()

	// move [0 - 5] to the right by 10
	folded := fold.New(changes.Move{Offset: 0, Count: 5, Distance: 10}, upstream)

	// move [1 - 2] to the right by 1 and see it on the folded
	upstream.Append(changes.Move{Offset: 1, Count: 1, Distance: 1})
	_, c := folded.Next()

	fmt.Printf("%#v\n", c)

}
Output:

changes.ChangeSet{changes.Change(nil), changes.Move{Offset:11, Count:1, Distance:1}}
Example (NilFold)
package main

import (
	"fmt"

	"github.com/dotchain/dot/changes"
	"github.com/dotchain/dot/changes/types"
	"github.com/dotchain/dot/streams"
	"github.com/dotchain/dot/x/fold"
)

func main() {
	upstream := streams.New()

	folded := fold.New(changes.Splice{Offset: 0, Before: types.S8(""), After: types.S8("hello")}, upstream)
	folded2 := folded.Append(changes.Splice{Offset: 1, Before: types.S8("e"), After: types.S8("u")})

	folded.Append(changes.Splice{Offset: 10, Before: types.S8(""), After: types.S8("insert")})

	_, c := upstream.Next()
	fmt.Println("Got Change:", c)

	c, _ = fold.Unfold(folded2)
	fmt.Println("Unfolded:", c)

}
Output:

Got Change: {5  insert}
Unfolded: {0  hullo}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

New returns a new stream with a "folded" change. This change is not applied onto the base stream but held back. Any further changes on the underlying stream or the returned stream are properly proxied back and forth.

The folded change can be fetched back by calling Unfold on the returned stream or any stream derived from it.

func Unfold

Unfold takes any stream derived from a folded stream (created by New) and returns the current state of the "change" that is folded as well as the modified base stream.

It panics if the provided stream is not derived from New().

Types

This section is empty.

Jump to

Keyboard shortcuts

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