Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Help using ruby enumerator idioms
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Mischa Fierer  
View profile  
 More options Jan 2 2009, 4:46 am
Newsgroups: comp.lang.ruby
From: Mischa Fierer <f.mis...@gmail.com>
Date: Fri, 2 Jan 2009 04:46:14 -0500
Local: Fri, Jan 2 2009 4:46 am
Subject: Re: Help using ruby enumerator idioms

> But here inner_acc is always identical to acc, making the inject just
> for show.  You're back to using #each.

> I guess the moral of the story is that functional style in ruby
> realistically applies only to flat arrays and hashes.  Once we reach
> the hash-of-hashes realm, imperative constructs are more suitable.

> Lazy evaluation is needed to be efficient, recursive, and purely
> functional all at the same time.  (See Haskell.)

Interesting, thanks a ton for the reply.

I think you may be right that for hashes of hashes imperative style Ruby
starts to make more sense.

This has been hitting me a lot lately, as one of the projects I'm
working on has a lot of hashes of hashes (data crunching). I used the
inject style a few times, but as you note, it becomes mostly just for
show.

M

--
Posted via http://www.ruby-forum.com/.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mike Gold  
View profile  
 More options Jan 2 2009, 3:34 am
Newsgroups: comp.lang.ruby
From: Mike Gold <mike.gold.4...@gmail.com>
Date: Fri, 2 Jan 2009 03:34:09 -0500
Local: Fri, Jan 2 2009 3:34 am
Subject: Re: Help using ruby enumerator idioms

Yes, I do the same.  In a broader sense, functional style is often
shorter and less error-prone.  Map, inject, select are just examples
of the functional idiom smuggled into ruby.

In this case, a hash-of-hash merge function is needed to reorder keys
in a clean way.

#
# hoh_merge(
#   { :x => { :y => :foo } },
#   { :x => { :z => :bar } }
# )
# #=> {:x=>{:y=>:foo, :z=>:bar}}
#
def hoh_merge(a, b)
  b.inject(a) { |acc, (key, inner_hash)|
    acc.merge(
      key => (
        if existing = a[key]
          existing.merge(inner_hash)
        else
          inner_hash
        end
      )
    )
  }
end

data = {
  :flintstone => {
    :husband => :fred,
    :wife => :wilma,
  },
  :rubble => {
    :husband => :barney,
    :wife => :betty,
  },

}

# original imperative version

h = Hash.new { |hash, key| hash[key] = Hash.new }
data.each { |hash_key, inner_hash|
  inner_hash.each { |key, value|
    h[key][hash_key] = value
  }

}

# pure functional version

h2 = data.inject(Hash.new) { |acc, (hash_key, inner_hash)|
  inner_hash.keys.inject(acc) { |inner_acc, key|
    hoh_merge(inner_acc, key => { hash_key => inner_hash[key] })
  }

}

require 'pp'

pp h
pp h2

#=>
# {:wife=>{:rubble=>:betty, :flintstone=>:wilma},
#  :husband=>{:rubble=>:barney, :flintstone=>:fred}}
# {:wife=>{:rubble=>:betty, :flintstone=>:wilma},
#  :husband=>{:rubble=>:barney, :flintstone=>:fred}}

I wrote a purely-functional hoh_merge just for fun.  An imperative
method would be more efficient (modifying its first argument),

h2 = data.inject(Hash.new) { |acc, (hash_key, inner_hash)|
  inner_hash.keys.inject(acc) { |inner_acc, key|
    hoh_merge!(inner_acc, key => { hash_key => inner_hash[key] })
  }

}

But here inner_acc is always identical to acc, making the inject just
for show.  You're back to using #each.

I guess the moral of the story is that functional style in ruby
realistically applies only to flat arrays and hashes.  Once we reach
the hash-of-hashes realm, imperative constructs are more suitable.

Lazy evaluation is needed to be efficient, recursive, and purely
functional all at the same time.  (See Haskell.)
--
Posted via http://www.ruby-forum.com/.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mischa Fierer  
View profile  
 More options Jan 1 2009, 7:39 am
Newsgroups: comp.lang.ruby
From: Mischa Fierer <f.mis...@gmail.com>
Date: Thu, 1 Jan 2009 07:39:04 -0500
Local: Thurs, Jan 1 2009 7:39 am
Subject: Help using ruby enumerator idioms
Hi,

In Ruby, when I see something like:

h = []

x.each do

..

end

h

Usually means that I can refactor it to use map, inject, select, or
reject.

However, I am unsure how to do this for the code below:

         h = Hash.new {|hash, key| hash[key] = {}}

   some_hash_of_hashes.each do |hash_key, sub_hash|
     sub_hash.each {|key, value|   h[key][hash_key] = value }
   end

   h

Let me know if anyone has any ideas.

M
--
Posted via http://www.ruby-forum.com/.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »