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 Reduce[T any, R any](slice []T, f func(in T, ref *R)) *R
- func Reverse[T any](list []T) chan T
- 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 Reduce ¶
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.
func Reverse ¶
func Reverse[T any](list []T) chan T
Reverse reverses the order of any slice passed to it.
Example ¶
package main
import (
"fmt"
"github.com/rwxrob/fn"
)
func main() {
strings := []string{"a", "b", "c", "d"}
for e := range fn.Reverse(strings) {
fmt.Print(e)
}
fmt.Println()
ints := []int{1, 2, 3, 4}
for e := range fn.Reverse(ints) {
fmt.Print(e)
}
fmt.Println()
anys := []any{1, "two", 3.3, '4'}
for e := range fn.Reverse(anys) {
fmt.Print(e)
}
}
Output: dcba 4321 523.3two1
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() {
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. |