fn

package module
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2024 License: Apache-2.0 Imports: 3 Imported by: 2

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

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

func Filter[T any](slice []T, f func(in T) bool) []T

Filter applies the boolean function on each item only returning those items that evaluate to true.

func Map

func Map[I any, O any](slice []I, f func(in I) O) []O

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

func Pipe(filter ...any) string

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

func Reduce

func Reduce[T any, R any](slice []T, f func(in T, ref *R)) *R

Reduce calls the given reducer function for every item in the slice passing a required reference to an item to hold the results If error handling is needed it should be handled within an enclosure within the function. This keeps signatures simple and functional.

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]) E

func (a A[any]) E(f func(i any))

E calls Do function on self.

func (A[any]) Each

func (a A[any]) Each(f func(i any))

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]) F

func (a A[any]) F(f func(i any) bool) A[any]

Filter calls Filter function on self.

func (A[any]) Filter

func (a A[any]) Filter(f func(i any) bool) A[any]

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

func (a 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]) Logf

func (a A[any]) Logf(f string)

Logf calls each.Logf on self.

func (A[any]) M

func (a A[any]) M(f func(i any) any) A[any]

M calls Map function on self.

func (A[any]) Map

func (a A[any]) Map(f func(i any) any) A[any]

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

func (a 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

func (A[any]) Printf

func (a A[any]) Printf(t string)

Printf calls each.Printf on self.

func (A[any]) Println

func (a A[any]) Println()

Println calls each.Println on self.

func (A[any]) R

func (a A[any]) R(f func(i any, r *any)) *any

Reduce calls Reduce function on self.

func (A[any]) Reduce

func (a A[any]) Reduce(f func(i any, r *any)) *any

Reduce calls Reduce function on self.

Example
package main

import (
	"fmt"

	"github.com/rwxrob/bonzai/fn"
)

func main() {
	Sum := func(i int, a *int) { *a += i }
	fmt.Println(*fn.A[int]{1, 2}.Reduce(Sum))
	fmt.Println(*fn.A[int]{1, 2, 3, 4, 5}.R(Sum))
}
Output:

3
15

type Filterer added in v0.7.0

type Filterer[T any] interface {
	Filter(in []T) []T
}

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 Reducer added in v0.7.0

type Reducer[T any] interface {
	Reduce(in []T) []T
}

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 Text

type Text interface {
	[]byte | string | []rune
}

Text combines byte slice and string.

type Transformer added in v0.7.0

type Transformer[I any, O any] interface {
	Transform(in []I) []O
}

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.

Jump to

Keyboard shortcuts

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