Documentation
¶
Overview ¶
Package fn standardizes functional types and traditional map/filter/reduce/each functions so that type switching on them can be done consistently. It also provides a collection of common type implementations.
func(in any) any ¶
Rather that force casting in order for type switches to work, the func(in any) any is left as is and assumed to be used for any type switches checking for map functions. In other words, the func(in any) any is itself a first-class type in this package and those using it should prefer it for all map function implementations.
Index ¶
- func Fall[T comparable](vals ...T) T
- func Filter[T any](slice []T, f func(in T) bool) []T
- func Map[I any, O any](slice []I, f func(in I) O) []O
- func Or[T comparable](this, that T) T
- func Pipe(filter ...any) string
- func PipePrint(filter ...any)
- func Reduce[T any, R any](slice []T, f func(in T, ref *R)) *R
- type A
- func (a A[any]) E(f func(i any))
- func (a A[any]) Each(f func(i any))
- func (a A[any]) F(f func(i any) bool) A[any]
- func (a A[any]) Filter(f func(i any) bool) A[any]
- func (a A[any]) Log()
- func (a A[any]) Logf(f string)
- func (a A[any]) M(f func(i any) any) A[any]
- func (a A[any]) Map(f func(i any) any) A[any]
- func (a A[any]) Print()
- func (a A[any]) Printf(t string)
- func (a A[any]) Println()
- func (a A[any]) R(f func(i any, r *any)) *any
- func (a A[any]) Reduce(f func(i any, r *any)) *any
- type Filterer
- type Number
- type Reducer
- type Sharable
- type Text
- type Transformer
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Fall ¶ added in v0.4.0
func Fall[T comparable](vals ...T) T
Fall iterates over a list of values, returning the first non-zero value. If [vals] contains only one element, it returns that value. It recursively calls Or to compare each element with the subsequent elements until it finds a non-zero value or reaches the end of the list.
Example ¶
package main import ( "fmt" "github.com/rwxrob/bonzai/fn" ) func main() { // int fmt.Println(fn.Fall(0, 1, 0)) fmt.Println(fn.Fall(2, 0, 0)) fmt.Println(fn.Fall(0, 0, 3)) fmt.Println(fn.Fall(0, 0, 0)) // string fmt.Println(fn.Fall("", "one", "")) fmt.Println(fn.Fall("two", "", "")) fmt.Println(fn.Fall("", "", "three")) fmt.Println(fn.Fall("", "", "")) }
Output: 1 2 3 0 one two three
func Filter ¶
Filter applies the boolean function on each item only returning those items that evaluate to true.
func Map ¶
Map executes an operator function provided on each item in the slice returning a new slice with items of a potentially different type completely (which is different from using the Array.Map method which requires returning the same type). If error handling is needed it should be handled within an enclosure within the function. This keeps signatures simple and functional.
func Or ¶ added in v0.4.0
func Or[T comparable](this, that T) T
Or returns the first non-zero value of the two provided. If this is the zero value for type [T], it returns that; otherwise, it returns this.
Example ¶
package main import ( "fmt" "github.com/rwxrob/bonzai/fn" ) func main() { // int fmt.Println(fn.Or(0, 1)) fmt.Println(fn.Or(1, 0)) fmt.Println(fn.Or(0, 0)) // string fmt.Println(fn.Or("", "one")) fmt.Println(fn.Or("one", "")) fmt.Println(fn.Or("", "")) }
Output: 1 1 0 one one
func Pipe ¶
Pipe implements the closest thing to UNIX pipelines possible in Go by passing each argument to the next assuming a func(in any) any format where the input (in) is converted to a string (if not already a string). If any return an error the pipeline returns an empty string and logs an error.
func PipePrint ¶
func PipePrint(filter ...any)
PipePrint prints the output (and a newline) of a Pipe logging any errors encountered.
Example ¶
package main import ( "fmt" "github.com/rwxrob/bonzai/fn" ) func main() { thing := func(in any) any { return fmt.Sprintf("%v thing", in) } other := func(in any) any { return fmt.Sprintf("%v other", in) } fn.PipePrint("some", other, thing) }
Output: some other thing
Example (Error) ¶
package main import ( "fmt" "log" "os" "github.com/rwxrob/bonzai/fn" ) func main() { defer log.SetOutput(os.Stderr) defer log.SetFlags(log.Flags()) log.SetOutput(os.Stdout) log.SetFlags(0) thing := func(in any) any { return fmt.Sprintf("%v thing", in) } other := func(in any) any { return fmt.Errorf("bork") } fn.PipePrint("some", other, thing) }
Output: bork
Types ¶
type A ¶
type A[T any] []T
A is the equivalent of an array primitive in other functional languages. It is a generic slice of anything.
Example (A_is_for_array) ¶
package main import ( "fmt" "github.com/rwxrob/bonzai/fn" ) func main() { fn.A[int]{1, 2, 3}.Print() fmt.Println() fn.A[string]{"one", "two", "three"}.Println() }
Output: 123 one two three
func (A[any]) Each ¶
Each calls each.Do on self
Example ¶
package main import ( "fmt" "github.com/rwxrob/bonzai/fn" ) func main() { fn.A[int]{1, 2, 3}.Each(func(i int) { fmt.Print(i) }) fn.A[int]{1, 2, 3}.E(func(i int) { fmt.Print(i) }) }
Output: 123123
func (A[any]) Filter ¶
Filter calls Filter function on self.
Example ¶
package main import ( "github.com/rwxrob/bonzai/fn" ) func main() { GtTwo := func(i int) bool { return i > 2 } LtFour := func(i int) bool { return i < 4 } fn.A[int]{1, 2, 3, 4}.Filter(GtTwo).Print() fn.A[int]{1, 2, 3, 4}.F(GtTwo).F(LtFour).Print() }
Output: 343
func (A[any]) Log ¶
Log calls each.Log on self.
Example ¶
package main import ( "log" "os" "github.com/rwxrob/bonzai/fn" ) func main() { defer log.SetOutput(os.Stderr) defer log.SetFlags(log.Flags()) log.SetOutput(os.Stdout) log.SetFlags(0) fn.A[int]{1, 2}.Log() fn.A[int]{1, 2}.Logf("some: %v") }
Output: 1 2 some: 1 some: 2
func (A[any]) Map ¶
Map calls Map function on self.
Example ¶
package main import ( "github.com/rwxrob/bonzai/fn" ) func main() { AddOne := func(i int) int { return i + 1 } fn.A[int]{1, 2, 3}.Map(AddOne).Print() fn.A[int]{1, 2, 3}.M(AddOne).M(AddOne).Print() }
Output: 234345
func (A[any]) Print ¶
Print calls each.Print on self.
Example ¶
package main import ( "github.com/rwxrob/bonzai/fn" ) func main() { fn.A[int]{1, 2}.Println() fn.A[int]{1, 2}.Printf("some: %v\n") fn.A[int]{1, 2}.Print() }
Output: 1 2 some: 1 some: 2 12
type Number ¶
type Number interface { int | int64 | int32 | int16 | int8 | uint64 | uint32 | uint16 | uint8 | float64 | float32 }
Number combines the primitives generally considered numbers by JSON and other high-level structure data representations.
type Sharable ¶
type Sharable interface { int | int64 | int32 | int16 | int8 | uint64 | uint32 | uint16 | uint8 | float64 | float32 | []byte | string | bool }
Sharable are the types that have representations in JSON, YAML, TOML and other high-level structured data representations.
type Transformer ¶ added in v0.7.0
Directories
¶
Path | Synopsis |
---|---|
Package each shamelessly attempts to bring the better parts of Lisp loops to Go specifically in order to enable rapid, and clean applications development --- particularly when replacing shell scripts with Go.
|
Package each shamelessly attempts to bring the better parts of Lisp loops to Go specifically in order to enable rapid, and clean applications development --- particularly when replacing shell scripts with Go. |
Package mapf contains nothing but map functions suitable for use with the fn.Map generic function or the equivalent fn.A method.
|
Package mapf contains nothing but map functions suitable for use with the fn.Map generic function or the equivalent fn.A method. |