fork(1) download
  1. def lift_middle_child(in_tree):
  2. (A, (B,), (C,), (D,)) = in_tree
  3.  
  4. return (C, (B,), (D,))
  5.  
  6. print lift_middle_child(('A', ('B',), ('C',), ('D',))) # could use lists too
  7.  
  8. def compose2(a,b): # might want to get this from the functional library
  9. return lambda *x: a(b(*x))
  10.  
  11. def compose(*funcs): #compose(a,b,c) = a(b(c(x))) - you might want to reverse that
  12. return reduce(compose2,funcs)
  13.  
  14. def reverse_children(in_tree):
  15. return in_tree[0:1] + in_tree[1:][::-1] # slightly cryptic, but works for anything subscriptable
  16.  
  17. lift_and_reverse = compose(reverse_children,lift_middle_child) # right most function applied first - if you find this confusing, reverse order in compose function.
  18.  
  19. print lift_and_reverse(('A', ('B',), ('C',), ('D',)))
  20.  
Success #stdin #stdout 0.01s 7768KB
stdin
Standard input is empty
stdout
('C', ('B',), ('D',))
('C', ('D',), ('B',))