Description: Functional programming library for Go 1.24+, inspired by fp-ts. Uses generic type aliases for a clean, composable API. Provides Option, Either, Result, IO, IOResult, Reader, and ReaderIOResult monads, plus optics (Lens, Prism, Traversal) for immutable data manipulation. Supports Functor, Applicative, and Monad abstractions with do-notation-style
The `ibm/fp-go` repository is a functional programming library for the Go programming language, designed to enhance code maintainability, testability, and composability. Heavily inspired by the TypeScript library `fp-ts`, it provides a comprehensive set of data types and functions that encourage a functional programming style, promoting pure functions and side-effect isolation. The library leverages Go's generics to offer a type-safe and composable API, aiming to make functional programming principles more accessible and enjoyable for Go developers.
The core purpose of `fp-go` is to provide developers with tools to write cleaner, more predictable, and easier-to-reason-about code. It achieves this by offering several key data types, or monads, that encapsulate common programming patterns. These include `Option`, representing optional values; `Either`, handling computations that can result in errors; `IO`, representing lazy computations; `IOEither`, combining lazy computations with error handling; `Reader`, for computations dependent on an environment; `ReaderIOEither`, combining environment dependency, lazy computation, and error handling; `Task`, for asynchronous computations; and `State`, for stateful computations. These monads, along with optics (Lens, Prism, Traversal) for immutable data manipulation, form the foundation of the library.
The library's main features revolve around these monadic data types and the operations that can be performed on them. It supports the fundamental Functor, Applicative, and Monad abstractions, enabling developers to transform, combine, and compose computations in a consistent and predictable manner. Key operations include `Map`, for transforming values within a context; `Chain` (also known as FlatMap), for transforming and flattening nested contexts; `Ap`, for applying a function within a context to a value within a context; `Of`, for wrapping a value in a context; and `Fold`, for extracting a value from a context. These operations are consistently named and behave similarly across all supported data types, promoting a unified and intuitive API.
A significant aspect of `fp-go` is its alignment with the "Zen of Go," emphasizing principles like single-purpose packages, explicit error handling, and simplicity. The library encourages the use of pure functions, which take inputs and produce outputs without side effects, making code easier to test and reason about. It also provides mechanisms for isolating side effects, such as using the `IO` monad to defer execution until necessary. The library's design promotes consistent composition, ensuring that functions are named consistently and have consistent semantics across different data types.
The repository also provides clear guidance on how to transition from idiomatic Go to a functional style. It explains how to handle errors using the `Either` monad, how to represent effectful functions using `IO` and `IOEither`, and how to manage context-dependent functions using `ReaderIOEither`. The library offers conversion functions to bridge the gap between idiomatic Go and the functional approach, making it easier for developers to adopt functional programming principles gradually.
Furthermore, the library utilizes Go generics for type safety and offers comprehensive API documentation and code samples to facilitate learning and usage. It also includes implementation notes, explaining how generics are used, the ordering of generic type parameters, and the use of the `~` operator to work with compatible types. The library is a work in progress, with ongoing efforts to minimize breaking changes and improve its features. The project is open-source and welcomes contributions, encouraging developers to submit issues and pull requests to help improve the library.
Fetching additional details & charts...