ffp

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2023 License: MIT Imports: 3 Imported by: 0

README

ffp - Frivolous Functional Programming

ffp is a Go library that adds functional types and functions to the Go language. It is designed to make functional programming in Go more fun. I called it Frivolous because I like idiomatic go and don't thing functional concepts should be used most of the time. I made this library primarily for the fun of it.

Installation

To install ffp, use go get:

go get github.com/Solidsilver/ffp

Usage

Here's an example of how to use ffp:

package main

import (
    "fmt"
    "github.com/Solidsilver/ffp"
)

func main() {
    // Create a new list.
    xs := []int {1, 2, 3, 4, 5}

    xsm := ffp.Map(xs, func(val int) int {
        return val * 2
    })

    // Print the result.
    fmt.Println(xsm)
    // []int{2, 4, 6, 8, 10}

    // Filter over the list.
    ys := xs.Filter(func(x int) bool {
        return x > 3
    })

    // Print the result.
    fmt.Println(ys)
    // []int{4, 5}
}

Contributing

Contributions are welcome! If you find a bug or have a feature request, please open an issue on GitHub. If you want to contribute code, please fork the repository and submit a pull request.

License

This library is licensed under the MIT License. See the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Every

func Every[T any](vals []T, f func(T) bool) bool

Every is a generic function that checks if all the elements in the input slice `vals` satisfy the provided condition `f`. The function `f` should return true if the element meets the condition, and false otherwise. It returns true if all elements satisfy the condition, and false otherwise.

func Filter

func Filter[T any](vals []T, f func(T) bool) []T

Filter is a generic function that filters the input slice `vals` based on the provided filtering function `f`. The function `f` should return true for elements that need to be included in the filtered slice. It returns a new slice containing the filtered elements.

func ForEach

func ForEach[T any](vals []T, f func(T))

ForEach runs thge given function for each element in the array

func Map

func Map[T any, V any](vals []T, f func(T) V) []V

Map is a generic function that maps the input slice `vals` to another type using the provided mapping function `f`. The function `f` should return a value of the desired output type for each input element. It returns a new slice containing the mapped elements.

func MapConcurrent

func MapConcurrent[T any, V any](vals []T, f func(T) V) []V

MapConcurrent applies a function to each value in the input slice concurrently and returns a slice of the results. Note: the mapped result can be out of order of the original

func MapOrEmpty

func MapOrEmpty[T any](vals []Result[T]) [](T)

MapOrEmpty takes in a slice of Results of type T. It calls .OrEmpty on each element, and returns a slice of the resulting values.

func OnlyErr

func OnlyErr[T any, V any](f func(T) (V, error)) func(T) error

OnlyErr changes the return type of the function to only return an error

Types

type Mapper

type Mapper[T any, V any] Result[T]

func (Mapper[T, V]) Map

func (m Mapper[T, V]) Map(fMap MapperFn[T, V]) Result[V]

type MapperFn

type MapperFn[T any, V any] func(T) (V, error)

type Option

type Option[T any] struct {
	// contains filtered or unexported fields
}

func None

func None[T any]() Option[T]

func Some

func Some[T any](val T) Option[T]

func (Option[T]) Get

func (o Option[T]) Get() (T, bool)

func (Option[T]) IsNone

func (o Option[T]) IsNone() bool

func (Option[T]) IsSome

func (o Option[T]) IsSome() bool

func (Option[T]) OrElse

func (o Option[T]) OrElse(val T) T

func (Option[T]) OrEmpty

func (o Option[T]) OrEmpty() T

type Result

type Result[T any] struct {
	// contains filtered or unexported fields
}

Result is a generic struct that holds a value or an error.x A result can be checked using the `IsOk()` and `IsErr()` functions. A result can be unwrapped with `Error()` and `Get()` functions.

func Call

func Call[T any, V any](f func(T) (V, error), val T) Result[V]

Calls the given function with the given argument and returns a Result

func Err

func Err[T any](err error) Result[T]

Err creates a new Result of type T with the given error

func MapConcurrentResult

func MapConcurrentResult[T any, V any](vals []T, f func(T) (V, error)) []Result[V]

MapConcurrentResult is similar to MapConcurrent but expects the function to return a value and an error. The results are returned as a slice of ConcurrentRunResult.

func MapResult

func MapResult[T any, V any](vals []T, f func(T) (V, error)) []Result[V]

MapResult is a generic function that maps the input slice `vals` to another type using the provided mapping function `f`. The function `f` should return a value of the desired output type for each input element, or an error. MapResult returns a new slice of Results of the given output type.

func NewResult

func NewResult[T any](val T, err error) Result[T]

func Ok

func Ok[T any](val T) Result[T]

Ok creates a new Result of type T with the given value

func Try

func Try[T any](f func() (T, error)) Result[T]

Try runs the given function that possibly returns an error. It then returns a Result of the returned values

func (Result[T]) Error

func (r Result[T]) Error() error

func (Result[T]) ErrorIs

func (r Result[T]) ErrorIs(otherErr error) bool

func (Result[T]) Get

func (r Result[T]) Get() (T, error)

func (Result[T]) IfNotError

func (r Result[T]) IfNotError(f func(T) error, ignore ...error) Result[T]

IfNotError returns an error result if the previous result was an error. Otherwise, it returns a Result with the input value. IfNotError also takes in an optional array of errors to ignore.

func (Result[T]) IsErr

func (r Result[T]) IsErr() bool

func (Result[T]) IsOk

func (r Result[T]) IsOk() bool

func (Result[T]) OrElse

func (r Result[T]) OrElse(val T) T

OrElse returns the Result value if the result is Ok, otherwise it will return the passed in value

func (Result[T]) OrEmpty

func (r Result[T]) OrEmpty() T

OrElse returns the Result value if the result is Ok, otherwise it will return the default value for the given type

func (Result[T]) ThenCall

func (r Result[T]) ThenCall(f func(T) (T, error)) Result[T]

ThenCall returns an error result if the previous result was an error. Otherwise, it returns the the output of the function as a Result.

func (Result[T]) ThenCallResult

func (r Result[T]) ThenCallResult(f func(T) Result[T]) Result[T]

func (Result[T]) ThenTry

func (r Result[T]) ThenTry(f func() (T, error)) Result[T]

ThenTry returns an error result if the previous result was an error. Otherwise, it returns the the output of the function as a Result.

Jump to

Keyboard shortcuts

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