ops

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2023 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

This package contains functions intended for use with iter.Fold and iter.Map.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Add added in v0.3.0

func Add[T constraints.Integer | constraints.Float | ~string](a, b T) T

Add performs the `+` operation for the two inputs, returning the result.

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/go-functional/iter"
	"github.com/BooleanCat/go-functional/iter/ops"
)

func main() {
	total := iter.Fold[int](iter.Lift([]int{1, 2, 3}), 0, ops.Add[int])

	fmt.Println(total)
}
Output:

6

func BitwiseAnd added in v0.5.0

func BitwiseAnd[T constraints.Integer](a, b T) T

BitwiseAnd performs the `&` operation for the two inputs, returning the result.

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/go-functional/iter"
	"github.com/BooleanCat/go-functional/iter/ops"
)

func main() {
	overlap := iter.Fold[int](iter.Lift([]int{5, 7, 13}), -1, ops.BitwiseAnd[int])

	fmt.Println(overlap)
}
Output:

5

func BitwiseOr added in v0.5.0

func BitwiseOr[T constraints.Integer](a, b T) T

BitwiseOr performs the `|` operation for the two inputs, returning the result.

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/go-functional/iter"
	"github.com/BooleanCat/go-functional/iter/ops"
)

func main() {
	union := iter.Fold[int](iter.Lift([]int{1, 2, 6}), 0, ops.BitwiseOr[int])

	fmt.Println(union)
}
Output:

7

func BitwiseXor added in v0.5.0

func BitwiseXor[T constraints.Integer](a, b T) T

BitwiseXor performs the `^` operation for the two inputs, returning the result.

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/go-functional/iter"
	"github.com/BooleanCat/go-functional/iter/ops"
)

func main() {
	result := iter.Fold[int](iter.Lift([]int{1, 2, 6}), 0, ops.BitwiseXor[int])

	fmt.Println(result)
}
Output:

5

func Multiply added in v0.5.0

func Multiply[T constraints.Integer | constraints.Float](a, b T) T

Multiply performs the `*` operation for the two inputs, returning the result.

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/go-functional/iter"
	"github.com/BooleanCat/go-functional/iter/ops"
)

func main() {
	product := iter.Fold[int](iter.Lift([]int{3, 4, 5}), 2, ops.Multiply[int])

	fmt.Println(product)
}
Output:

120

func Passthrough added in v0.5.0

func Passthrough[T any](t T) T

Passthrough returns the provided value.

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/go-functional/iter"
	"github.com/BooleanCat/go-functional/iter/ops"
)

func main() {
	numbers := iter.Map[int](iter.Lift([]int{1, 2, 3}), ops.Passthrough[int])

	fmt.Println(numbers.Collect())
}
Output:

[1 2 3]

func UnwrapOption

func UnwrapOption[T any](o option.Option[T]) T

UnwrapOption calls Unwrap on an option.Option.

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/go-functional/iter"
	"github.com/BooleanCat/go-functional/iter/ops"
	"github.com/BooleanCat/go-functional/option"
)

func main() {
	options := iter.Lift([]option.Option[int]{
		option.Some(4),
		option.Some(6),
		option.Some(-1),
	})

	numbers := iter.Map[option.Option[int]](options, ops.UnwrapOption[int])

	fmt.Println(numbers.Collect())
}
Output:

[4 6 -1]

func UnwrapResult

func UnwrapResult[T any](r result.Result[T]) T

UnwrapResult calls Unwrap on a result.Result.

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/go-functional/iter"
	"github.com/BooleanCat/go-functional/iter/ops"
	"github.com/BooleanCat/go-functional/result"
)

func main() {
	results := iter.Lift([]result.Result[int]{
		result.Ok(1),
		result.Ok(3),
		result.Ok(-6),
	})

	numbers := iter.Map[result.Result[int]](results, ops.UnwrapResult[int])

	fmt.Println(numbers.Collect())
}
Output:

[1 3 -6]

Types

This section is empty.

Jump to

Keyboard shortcuts

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