On Feb 18, 9:46 pm, Steven D'Aprano <st...@remove-this- cybersource.com.au> wrote: > On Thu, 18 Feb 2010 19:57:35 -0800, Steve Howell wrote: > > The names you give to the intermediate results here are terse--"tuples" > > and "filtered"--so your code reads nicely. > > > In a more real world example, the intermediate results would be > > something like this: > > > departments > > departments_in_new_york > > departments_in_new_york_not_on_bonus_cycle > > employees_in_departments_in_new_york_not_on_bonus_cycle > > names_of_employee_in_departments_in_new_york_not_on_bonus_cycle > > Those last two could be written more concisely as: > > serfs_in_new_york > names_of_serfs_in_new_york_as_if_we_cared > > But seriously... if you have a variable called "departments_in_new_york", > presumably you also have variables called "departments_in_washington", > "departments_in_los_angeles", "departments_in_houston", > "departments_in_walla_walla", and so forth. If so, this is a good sign > that you are doing it wrong and you need to rethink your algorithm. >
Sure, but it could also be that you're launching a feature that is only temporarily limited to New York departments, and any investment in coming up with names for the New York filter function or intermediate local variables becomes pointless once you go national: # version 1 emps = [ ['Bob Rich', 'NY', 55], ['Alice Serf', 'NY', 30], ['Joe Peasant', 'MD', 12], ['Mary Pauper', 'CA', 13], ] emps.select { |name, state, salary| salary < 40 }.select { |name, state, salary| # limit bonuses to NY for now...reqs # may change! state == 'NY' }.each { |name, state, salary| new_salary = salary * 1.1 puts "#{name} gets a raise to #{new_salary}!" } # version 2 emps = [ ['Bob Rich', 'NY', 55], ['Alice Serf', 'NY', 30], ['Joe Peasant', 'MD', 12], ['Mary Pauper', 'CA', 13], ] emps.select { |name, state, salary| salary < 40 }.each { |name, state, salary| new_salary = salary * 1.1 puts "#{name} gets a raise to #{new_salary}!" } -- http://mail.python.org/mailman/listinfo/python-list