code: def lambda_return(reward, value, length, discount, lambda_):
    
    timestep = tf.range(reward.shape[1].value)
    mask = tf.cast(timestep[None, :] < length[:, None], tf.float32)
    sequence = mask * reward + discount * value * (1 - lambda_)
    discount = mask * discount * lambda_
    sequence = tf.stack([sequence, discount], 2)
    return_ = tf.reverse(tf.transpose(tf.scan(
        lambda agg, cur: cur[0] + cur[1] * agg,
        tf.transpose(tf.reverse(sequence, [1]), [1, 2, 0]),
        tf.zeros_like(value[:, -1]), 1, False), [1, 0]), [1])
    return tf.check_numerics(tf.stop_gradient(return_), 'return')
docs: TD-lambda returns.