-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexamples.py
74 lines (56 loc) · 1.79 KB
/
examples.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from maybe import Maybe
from numbers import Number
from math import sqrt
from typing import Any
##################
# STRING EXAMPLE #
##################
result, err = (
Maybe("Hello, World") # Inital value of "Hello, World"
.bind(str.upper) # Apply upper to string
.bind(str.split, ",") # Apply split to string
.bind(lambda x: ", ".join(x)) # Apply lambda to join split string
.resolve_as(str) # Resolve the value as string type
)
# Print the result if success else any error that occurs
print("String example:", result or err)
# The resolve type can be set to any to prevent type casting
result, err = Maybe("Another string").bind(len).resolve_as(Any)
print("Any example type:", type(result) or err)
###################
# NUMERIC EXAMPLE #
###################
# Define a function to use
def add(x: Number, y: Number) -> Number:
return x + y
result, err = (
Maybe(5)
.bind(add, 5) # Fuctions that need an extra argument can be done like this
.bind(lambda x: add(x, 5)) # Or like this
.bind(sqrt)
.bind(pow, 4)
.resolve_as(float)
)
print("Numeric example:", result or err)
################
# LIST EXAMPLE #
################
result, err = (
Maybe([i + 1 for i in range(10)])
.bind(lambda x: map(sqrt, x))
.bind(sum)
.resolve_as(float)
)
print("List example:", result or err)
###########################
# ERROR CATCHING EXAMPLES #
###########################
# This will raise a ZeroDivisionError
result, err = Maybe(5).bind(lambda x: x / 0).resolve_as(float)
# Catch the error
if err:
print("An error has been caught and handled:", err)
# This will raise an exception when casting to int
result, err = Maybe("hello").resolve_as(int)
# Onelineer to print result or handle the error
print("Result:", result) if result else print("Error:", err)