Skip to content

fix: add pipe and sequencing operators to Maybe monad#43

Merged
dbrattli merged 1 commit intomasterfrom
fix/maybe-pipe-operator
Nov 16, 2025
Merged

fix: add pipe and sequencing operators to Maybe monad#43
dbrattli merged 1 commit intomasterfrom
fix/maybe-pipe-operator

Conversation

@dbrattli
Copy link
Copy Markdown
Owner

Summary

Fixes #32

Adds | (pipe) and >> (sequencing) operators to the Maybe monad (Just and Nothing classes), enabling more readable monadic composition.

Problem

The Maybe monad had a bind method but lacked the operator overloading that makes monadic composition more readable. Users attempting to use the pipe operator would get:

Just(5) | (lambda x: Just(x + 1))
# TypeError: unsupported operand type(s) for |: 'Just' and 'function'

Solution

Added two operators to both Just and Nothing classes:

1. Pipe operator (|) - Monadic bind

Provides syntactic sugar for the bind method:

# Before (still works)
Just(5).bind(lambda x: Just(x + 1))

# After (new syntax)
Just(5) | (lambda x: Just(x + 1))  # Returns Just(6)

# Chained operations
Just(5) | (lambda x: Just(x + 1)) | (lambda x: Just(x * 2))  # Just(12)

# The exact example from issue #32 now works
Just(5) | (lambda x: Just(x + 1) | (lambda x: Just(x * 2) | (lambda x: Just(x - 4))))  # Just(8)

2. Sequencing operator (>>) - Then combinator

Executes two monadic actions in sequence, discarding the first result:

Just(5) >> Just(10)  # Returns Just(10)
Nothing() >> Just(10)  # Returns Nothing()

Implementation Details

  • Added __or__ method to Just (delegates to bind)
  • Added __or__ method to Nothing (always returns Nothing)
  • Added __rshift__ method to Just (sequences, discarding first value)
  • Added __rshift__ method to Nothing (always returns Nothing)

Testing

Added 16 comprehensive tests in TestMaybeOperators class:

  • ✅ 7 tests for | operator (simple, chained, nested, with Nothing)
  • ✅ 7 tests for >> operator (simple, chained, discarding behavior)
  • ✅ 2 tests for operator combinations
  • ✅ All tests verify equivalence to existing bind behavior

Results

  • ✅ All 198 tests pass (16 new + 182 existing)
  • ✅ Type checking passes with zero errors (strict mode)
  • ✅ Maybe coverage improved: 52% → 86%
  • ✅ API consistency with List and other monads
  • ✅ Full backward compatibility maintained

Consistency

These operators match the existing implementation in the List monad, providing a consistent API across all monadic types in OSlash.

Test Plan

# Run all tests
uv run pytest -v

# Run just the new tests
uv run pytest tests/test_maybe.py::TestMaybeOperators -v

# Type check
uv run pyright oslash/maybe.py

🤖 Generated with Claude Code

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>
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.

Operand Type | error

1 participant