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 ¶
- 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 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 Number
- type Sharable
- type Text
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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 Pipe ¶ added in v0.3.0
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
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]) Each ¶
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]) Filter ¶
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 ¶
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]) Map ¶
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 ¶
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
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.
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. |