def if_else(self, if_: Filter[X], then: Function[X, Y], else_: Function[X, Y] = identity) -> 'Stream[Y]': """ if "if_" returns True then elements are transformed according to "then" otherwise else_ function is used. This method is the special case of "conditional" method. "else_" has default value "identity" which return element as it is in case "if_" fails; Example: Stream(range(10)).condition(lambda x: 3 <= x <= 7, lambda x: 1 , lambda x: 0).collect(ToList()) -> [0, 0, 0, 1, 1, 1, 1, 1, 0, 0] :param if_: :param then: :param else_: by default it returns element as it is. :return: Stream itself """ return self.map(ChainedCondition.if_else(if_, then, else_))
def test_map7(self): rnd = self.rnd a, b, size = 1, 100, 50 cc = (ChainedCondition.if_else(lambda x: x < 50, lambda x: x**2, lambda x: x % 50)) out = Stream(rnd.int_range_supplier(a, b)) \ .limit(size) \ .conditional(cc) \ .reduce(op.mul, 1) \ .get() rnd.reset() data = (cc(e) for e in rnd.int_range(a, b, size=size)) out_target = reduce(op.mul, data) self.assertEqual(out, out_target)