xotl.tools.fp.option - Functional Programming Option Type¶
Functional Programming Option Type definition.
In Programming, and Type Theory, an option type, or maybe type, represents encapsulation of an optional value; e.g., it is used in functions which may or may not return a meaningful value when they are applied.
It consists of either a constructor encapsulating the original value x
(written Just x or Some x) or an empty constructor (called None or
Nothing). Outside of functional programming, these are known as nullable
types.
In our case option type will be the Maybe class (the equivalent of
Option in Scala Programming Language), the wrapper for valid values will
be the Just class (equivalent of Some in Scala); and the wrapper
for invalid values will be the Wrong class.
Instead of None or Nothing, Wrong is used because two reasons:
(1) already existence of None special Python value, and (2) Wrong
also wraps incorrect values and can have several instances (not only a null
value).
-
class
xotl.tools.fp.option.Maybe(*args)[source]¶ Wrapper for optional values.
The Maybe type encapsulates an optional value. A value of type
Maybe aeither contains a value of typea(represented asJust a), or it is empty (represented asNothing). Using Maybe` is a good way to deal with errors or exceptional cases without resorting to drastic measures such as error. In this implementation we make a variation where aWrongobject represents a missing (with special valueNothing) or an improper value (including errors).See descendant classes
JustandWrongfor more information.This implementation combines
MaybeandEitherHaskell data types.Maybeis a means of being explicit that you are not sure that a function will be successful when it is executed. Conventionally, the usage ofEitherfor errors usesRightwhen the computation is successful, andLeftfor failing scenarios.In this implementation, Just:class` us used for equivalence with both Haskell
JustandRighttypes;Wrongis used with the special valueNothingand to encapsulate errors or incorrect values (HaskellLeft).Haskell:
data Maybe a = Nothing | Just a either :: (a -> c) -> (b -> c) -> Either a b -> c
Case analysis for the Either type. If the value is Left a, apply the first function to a; if it is Right b, apply the second function to b.
-
classmethod
choose(*types)[source]¶ Decorator to force Maybe values constraining to expecting types.
For example, a function that return a collection (tuple or list) if valid or False if not, if not decorated could be ambiguous for an empty collection:
>>> @Just.choose(tuple, list) ... def check_range(values, min, max): ... if isinstance(values, (tuple, list)): ... return [v for v in values if min <= v <= max] ... else: ... return False >>> check_range(range(10), 7, 17) [7, 8, 9] >>> check_range(range(10), 17, 27) Just([]) >>> check_range(set(range(10)), 7, 17) False
-
classmethod
Further Notes¶
It could be thought that this kind of concept is useless in Python because the dynamic nature of the language, but always there are certain logic systems that need to wrap “correct” false values and “incorrect” true values.
Also, in functional programming, errors can be reasoned in a new way: more
like as error values than in exception handling. Where the Maybe
type expresses the failure possibility through Wrong instances
encapsulating errors.
When receiving a Wrong instance encapsulating an error, and want to
recover the exception propagation style -instead of continue in pure
functional programming-, to re-raise the exception, instead the raise
Python statement, use throw().
See https://en.wikipedia.org/wiki/Monad_%28functional_programming%29#The_Maybe_monad