Add Writer monad implementation with comprehensive tests#17
Merged
robertvansteen merged 3 commits intomainfrom Feb 9, 2026
Merged
Add Writer monad implementation with comprehensive tests#17robertvansteen merged 3 commits intomainfrom
robertvansteen merged 3 commits intomainfrom
Conversation
Introduce the Writer monad for carrying a value alongside an accumulated log through a chain of computations. Supports array-based logs by default via the Writer() factory function, and custom log types via Writer::of() with a user-provided combiner. API: value(), log(), run(), map(), andThen(), tell(), mapLog(), inspect(), reset(), listen() https://claude.ai/code/session_01DZ1sjcUYedPJhLMyCBw7ZW
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
This PR introduces a new Writer monad to the library, enabling computations that carry values alongside accumulated logs. The Writer monad is useful for tracking side effects, audit trails, and other logging scenarios while maintaining functional purity.
Key Changes
New Writer Monad: Added
src/Writer/Writer.phpwith a complete implementation featuring:map()- Transform the value while preserving the logandThen()- Chain computations with automatic log combinationtell()- Append entries to the logmapLog()- Transform the accumulated loginspect()- Execute side effects without changing the valuereset()- Clear the log to a new valuelisten()- Access both value and log in a computationrun()- Extract both value and log as a tupleHelper Function: Added
src/Writer/functions.phpwith aWriter()factory function that creates Writers with array-based logs using array spread for combining entriesComprehensive Tests: Added
tests/Writer/WriterTest.phpwith 11 test cases covering all operations and immutability guarantees, plus type assertion tests intests/Writer/types.phpCode Quality: Applied consistent formatting across the codebase:
fn()vsfn ())Autoload Configuration: Updated
composer.jsonto include the new Writer functions file in autoloadImplementation Details
The Writer monad uses a generic combiner function to support flexible log types (arrays, strings, monoids, etc.). The implementation maintains immutability throughout all operations and properly chains logs when using
andThen(). The default array-based Writer uses array spread syntax for efficient log combination.https://claude.ai/code/session_01DZ1sjcUYedPJhLMyCBw7ZW