fix: add pipe and sequencing operators to Maybe monad#43
Merged
Conversation
Fixes #32 Add __or__ and __rshift__ operators to Just and Nothing classes to enable pipe-based monadic composition and sequencing. Changes: - Add __or__ operator to Just and Nothing for pipe syntax (|) - Add __rshift__ operator to Just and Nothing for sequencing (>>) - Add 16 comprehensive tests covering all operator combinations - Improve Maybe test coverage from 52% to 86% This enables more readable monadic chains: Just(5) | (lambda x: Just(x + 1)) # Returns Just(6) Just(5) >> Just(10) # Returns Just(10) The operators provide syntactic sugar for the existing bind method, maintaining consistency with the List monad and other monadic types. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This was referenced Nov 16, 2025
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
Fixes #32
Adds
|(pipe) and>>(sequencing) operators to theMaybemonad (JustandNothingclasses), enabling more readable monadic composition.Problem
The
Maybemonad had abindmethod but lacked the operator overloading that makes monadic composition more readable. Users attempting to use the pipe operator would get:Solution
Added two operators to both
JustandNothingclasses:1. Pipe operator (
|) - Monadic bindProvides syntactic sugar for the
bindmethod:2. Sequencing operator (
>>) - Then combinatorExecutes two monadic actions in sequence, discarding the first result:
Implementation Details
__or__method toJust(delegates tobind)__or__method toNothing(always returnsNothing)__rshift__method toJust(sequences, discarding first value)__rshift__method toNothing(always returnsNothing)Testing
Added 16 comprehensive tests in
TestMaybeOperatorsclass:|operator (simple, chained, nested, with Nothing)>>operator (simple, chained, discarding behavior)bindbehaviorResults
Listand other monadsConsistency
These operators match the existing implementation in the
Listmonad, providing a consistent API across all monadic types in OSlash.Test Plan
🤖 Generated with Claude Code