Add luarrow.monad module with Identity, Maybe, and Either monads #11
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview
This PR implements the
luarrow.monadmodule, adding support for monadic programming with three core monads: Identity, Maybe, and Either. The module provides Haskell-inspired monadic operations with Lua's operator overloading for elegant composition.What's New
Three Monad Implementations
1. Identity Monad
The simplest monad that wraps values in a computational context, enabling applicative-style function composition:
Note:
pureis available as an alias foridentityfor backward compatibility.2. Maybe Monad
Represents optional values with
just(value)for values andnothing()for absence:3. Either Monad
Represents computations that may fail with
right(value)for success andleft(error)for failures:How It Works
Each monad provides:
fmap(via*operator): Map functions over wrapped valuesbind(via%operator): Chain monadic computationsor_else: Extract value with a default fallbackImplementation Details
New Files
src/luarrow/monad/identity.lua: Identity monad implementationsrc/luarrow/monad/maybe.lua: Maybe monad implementationsrc/luarrow/monad/either.lua: Either monad implementationsrc/luarrow/monad.lua: Main module exporting all three monadsspec/luarrow/monad/identity_spec.lua: Identity monad testsspec/luarrow/monad/maybe_spec.lua: Maybe monad tests (9 tests)spec/luarrow/monad/either_spec.lua: Either monad tests (11 tests)Updated Files
src/luarrow.lua: Exports all monad functions (identity/pure,just,nothing,right,left)doc/api.md: Complete API documentation for all three monads with examplesREADME.md: Added monad overview and examplesdoc/examples.md: Added practical monad usage examplesspec/luarrow/monad_spec.lua: Original tests maintained for backward compatibilitysrc/luarrow/fun.lua,src/luarrow/arrow.lua, andscripts/benchmark.luaDesign Philosophy
The implementation follows Haskell's monad conventions while maintaining luarrow's patterns:
*for fmap,%for bind)purestill works as an alias foridentityTesting
All tests pass (20+ tests total):
The test suite covers:
pureimplementationDocumentation
Complete documentation has been added following the existing style:
doc/api.mdwith detailed monad explanations and type signaturesdoc/examples.mdshowing practical use casesREADME.mdCloses
Closes #7
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.