Coverage for src/monadc/__init__.py: 100%
10 statements
« prev ^ index » next coverage.py v7.10.2, created at 2025-08-19 20:24 -0700
« prev ^ index » next coverage.py v7.10.2, created at 2025-08-19 20:24 -0700
1"""
2monadc - Comprehensive functional programming monads for Python
4A complete monad library providing Option, Either, Try, and Result types
5for functional programming patterns, inspired by Scala and Rust.
7Features:
8- Option monad with dual Scala/Rust API (get/unwrap, map/and_then, etc.)
9- Either monad for error handling (Scala-inspired)
10- Try monad for exception handling (Scala-inspired)
11- Result monad for error handling (Rust-inspired)
12- Full interoperability between all monad types
13- Comprehensive utility functions and decorators
15Example usage:
16 from monadc import Option, Some, Nil, Result, Ok, Err
18 # Option with dual API
19 user = Option("john@example.com") # Some("john@example.com")
21 # Scala-style
22 email = user.map(str.upper).get_or_else("no-email@example.com")
24 # Rust-style
25 email = user.and_then(lambda x: Some(x.upper())).unwrap_or("no-email@example.com")
27 # Mixed styles work together
28 result = (Option("hello")
29 .map(str.upper) # Scala
30 .and_then(lambda x: Some(x + "!")) # Rust
31 .inspect(print) # Rust
32 .filter(lambda x: "!" in x)) # Scala
34 # Result monad (Rust-style)
35 success = Ok("data")
36 error = Err("failed")
37 value = success.unwrap_or("default")
39 # Utility functions for common patterns
40 config_val = from_dict_get(config, "timeout", 30)
41 attr_val = from_getattr(obj, "property", "default")
42 computed = from_callable(lambda: expensive_computation())
43"""
45# Option monad (Scala-inspired)
46from .option import Option, Some, Nil
48# Either monad (Scala-inspired)
49from .either import Either, Left, Right
51# Try monad (Scala-inspired)
52from .try_ import Try, Success, Failure
54# Result monad (Rust-inspired)
55from .result import Result, Ok, Err
56from .utils import from_callable, from_dict_get, from_getattr
57from .decorators import option, try_decorator, try_, result
59__version__ = "0.1.0"
60__author__ = "Carl You"
61__email__ = ""
63__all__ = [
64 # Option monad (currently available)
65 "Option", # Both type annotation and constructor: Option(x)
66 "Some", # Direct construction: Some(x)
67 "Nil", # Direct construction: Nil()
69 # Either monad
70 "Either", "Left", "Right",
72 # Try monad
73 "Try", "Success", "Failure",
75 # Result monad
76 "Result", "Ok", "Err",
78 # Utility functions for common patterns
79 "from_callable", # from_callable(lambda: func())
80 "from_dict_get", # from_dict_get(dict, key, default)
81 "from_getattr", # from_getattr(obj, attr, default)
83 # Decorators for automatic monad wrapping
84 "option", # @option - wrap returns in Option, exceptions propagate
85 "try_decorator", # @try_decorator - wrap returns in Try (Success/Failure)
86 "try_", # @try_ - alias for try_decorator (more natural usage)
87 "result", # @result - wrap returns in Result (Ok/Err)
88]