Coverage for src/monadc/__init__.py: 100%

1"""

2 monadc - Comprehensive functional programming monads for Python

3

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

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

6

7 Features:

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

15 Example 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)

46 from .option import Option, Some, Nil

47

48# Either monad (Scala-inspired)

49 from .either import Either, Left, Right

50

51# Try monad (Scala-inspired)

52 from .try_ import Try, Success, Failure

53

54# Result monad (Rust-inspired)

55 from .result import Result, Ok, Err

56 from .utils import from_callable, from_dict_get, from_getattr

57 from .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]