def come_out_roll(dice, status): d = dice() if sum(d) in (7, 11): return Just(("win", sum(d), [d])) elif sum(d) in (2, 3, 12): return Just(("lose", sum(d), [d])) return Just(("point", sum(d), [d]))
def read_rest(file, data): # file, data = file_data[0], file_data[1] txt = file.readline().rstrip() if txt: row = float * List(*txt.split("\t")) return Just(data + [list(row)]) >> read_rest(file) return Just(data)
def come_out_roll(dice, status): d = dice() if sum(d) in (7, 11): return Just(('win', sum(d), [d])) elif sum(d) in (2, 3, 12): return Just(('lose', sum(d), [d])) else: return Just(('point', sum(d), [d]))
def point_roll(dice, status): prev, point, so_far = status if prev != "point": return Just(status) d = dice() if sum(d) == 7: return Just(("craps", point, so_far + [d])) elif sum(d) == point: return Just(("win", point, so_far + [d])) return Just(("point", point, so_far + [d])) >> point_roll(dice)
def point_roll(dice, status): prev, point, so_far = status if prev != 'point': return Just(status) d = dice() if sum(d) == 7: return Just(('craps', point, so_far+[d])) elif sum(d) == point: return Just(('win', point, so_far+[d])) else: return ( Just(('point', point, so_far+[d])) >> point_roll(dice) )
def _run_pip_install(directory: Path): if _requires_pip_install(directory): with ChangeDir(directory): print(f'Running pip install in {directory}') execute_shell_command( 'pip3 install -r requirements.txt -t . >> /dev/null') return Just(directory)
def hasIdentity(self): """ """ for x in range(self.ORDER): if (self.isIdentity(self.__class__(x))): if (self.__identity == Nothing): self.__identity = Just(x) return True return False
def anscombe(): """ >>> d= anscombe() >>> d[0] [10.0, 8.04, 10.0, 9.14, 10.0, 7.46, 8.0, 6.58] >>> d[-1] [5.0, 5.68, 5.0, 4.74, 5.0, 5.73, 8.0, 6.89] """ with open("Anscombe.txt") as source: data = Just([]) >> read_header(source) >> read_rest(source) data = data.getValue() return data
def craps(dice): """ >>> def seven(): ... return (3,4) >>> craps( seven ) ('win', 7, [(3, 4)]) >>> rolls= [(3,3), (2,2), (3,3)] >>> def fixed(): ... global rolls ... head, *tail = rolls ... rolls= tail ... return head >>> craps( fixed ) ('win', 6, [(3, 3), (2, 2), (3, 3)]) """ outcome = (Just(("", 0, [])) >> come_out_roll(dice) >> point_roll(dice)) print(outcome.getValue())
def _create_dist_and_copy_files(directory: Path): dist_path = _get_dist_path(directory) copytree(directory, dist_path) return Just(dist_path)
def _run_zip(directory: Path): with ChangeDir(directory): zip_name = f'{directory.parent.name}.zip' execute_shell_command(f'zip -r {zip_name} . >> /dev/null') return Just(directory)
def check_stack_requirements(stack_data: StackData): # also: check whether bucket exists? does require boto3 dependency return get_as_path('.') >> partial(_check_template_exists, stack_data) >> Just(stack_data)
def check_requirements(lambda_dir): return Just( lambda_dir ) >> _check_root_dir >> _check_dir_has_sub_dirs >> _check_dirs_contain_python
from pymonad import Just, List, Nothing, curry @curry def add(x, y): return x + y def add10(x): return add * Just(10) & x print(add10(List(1, 2, 3, 4, 5, 6))) print(add10(Just(33)))
def _get_prefix(stack_data: StackData): return Just( stack_data.bucket_prefix) if stack_data.bucket_prefix else Just( stack_data.stack_name)
""" fp_task4 just_nothing_list """ from pymonad import Just, List, Nothing, curry @curry def add(x, y): return x + y @curry def add10(x): return add() * Just(10) & x print(add10(List(12, 56, 0, 3))) print(add10(Just(8))) print(add10()(Nothing))
""" fp_task5 """ from pymonad import List, Just, Nothing, Maybe # посадка птиц на левую сторону to_left = lambda num: lambda x: Nothing if abs( (x[0] + num) - x[1]) > 4 else Just((x[0] + num, x[1])) # посадка птиц на правую сторону to_right = lambda num: lambda x: Nothing if abs( (x[1] + num) - x[0]) > 4 else Just((x[0], x[1] + num)) # банановая кожура banana = lambda x: Nothing # отображение результата def show(Maybe): if Maybe == Nothing: return print("Will Fall") else: falled, pole = Maybe.getValue() return print( "Will not Fall, birds on l.side:{}, birds on r.side:{}".format( falled, pole)) begin = lambda: Just((0, 0))
def get_as_path(value): return Just(Path(value))
def get_maybe(cls, *args): try: return Just(super().get(*args)) except super().DoesNotExist as e: return Nothing
def craps(): return ( Just(('', 0, [])) >> come_out_roll(dice) >> point_roll(dice) )
def add10(x): return add * Just(10) & x
def read_header(file): _ = file.readline() _ = file.readline() _ = file.readline() return Just([])