Skip to content

Add Writer monad implementation with comprehensive tests#17

Merged
robertvansteen merged 3 commits intomainfrom
claude/add-writer-monad-xmFUF
Feb 9, 2026
Merged

Add Writer monad implementation with comprehensive tests#17
robertvansteen merged 3 commits intomainfrom
claude/add-writer-monad-xmFUF

Conversation

@robertvansteen
Copy link
Contributor

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.php with a complete implementation featuring:

    • map() - Transform the value while preserving the log
    • andThen() - Chain computations with automatic log combination
    • tell() - Append entries to the log
    • mapLog() - Transform the accumulated log
    • inspect() - Execute side effects without changing the value
    • reset() - Clear the log to a new value
    • listen() - Access both value and log in a computation
    • run() - Extract both value and log as a tuple
  • Helper Function: Added src/Writer/functions.php with a Writer() factory function that creates Writers with array-based logs using array spread for combining entries

  • Comprehensive Tests: Added tests/Writer/WriterTest.php with 11 test cases covering all operations and immutability guarantees, plus type assertion tests in tests/Writer/types.php

  • Code Quality: Applied consistent formatting across the codebase:

    • Removed unnecessary blank lines in imports
    • Standardized closure syntax (fn() vs fn ())
    • Added trailing commas in multi-line parameter lists
    • Fixed string concatenation formatting in type assertions
  • Autoload Configuration: Updated composer.json to include the new Writer functions file in autoload

Implementation 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

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
@robertvansteen robertvansteen merged commit c2cb2ba into main Feb 9, 2026
4 checks passed
@robertvansteen robertvansteen deleted the claude/add-writer-monad-xmFUF branch February 9, 2026 18:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants