def test_unit_maybe(self): add_simplads = SM.add_simplads({ 's1': MS(), 's2': MS(), 's3': MS(), 's4': MS() }) order = SM.set_simplad_order(['s1','s2','s3','s4']) sm = (F() >> add_simplads >> order)(SM.make()) res = SM.unit(sm)(4) expect(res).to_equal(((((4, MaybeType.has_value), MaybeType.has_value), MaybeType.has_value), MaybeType.has_value))
def set_simplads(self, simplads): for key, simplad in simplads.iteritems(): self.sm = SimpladMonad.push_simplad( simplad=simplad, key=key )(self.sm) return self
def test_unit_list(self): add_simplads = SM.add_simplads({ 'm1': MS(), 's1': ListSimplad(), 's2': ListSimplad(), 's3': ListSimplad(), 's4': ListSimplad(), }) order = SM.set_simplad_order(['s1','s2','s3','s4','m1']) sm = (F() >> add_simplads >> order)(SM.make()) val = [[[[1,2]],[[1]]],[[[4]]]] res = SM.unit(sm)(val) expect(res).to_equal( ([([([([(1, MaybeType.has_value), (2, MaybeType.has_value)], None)], None), ([([(1, MaybeType.has_value)], None)], None)], None), ([([([(4, MaybeType.has_value)], None)], None)], None)], None))
def test_box_four(a): add_simplads = SM.add_simplads({ 's1': ListSimplad(), 's2': ListSimplad(), 's3': ListSimplad(), 's4': ListSimplad()}) order = SM.set_simplad_order(['s4','s3','s2','s1']) sm = (F() >> add_simplads >> order)(SM.make()) boxed = SM.get_box(sm)(lambda x: x)( BindArgs(bound=SimpladResult(val=8, delta_map={'s2': MaybeType.no_value}), deltas=[])) expect(boxed).to_equal( BindArgs(bound=8, deltas=[ WrappedDelta(type=DeltaType.default, delta=None), WrappedDelta(type=DeltaType.configured, delta=MaybeType.no_value), WrappedDelta(type=DeltaType.default, delta=None), WrappedDelta(type=DeltaType.default, delta=None)]))
def test_pushing_simplad(self): add_maybe = SM.push_simplad(simplad=MS()) add_log = SM.push_simplad(simplad=LogSimplad()) sm1 = (F() >> add_maybe >> add_maybe >> add_log)(SM.make()) add_simplads = SM.add_simplads({ '1': MS(), '2': MS(), '3': LogSimplad(), }) order = SM.set_simplad_order(['1','2','3']) sm2 = (F() >> add_simplads >> order)(SM.make()) expect(SM.unit(sm1)(4)).to_equal(SM.unit(sm2)(4))
def test_bind_maybe(self): add_simplads = SM.add_simplads({ '1': MS(), '2': MS(), '3': MS(), '4': MS(), '5': MS() }) order = SM.set_simplad_order(['1','2','3','4','5']) sm = (F() >> add_simplads >> order)(SM.make()) val = 4 bound = SM.unit(sm)(val) def double_simplad_result(i): return SimpladResult(val=i*2, delta_map={}) bound = SM.bind(double_simplad_result)( (sm, BindArgs(bound=bound, deltas=[]))) bound = SM.bind(double_simplad_result)(bound) bound = SM.bind(double_simplad_result)(bound) bound = SM.bind(double_simplad_result)(bound) bound = SM.bind(double_simplad_result)(bound) bound = SM.bind(double_simplad_result)(bound) bound = SM.bind(double_simplad_result)(bound) expect(bound[1].bound).to_equal( Bound(unbound=Bound(unbound=Bound(unbound=Bound(unbound=Bound( unbound=512, annotation=MaybeType.has_value), annotation=MaybeType.has_value), annotation=MaybeType.has_value), annotation=MaybeType.has_value), annotation=MaybeType.has_value))
def test_fm(self): func = SM.get_four() expect(func).to_equal(4)
def test_bind_with_fail(self): add_simplads = SM.add_simplads({ '1': MS(), '2': MS(), '3': ListSimplad(), '4': ListSimplad(), '5': MS() }) order = SM.set_simplad_order(['1','2','3','4','5']) sm = (F() >> add_simplads >> order)(SM.make()) val = [[4]] bound = SM.unit(sm)(val) def double_simplad_result(i): return SimpladResult(val=i*2, delta_map={}) def double_simplad_result_fail_2(i): return SimpladResult(val=i*2, delta_map={ '5': MaybeType.no_value }) bound = SM.bind(double_simplad_result)( (sm, BindArgs(bound=bound, deltas=[]))) bound = SM.bind(double_simplad_result)(bound) bound = SM.bind(double_simplad_result)(bound) bound = SM.bind(double_simplad_result_fail_2)(bound) bound = SM.bind(double_simplad_result)(bound) bound = SM.bind(double_simplad_result)(bound) bound = SM.bind(double_simplad_result)(bound) expect(bound[1].bound).to_equal(Bound(unbound=None, annotation=MaybeType.no_value))
def __init__(self): self.sm = SimpladMonad.make() self.init_deltas = {}
def run(self, func): bind_args = BindArgs(bound=self.bound, deltas=[]) self.sm, bind_args = SimpladMonad.bind(func)((self.sm, bind_args)) self.bound = bind_args.bound return self.bound
def pipe(self, funcs): bind_args = BindArgs(bound=self.bound, deltas=[]) for func in funcs: self.sm, bind_args = SimpladMonad.bind(func)((self.sm, bind_args)) self.bound = bind_args.bound return self.bound
def unit(self, unbound=None): self.bound = SimpladMonad.unit(self.sm)(unbound) self.run(SimpladBundle.delta_map(self.init_deltas)) return self
def add_error(self): self.sm = SimpladMonad.push_simplad( key='error', simplad=ErrorSimplad())(self.sm) return self
def add_reader(self, obj): self.sm = SimpladMonad.push_simplad( key='reader', simplad=ReaderSimplad())(self.sm) self.init_deltas['reader'] = ReaderDeltaMaker.set_obj(obj) return self