Quick Start
Installation
To install this library, simply use your favorite package manager, here we use pure pip.
Then, import the library:
Now you could use the utilities this library provides. For more information and examples, see the api documentation.
Using monads
Option
Options refers to a value which can be a None. Instead of using Python's Optional[T], monad_std.Option[T]
provides a wrapped nullable value, which is also capable of representing a Option::Some(None) that Python's current
solution cannot. To use this, simply import it:
And create value like this:
For more information, see the documentation: monad-std: Option.
Homework
1. Write a function which accepts a possibly empty number, and returns its square root if it exists.
Key
2. Write a function that accepts two nullable number and returns their sum.
If one of the number is null, use 0 as a fallback.
Key
Result
Results refers to a result of something, possibly a successful value or an error. Both value can be any type.
Instead of using Python's exceptions and try-catch patterns, this can force you to check all potential exceptions and
error, and throw them explicitly. You can use this just like the Option:
Values can be created via both Result's static methods and Err or Ok.
For more information, see the documentation: monad-std: Result.
Note: Results can be transformed into Options via the Result.ok and Result.err method.
Note: You can catch a Python styled exception by using Result.catch or Result.catch_from static methods, which
accept a function call and return a result, containing the raised exception:
Using Advanced Iterators
The iterator can be used through the IterMeta abstract class(as an entry point):
An iterator can be constructed by passing any iterator or iterable objects, or a single value(once):
The iterator is lazily calculated, so call collect if you want to get the result:
IterMeta.collect_list: collect everything in the iterator into a list, stopping at the firstOption::None.IterMeta.collect_tuple: collect everything in the iterator into a tuple, stopping at the firstOption::None.IterMeta.collect_string: joining the iterator into a string, calling__str__and falling back to__repr__, stopping at the firstOption::None.IterMeta.collect_array: collect the iterator intofunct.Array.functis another library which enhanced the Python's builtin list. If you need to use this functionality, you should first install that lib.
For more information, see the documentation: monad-std: Iterator.
Homework
1. Write a function that accepts a list of positive integers, and returns those integers that can be devided exactly by 3 and 5.
Key
2. Write a function that accepts a list of positive integers, returns a list with flattened items of the intenger and its double.
Example:
Input: range(10)
Output: [0, 0, 1, 2, 2, 4, 3, 6, 4, 8, 5, 10, 6, 12, 7, 14, 8, 16, 9, 18]