Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

v0 source pure

Package fp provides functional programming utilities for Gno, enabling transformations, filtering, and other operatio...

Readme View source

gno.land/p/moul/fp/v0

TODO: describe this package.


Part of moul/gno-contracts — moul's versioned gno.land contracts. See the repository for the full catalog, build/test tooling, and usage.

⚠️ Disclaimer: provided as-is, without warranty; not security-audited. Full disclaimer: DISCLAIMER.

Overview

Package fp provides functional programming utilities for Gno, enabling transformations, filtering, and other operations on slices of any.

Example of chaining operations:

Example
 1numbers := []any{1, 2, 3, 4, 5, 6}
 2
 3// Define predicates, mappers and reducers
 4isEven := func(v any) bool { return v.(int)%2 == 0 }
 5double := func(v any) any { return v.(int) * 2 }
 6sum := func(a, b any) any { return a.(int) + b.(int) }
 7
 8// Chain operations: filter even numbers, double them, then sum
 9evenNums := Filter(numbers, isEven)        // [2, 4, 6]
10doubled := Map(evenNums, double)           // [4, 8, 12]
11result := Reduce(doubled, sum, 0)          // 24
12
13// Alternative: group by even/odd, then get even numbers
14byMod2 := func(v any) any { return v.(int) % 2 }
15grouped := GroupBy(numbers, byMod2)        // {0: [2,4,6], 1: [1,3,5]}
16evens := grouped[0]                        // [2,4,6]

Functions 14

func All

1func All(values []any, fn Predicate) bool
source

All returns true if all elements satisfy the predicate.

Example:

Example
1numbers := []any{2, 4, 6, 8}
2isEven := func(v any) bool { return v.(int)%2 == 0 }
3result := All(numbers, isEven) // true

func Any

1func Any(values []any, fn Predicate) bool
source

Any returns true if at least one element satisfies the predicate.

Example:

Example
1numbers := []any{1, 3, 4, 7}
2isEven := func(v any) bool { return v.(int)%2 == 0 }
3result := Any(numbers, isEven) // true (4 is even)

func Chunk

1func Chunk(values []any, size int) [][]any
source

Chunk splits a slice into chunks of the given size.

Example:

Example
1numbers := []any{1, 2, 3, 4, 5}
2result := Chunk(numbers, 2) // [[1,2], [3,4], [5]]

func Filter

1func Filter(values []any, fn Predicate) []any
source

Filter filters elements from the slice that satisfy the given predicate.

Example:

Example
1numbers := []any{-1, 0, 1, 2}
2isPositive := func(v any) bool { return v.(int) > 0 }
3result := Filter(numbers, isPositive) // [1, 2]

func Find

1func Find(values []any, fn Predicate) (any, bool)
source

Find returns the first element that satisfies the predicate and a boolean indicating if an element was found.

Example:

Example
1numbers := []any{1, 2, 3, 4}
2isEven := func(v any) bool { return v.(int)%2 == 0 }
3result, found := Find(numbers, isEven) // 2, true

func FlatMap

1func FlatMap(values []any, fn Mapper) []any
source

FlatMap maps each element to a collection and flattens the results.

Example:

Example
1words := []any{"hello", "world"}
2split := func(v any) any {
3    chars := []any{}
4    for _, c := range v.(string) {
5        chars = append(chars, string(c))
6    }
7    return chars
8}
9result := FlatMap(words, split) // ["h","e","l","l","o","w","o","r","l","d"]

func Flatten

1func Flatten(values [][]any) []any
source

Flatten flattens a slice of slices into a single slice.

Example:

Example
1nested := [][]any{{1,2}, {3,4}, {5}}
2result := Flatten(nested) // [1,2,3,4,5]

func GroupBy

1func GroupBy(values []any, fn Mapper) map[any][]any
source

GroupBy groups elements based on a key returned by a Mapper.

Example:

Example
1numbers := []any{1, 2, 3, 4, 5, 6}
2byMod3 := func(v any) any { return v.(int) % 3 }
3result := GroupBy(numbers, byMod3) // {0: [3,6], 1: [1,4], 2: [2,5]}

func Map

1func Map(values []any, fn Mapper) []any
source

Map applies a function to each element in the slice.

Example:

Example
1numbers := []any{1, 2, 3}
2toString := func(v any) any { return fmt.Sprintf("%d", v) }
3result := Map(numbers, toString) // ["1", "2", "3"]

func None

1func None(values []any, fn Predicate) bool
source

None returns true if no elements satisfy the predicate.

Example:

Example
1numbers := []any{1, 3, 5, 7}
2isEven := func(v any) bool { return v.(int)%2 == 0 }
3result := None(numbers, isEven) // true (no even numbers)

func Reduce

1func Reduce(values []any, fn Reducer, initial any) any
source

Reduce reduces a slice to a single value by applying a function.

Example:

Example
1numbers := []any{1, 2, 3, 4}
2sum := func(a, b any) any { return a.(int) + b.(int) }
3result := Reduce(numbers, sum, 0) // 10

func Reverse

1func Reverse(values []any) []any
source

Reverse reverses the order of elements in a slice.

Example:

Example
1numbers := []any{1, 2, 3}
2result := Reverse(numbers) // [3, 2, 1]

func Unzip

1func Unzip(pairs [][2]any) ([]any, []any)
source

Unzip splits a slice of pairs into two separate slices.

Example:

Example
1pairs := [][2]any{{1,"a"}, {2,"b"}, {3,"c"}}
2numbers, letters := Unzip(pairs) // [1,2,3], ["a","b","c"]

func Zip

1func Zip(a, b []any) [][2]any
source

Zip combines two slices into a slice of pairs. If the slices have different lengths, extra elements from the longer slice are ignored.

Example:

Example
1a := []any{1, 2, 3}
2b := []any{"a", "b", "c"}
3result := Zip(a, b) // [[1,"a"], [2,"b"], [3,"c"]]

Types 3

type Mapper

func
1type Mapper func(any) any
source

Mapper is a function type that maps an element to another element.

type Predicate

func
1type Predicate func(any) bool
source

Predicate is a function type that evaluates a condition on an element.

type Reducer

func
1type Reducer func(any, any) any
source

Reducer is a function type that reduces two elements to a single value.

Source Files 3