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

1""" 

2monadc - Comprehensive functional programming monads for Python 

3 

4A complete monad library providing Option, Either, Try, and Result types 

5for functional programming patterns, inspired by Scala and Rust. 

6 

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 

14 

15Example usage: 

16 from monadc import Option, Some, Nil, Result, Ok, Err 

17 

18 # Option with dual API 

19 user = Option("john@example.com") # Some("john@example.com") 

20 

21 # Scala-style 

22 email = user.map(str.upper).get_or_else("no-email@example.com") 

23 

24 # Rust-style 

25 email = user.and_then(lambda x: Some(x.upper())).unwrap_or("no-email@example.com") 

26 

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 

33 

34 # Result monad (Rust-style) 

35 success = Ok("data") 

36 error = Err("failed") 

37 value = success.unwrap_or("default") 

38 

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

44 

45# Option monad (Scala-inspired) 

46from .option import Option, Some, Nil 

47 

48# Either monad (Scala-inspired) 

49from .either import Either, Left, Right 

50 

51# Try monad (Scala-inspired) 

52from .try_ import Try, Success, Failure 

53 

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 

58 

59__version__ = "0.1.0" 

60__author__ = "Carl You" 

61__email__ = "" 

62 

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() 

68 

69 # Either monad 

70 "Either", "Left", "Right", 

71 

72 # Try monad 

73 "Try", "Success", "Failure", 

74 

75 # Result monad 

76 "Result", "Ok", "Err", 

77 

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) 

82 

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]