Documentation ¶
Overview ¶
Example (Interfaces) ¶
package main import ( "github.com/GoLangsam/do" ) func main() { // doer represents anyone who can apply some action // - usually as a closure around itself. type doer interface { Do() } // Iter represents anyone who can apply some action // - usually as a closure around itself. type Iter interface { doer Set(its ...do.It) do.Option Add(its ...do.It) do.Option } // Iffer represents anyone who can apply some action iff. // - usually as a closure around itself. type Iffer interface { doer Set(its ...do.It) do.Option Add(its ...do.It) do.Option } // Booler represents anyone who can provide some boolean // - usually as a closure around itself. type booler interface { Do() bool } // Oker represents anyone who can provide some boolean // (by default: true) // - usually as a closure around itself. type Oker interface { booler Set(oks ...do.Ok) do.Option Add(oks ...do.Ok) do.Option } // Noker represents anyone who can provide some boolean // (by default: false) // - usually as a closure around itself. type Noker interface { booler Set(noks ...do.Nok) do.Option Add(noks ...do.Nok) do.Option } // Errer represents anyone who can provide some error // - usually as a closure around itself. type Errer interface { Do() error Set(errs ...do.Err) do.Option Add(errs ...do.Err) do.Option } // Opter represents anyone who can provide some option // - usually as a closure around itself. type Opter interface { Do() do.Opt Set(opts ...do.Opt) do.Option Add(opts ...do.Opt) do.Option } var doit do.It = func() { return } var ok do.Ok = func() bool { return true } var iff do.If var nok do.Nok = func() bool { return false } var err do.Err = func() error { return nil } var opt do.Opt var _ doer = &doit var _ Iter = &doit var _ Iffer = &iff var _ Oker = &ok var _ Noker = &nok var _ Errer = &err var _ Opter = &opt }
Output:
Example (Value) ¶
package main import ( "fmt" "github.com/GoLangsam/container/oneway/list" "github.com/GoLangsam/do" ) // Value returns a function which // sets Value to v // and returns it's undo Opt. func Value(v interface{}) do.Option { return func(any interface{}) do.Opt { a := any.(*list.Element) prev := (*a).Value (*a).Value = v return func() do.Opt { return Value(prev)(a) } } } func main() { // Value returns a function which // sets Value to v // and returns it's undo Opt. Value := func(v interface{}) do.Option { return func(any interface{}) do.Opt { a := any.(*list.Element) prev := a.Value a.Value = v return func() do.Opt { return Value(prev)(a) } } } e := list.NewList("TestList", "Element One").Front() setValue := func(e *list.Element, v interface{}) { // upon exit apply undo to restore original value while setting to new value v now via defer Value(v)(e)() // Note the triple evaluation. // ... do some stuff with Value being temporarily set to v. fmt.Println(e.Value) // Changed Value } fmt.Println(e.Value) // Original Value setValue(e, 5) fmt.Println(e.Value) }
Output: Element One 5 Element One
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Err ¶
type Err func() error
Err represents some action which might go wrong (for some reason).
The null value is useful: its Do() never returns a non-nil error.
func ErrIt ¶
ErrIt returns an Err function which Do()es the Join of the given its and returns the default, namely: `nil`, upon evaluation.
Evaluate the returned function by invoking it's Do() or by invoking it directly, iff not nil.
ErrIt is a convenient wrapper.
func ErrJoin ¶
ErrJoin returns a closure around given fs.
Iff there are no fs, nil is returned, and iff there is only one fs, this single fs is returned.
Evaluate the returned function by invoking its Do() or by invoking it directly, iff not nil.
Note: Order matters - evaluation terminates on first exceptional (non-default) result.
type If ¶
If represents an opional action.
It wraps It (an Action - pun intended) and a boolean If switch and it facilitates conditional invocation via its Do() method.
Intended use is for conditional logging, counting etc.
The null value is useful: its Do() never does anything, it's a nop.
type It ¶
type It func()
It represents some action: do.It.
The null value is useful: its Do() never does anything: it's a nop.
func ItJoin ¶
ItJoin returns a closure around given fs.
Iff there are no fs, nil is returned, and iff there is only one fs, this single fs is returned.
Evaluate the returned function by invoking its Do() or by invoking it directly, iff not nil.
type Nok ¶
type Nok func() bool
Nok represents some condition which is false by default.
The null value is useful: its Do() never returns true.
func NokIt ¶
NokIt returns a Nok function which Do()es the Join of the given its and returns the default, namely: `false`, upon evaluation.
Evaluate the returned function by invoking it's Do() or by invoking it directly, iff not nil.
NokIt is a convenient wrapper.
func NokJoin ¶
NokJoin returns a closure around given fs.
Iff there are no fs, nil is returned, and iff there is only one fs, this single fs is returned.
Evaluate the returned function by invoking its Do() or by invoking it directly, iff not nil.
Note: Order matters - evaluation terminates on first exceptional (non-default) result.
type Ok ¶
type Ok func() bool
Ok represents some condition which is true by default.
The null value is useful: its Do() never returns false.
func OkIt ¶
OkIt returns an Ok function which Do()es the Join of the given its and returns the default, namely: `true`, upon evaluation.
Evaluate the returned function by invoking it's Do() or by invoking it directly, iff not nil.
OkIt is a convenient wrapper.
func OkJoin ¶
OkJoin returns a closure around given fs.
Iff there are no fs, nil is returned, and iff there is only one fs, this single fs is returned.
Evaluate the returned function by invoking its Do() or by invoking it directly, iff not nil.
Note: Order matters - evaluation terminates on first exceptional (non-default) result.
type Opt ¶
type Opt func() Opt
Opt represents some option which can be taken.
Opt is a self referential function obtained when some Option is applied and should return its own undo Opt.
The null value is useful: its Do() never does anything as it is a nop and returns a nop.Opt.
Example ¶
package main import ( "fmt" "github.com/GoLangsam/container/oneway/list" "github.com/GoLangsam/do" ) // Value returns a function which // sets Value to v // and returns it's undo Opt. func Value(v interface{}) do.Option { return func(any interface{}) do.Opt { a := any.(*list.Element) prev := (*a).Value (*a).Value = v return func() do.Opt { return Value(prev)(a) } } } func main() { e := list.NewList("TestList", "Element One").Front() fmt.Println(e.Value) // Element One undo := Value(3)(e) fmt.Println(e.Value) // 3 (temporarily) redo := undo() fmt.Println(e.Value) // Element One (temporary setting undone) rere := redo() fmt.Println(e.Value) // 3 (undo undone) _ = rere() fmt.Println(e.Value) // Element One (temporary setting undone) }
Output: Element One 3 Element One 3 Element One
func OptIt ¶
OptIt returns an Opt function which Do()es the Join of the given its and returns the default, namely: its undo Opt, upon evaluation.
Evaluate the returned function by invoking its Do() or by invoking it directly, iff not nil.
OptIt may look like a convenient wrapper.
Just beware: OptIt violates the option contract! No working undo Opt is returned - only a nop.Opt.
func OptJoin ¶
OptJoin returns a closure around given fs.
Iff there are no fs, a nop.Opt is returned, and iff there is only one fs, this single fs is returned.
Evaluate the returned function by invoking its Do() or by invoking it directly, iff not nil.
func Options ¶
Options applies every Option and returns its undo Opt-function which, when evaluated, fully restores the previous state.
Example ¶
package main import ( "fmt" "github.com/GoLangsam/container/oneway/list" "github.com/GoLangsam/do" ) // Value returns a function which // sets Value to v // and returns it's undo Opt. func Value(v interface{}) do.Option { return func(any interface{}) do.Opt { a := any.(*list.Element) prev := (*a).Value (*a).Value = v return func() do.Opt { return Value(prev)(a) } } } func main() { e := list.NewList("TestList", "Element One").Front() fmt.Println(e.Value) // Element One undo := do.Options(e, Value(3), Value("5"), Value(7)) fmt.Println(e.Value) // 7 (temporarily) redo := undo() fmt.Println(e.Value) // Element One (temporary setting undone) _ = redo() fmt.Println(e.Value) // 7 (undo undone) }
Output: Element One 7 Element One 7
Example (Nochange) ¶
package main import ( "fmt" "github.com/GoLangsam/container/oneway/list" "github.com/GoLangsam/do" ) func main() { e := list.NewList("TestList", "Element One").Front() undo := do.Options(e) fmt.Println(e.Value) // Element One (unchanged) redo := undo() fmt.Println(e.Value) // Element One (temporary no-change undone) _ = redo() fmt.Println(e.Value) // Element One (temporary no-change undo redone) }
Output: Element One Element One Element One
type Option ¶
type Option func(interface{}) Opt
Option is a function which modifes something when applied to it and returns its own undo function (as Opt),
which, when applied, returns it's redo function (as Opt), which, when applied, returns it's redo undo function (as Opt), which, when applied, returns it's redo undo undo function (as Opt), which, when applied, returns ... (and so on: mind You: Opt is a self referential function).
Hint: To apply multiple options at once, use do.Options(a, opts...).
Note: Option will panic iff applied to the wrong type of object. (This is due to the known need of Go to assert the type dynamically.)
Hint: Provide Your own Options method and catch any panic in there.
Note: This implementation was inspired by:
- http://commandcenter.blogspot.com.au/2014/01/self-referential-functions-and-design.html
- https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis
- https://www.calhoun.io/using-functional-options-instead-of-method-chaining-in-go/
(Just: in these samples Undo is only supported for the last Option passed.)
This implementation of do.Options(myType, myOpts...) provides full undo.
func OptionIt ¶
OptionIt returns an Option function which effects the Join of the given its when the returned Option is applied and returns the default, namely: its undo Opt.
OptionIt may look like a convenient wrapper.
Just beware: OptIt violates the option contract! No working undo Opt is returned - only a nop.Opt.
func OptionJoin ¶
OptionJoin returns a closure around given fs.
Iff there are no fs, a nop.Option is returned, and iff there is only one fs, this single fs is returned.
Evaluate the returned function in order to apply its effect.
Example ¶
package main import ( "fmt" "github.com/GoLangsam/container/oneway/list" "github.com/GoLangsam/do" ) // Value returns a function which // sets Value to v // and returns it's undo Opt. func Value(v interface{}) do.Option { return func(any interface{}) do.Opt { a := any.(*list.Element) prev := (*a).Value (*a).Value = v return func() do.Opt { return Value(prev)(a) } } } func main() { e := list.NewList("TestList", "Element One").Front() fmt.Println(e.Value) // Element One undo := do.OptionJoin(Value(3), Value("5"), Value(7))(e) fmt.Println(e.Value) // 7 (temporarily) redo := undo() fmt.Println(e.Value) // Element One (temporary setting undone) _ = redo() fmt.Println(e.Value) // 7 (undo undone) }
Output: Element One 7 Element One 7
Directories ¶
Path | Synopsis |
---|---|
Package ami / `any meta info` - easy access to meta data I love to be informative - and even give metadata about my content anything use TypeName to get the name of the type of my content use TypePkgPath to get the package name of the type of my content use TypeSize to get the size (in bytes) of the type of my content use TypeString to get a 'nick-name' of the type of my content use TypeKind to get the Kind of the type of my content ( int, struct, func, ...) use TypeIsComparable ...
|
Package ami / `any meta info` - easy access to meta data I love to be informative - and even give metadata about my content anything use TypeName to get the name of the type of my content use TypePkgPath to get the package name of the type of my content use TypeSize to get the size (in bytes) of the type of my content use TypeString to get a 'nick-name' of the type of my content use TypeKind to get the Kind of the type of my content ( int, struct, func, ...) use TypeIsComparable ... |
Package ats (= any to string) provides functions to Get a string from 'anything' (= interface{}) ats observes different conventions of 'things' (=objects) to do so: stringer: String() - fmt.Stringer & friends string: string - builtin type namer: Name() - filepath.File & .FileInfo, text/template.Template ...
|
Package ats (= any to string) provides functions to Get a string from 'anything' (= interface{}) ats observes different conventions of 'things' (=objects) to do so: stringer: String() - fmt.Stringer & friends string: string - builtin type namer: Name() - filepath.File & .FileInfo, text/template.Template ... |
cli
|
|
cancel
Package cancel - convenient cancellation for cli based cmd's.
|
Package cancel - convenient cancellation for cli based cmd's. |
cancel/cmd/canceller
Package main is a toy to play with do/cli/cancel (and context)
|
Package main is a toy to play with do/cli/cancel (and context) |
Package id provides a few simple iterables useful to range over and/or to produce some testdata such as: some finite read-only channel: - I(N) to range over the first N ordinal numbers 1...N - C(N) to range over the first N+1 cardinal numbers 0...N - Names(prefix, N) to range over N size-adjusted prefixed IDs.
|
Package id provides a few simple iterables useful to range over and/or to produce some testdata such as: some finite read-only channel: - I(N) to range over the first N ordinal numbers 1...N - C(N) to range over the first N+1 cardinal numbers 0...N - Names(prefix, N) to range over N size-adjusted prefixed IDs. |
Package nvp provides helper functions for any KeyValuePair, which satisfies the K/V/GetV = Key/Value interface.
|
Package nvp provides helper functions for any KeyValuePair, which satisfies the K/V/GetV = Key/Value interface. |
Package qqq provides easy printing, logging and panicing to any package.
|
Package qqq provides easy printing, logging and panicing to any package. |
Package scan provides some dead-simple useful scanners starting with - LinesOfWords - WordsPerLine
|
Package scan provides some dead-simple useful scanners starting with - LinesOfWords - WordsPerLine |
Package ds provides useless string functions which (s)c(h)ould be in "strings" but are not (yet) there.
|
Package ds provides useless string functions which (s)c(h)ould be in "strings" but are not (yet) there. |