do

package module
v0.0.0-...-326b91d Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2019 License: MIT Imports: 0 Imported by: 8

README

"I did it my Way"

  • poor, or well?

  • too clever?

  • too stupid?

Time will tell :-)

Software License Go Report Card Build Status GoDoc

Allways aiming at the Middle Way - the Path is the Journey ... :-)

Your suggestions, remarks, questions and/or contributions are welcome ;-)


Think deep - code happy - be simple - see clear :-)


Support on Beerpay

Hey dude! Help me out for a couple of 🍻!

Beerpay Beerpay

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

func ErrIt(its ...It) Err

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

func ErrJoin(fs ...Err) Err

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.

func (*Err) Add

func (err *Err) Add(errs ...Err) Option

Add appends all errs to the existing err when the returned Option is applied.

func (Err) Do

func (a Err) Do() error

Do evaluates Err iff Err is not nil, and makes Err an Errer.

func (*Err) Set

func (err *Err) Set(errs ...Err) Option

Set sets all errs as the new err when the returned Option is applied.

type If

type If struct {
	It
	If bool
}

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.

func (If) Do

func (a If) Do()

Do applies It iff If is true and It is not nil, and makes If a Doer.

func (*If) Iff

func (fn *If) Iff(iff bool) Option

Iff makes iff the new If value when the returned Option is applied.

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

func ItJoin(fs ...It) It

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.

func (*It) Add

func (it *It) Add(its ...It) Option

Add appends all its to the existing It action when the returned Option is applied.

func (*It) Do

func (it *It) Do()

Do applies It iff It is not nil.

func (*It) Set

func (it *It) Set(its ...It) Option

Set sets all its as the new It action when the returned Option is applied.

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

func NokIt(its ...It) Nok

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

func NokJoin(fs ...Nok) Nok

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.

func (*Nok) Add

func (nok *Nok) Add(noks ...Nok) Option

Add appends all noks to the existing nok when the returned Option is applied.

func (*Nok) Do

func (nok *Nok) Do() bool

Do applies Ok iff Ok is not nil and returns its value - usually false,

func (*Nok) Not

func (nok *Nok) Not() bool

Not returns the negation - usually true.

func (*Nok) Set

func (nok *Nok) Set(noks ...Nok) Option

Set sets all noks as the new nok when the returned Option is applied.

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

func OkIt(its ...It) Ok

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

func OkJoin(fs ...Ok) Ok

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.

func (*Ok) Add

func (ok *Ok) Add(oks ...Ok) Option

Add appends all oks to the existing ok when the returned Option is applied.

func (*Ok) Do

func (ok *Ok) Do() bool

Do applies Ok iff Ok is not nil and returns its value - usually true,

func (*Ok) Not

func (ok *Ok) Not() bool

Not returns the negation - usually false.

func (*Ok) Set

func (ok *Ok) Set(oks ...Ok) Option

Set sets all oks as the new ok when the returned Option is applied.

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

func OptIt(its ...It) Opt

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

func OptJoin(fs ...Opt) Opt

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

func Options(a interface{}, options ...Option) Opt

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

func (*Opt) Add

func (opt *Opt) Add(opts ...Opt) Option

Add appends all opts to the existing opt when the returned Option is applied.

func (Opt) Do

func (a Opt) Do() Opt

Do applies Opt iff Opt is not nil.

func (*Opt) Set

func (opt *Opt) Set(opts ...Opt) Option

Set sets all opts as the new opt when the returned Option is applied.

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:

(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

func OptionIt(its ...It) Option

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

func OptionJoin(fs ...Option) Option

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

func (*Option) Add

func (option *Option) Add(options ...Option) Option

Add appends all options to the existing option when the returned Option is applied.

func (*Option) Set

func (option *Option) Set(options ...Option) Option

Set sets all options as the new option when the returned Option is applied.

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.

Jump to

Keyboard shortcuts

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