Help us shape the future of Go! Take the 2026 Go Developer Survey

fn

package module
v0.4.0 Latest Latest
Warning

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

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

README

Functional Go 1.18 with Generics

Go Version GoDoc License Go Report Card

Note: Go report card doesn't do 1.18 yet.

Documentation

Overview

Package fn implements the traditional map/filter/reduce/each functions and an array type (A) for those who prefer a more object-oriented approach. Unlike other implementations, the array (slice) is always first preventing the first-class in-line anonymous function from obfuscating the parameter list of the functional function.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

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 Pipe added in v0.3.0

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

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/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/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/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/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/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/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/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/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/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 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 Text

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

Text combines byte slice and string.

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