test is function(exp) >> bool.
  mapper is function(expression) >> list of subexpressions.
  returns [subexpression, ...].

def flatten_tree(test, enumerator, exp):
  """test is function(exp) >> bool.
  mapper is function(expression) >> list of subexpressions.
  returns [subexpression, ...].
  """
  return sum((flatten_tree(test, enumerator, subx) for subx in enumerator(exp)), []) if test(exp) else [exp]