On Fri, 3 Feb 2006, Jacob Fugal wrote:

> On 2/2/06, ara.t.howard / noaa.gov <ara.t.howard / noaa.gov> wrote:
>>    require "csv"
>>    require "yaml"
>>
>>    path = ARGV.shift
>>    sum = Hash::new{|h,k| h[k] = 0}
>>    count = lambda{|row| sum[row.last.to_s[0,6]] += 1}
>>    CSV::open(path,"r"){|row| count[row]}
>>    y sum.delete_if{|k,v| v == 1}
>
> I'm curious why you decided to make `count` its own lambda when:
>
>  1) It's only ever used once
>  2) The block that uses it has only one statement, namely the call to `count`
>  3) count and the block to CSV::open have the same signature

it's for abstraction only.  i wrote how to count before writing the csv open
line.  when i wrote it ended up with something like

   CSV::open(path,"r"){|row| p row; count[row]}

during editing - as i always seem to for debugging ;-)

basically i find

   {{{{}}}}

tough to read sometimes and factor out things using lambda.  it's rare that it
acutally ends up being the the only thing left as in this case - but here you
are quite right that it can be compacted.

> I think at a minimum, given 2) and 3), I'd just replace the block to
> CSV::open with count itself:

>
>  count = lambda{|row| sum[row.last.to_s[0,6]] += 1}
>  CSV::open(path,"r", &count)
>
> Then, since count isn't used anywhere else, I'd join those together:
>
>  CSV::open(path,"r"){|row| sum[row.last.to_s[0,6]] += 1}

but i disagree here.  people, esp nubies will look at that and say - what?
whereas reading

   count = lambda{|row| sum[row.last.to_s[0,6]] += 1}

   ... count[row] ...

is pretty clear.  i often us variable as comments to others and myself.  eg.
what does this do:

   password = "#{ sifname }_#{ eval( ((0...256).to_a.map{|c| c.chr}.sort_by{rand}.select{|c| c =~ %r/[[:print:]]/})[0,4].join.inspect ) }"

hard to say huh?

how about this?

   four_random_printable_chars = eval( ((0...256).to_a.map{|c| c.chr}.sort_by{rand}.select{|c| c =~ %r/[[:print:]]/})[0,4].join.inspect )
   password = "#{ sifname }_#{ four_random_printable_chars }"

ugly (yes i'm hacking like crazy today) but at least anyone reading it (most
importantly me) knows what i'm trying to do if not how!

anyhow - same goes with 'count': it's all good until you start cutting and
pasting - then you want vars not wicked expressions to move around.

> Just seems a little clearer to me over having an extra one-time use lambda.

__iff__ you are good at reading ruby ;-)

cheers.

-a

-- 
happiness is not something ready-made.  it comes from your own actions.
- h.h. the 14th dali lama