<s> """<STR_LIT>""" <EOL> from functools import wraps , partial <EOL> from operator import eq , is_not <EOL> class Option ( object ) : <EOL> def __new__ ( tp , value , checker = partial ( is_not , None ) ) : <EOL> if isinstance ( value , Option ) : <EOL> return value <EOL> return Full ( value ) if checker ( value ) else Empty ( ) <EOL> @ staticmethod <EOL> def from_value ( value ) : <EOL> return Option ( value ) <EOL> @ staticmethod <EOL> def from_call ( callback , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> exc = kwargs . pop ( "<STR_LIT>" , Exception ) <EOL> try : <EOL> return Option ( callback ( * args , ** kwargs ) ) <EOL> except exc : <EOL> return Empty ( ) <EOL> def map ( self , callback ) : <EOL> raise NotImplementedError ( ) <EOL> def filter ( self , callback ) : <EOL> raise NotImplementedError ( ) <EOL> def get_or ( self , default ) : <EOL> raise NotImplementedError ( ) <EOL> def get_or_call ( self , callback , * args , ** kwargs ) : <EOL> raise NotImplementedError ( ) <EOL> def or_else ( self , default ) : <EOL> raise NotImplementedError ( ) <EOL> def or_call ( self , callback , * args , ** kwargs ) : <EOL> raise NotImplementedError ( ) <EOL> class Full ( Option ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = "<STR_LIT:x>" , <EOL> empty = False <EOL> def __new__ ( tp , value , * args , ** kwargs ) : <EOL> if isinstance ( value , Empty ) : <EOL> return Empty ( ) <EOL> return object . __new__ ( tp ) <EOL> def __init__ ( self , value , * args ) : <EOL> self . x = value . get_or ( "<STR_LIT>" ) if isinstance ( value , Full ) else value <EOL> def map ( self , callback ) : <EOL> return Option . from_value ( callback ( self . x ) ) <EOL> def filter ( self , callback ) : <EOL> return self if callback ( self . x ) else Empty ( ) <EOL> def get_or ( self , default ) : <EOL> return self . x <EOL> def get_or_call ( self , callback , * args , ** kwargs ) : <EOL> return self . x <EOL> def or_else ( self , default ) : <EOL> return self <EOL> def or_call ( self , callback , * args , ** kwargs ) : <EOL> return self <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % self . x <EOL> __repr__ = __str__ <EOL> def __eq__ ( self , other ) : <EOL> if not isinstance ( other , Full ) : <EOL> return False <EOL> return eq ( self . x , other . x ) <EOL> class Empty ( Option ) : <EOL> """<STR_LIT>""" <EOL> __object = None <EOL> empty = True <EOL> def __new__ ( tp , * args , ** kwargs ) : <EOL> if Empty . __object is None : <EOL> Empty . __object = object . __new__ ( tp ) <EOL> return Empty . __object <EOL> def map ( self , callback ) : <EOL> return Empty ( ) <EOL> def filter ( self , callback ) : <EOL> return Empty ( ) <EOL> def get_or ( self , default ) : <EOL> return default <EOL> def get_or_call ( self , callback , * args , ** kwargs ) : <EOL> return callback ( * args , ** kwargs ) <EOL> def or_else ( self , default ) : <EOL> return Option ( default ) <EOL> def or_call ( self , callback , * args , ** kwargs ) : <EOL> return Option ( callback ( * args , ** kwargs ) ) <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" <EOL> __repr__ = __str__ <EOL> def __eq__ ( self , other ) : <EOL> return isinstance ( other , Empty ) <EOL> def optionable ( f ) : <EOL> @ wraps ( f ) <EOL> def wrapper ( * args , ** kwargs ) : <EOL> return Option ( f ( * args , ** kwargs ) ) <EOL> return wrapper </s>
