Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > What would you guys think about adding the Thrush combinator toObject?

Reply
Thread Tools

What would you guys think about adding the Thrush combinator toObject?

 
 
Brian Maddy
Guest
Posts: n/a
 
      06-03-2011
Hello,

I'd like to get some peoples' opinion on this. I recently needed to
make a change to an enumerator that changed the number of items in the
enumerator. I wanted to be able to chain this function in like map and
inject, but it needed to operate on the enumerator, not just the items
in it. Adding functions like this to the Object or Enumerator class
seemed like poor design (since there could be many of them). I was
surprised to find out that there isn't a Thrush combinator function on
the Object class. I ended up copying the code Reganwald
(https://github.com/raganwald/homoico...arkdown#readme)
and wanted to see if anyone else thought it would be a good addition
to the Ruby language. Here's the code:

class Object
def into(expr = nil)
expr.nil? ? yield(self) : expr.to_proc.call(self)
end
end

Thoughts?


Here's my situation in more detail for those who are interested:

I had some data like this:

orig_data = [{:type => roducts, :ids => [1, 2, 3, 4, 5]}, {:type =>
:stores, :ids => [6, 7, 8, 9]}]

that I needed to split into smaller chunks, like this:

[{:type => roducts, :ids => [1, 2]},
{:type => roducts, :ids => [3, 4]},
{:type => roducts, :ids => [5]},
{:type => :stores, :ids => [6, 7]},
{:type => :stores, :ids => [8, 9]}]

My function for splitting them was pretty simple, but had to take in
an enumerator:

def slice_by_ids(hashes)
Enumerator.new do |yielder|
hashes.each do |hash|
hash[:ids].each_slice(2) do |slice|
yielder.yield hash.merge(:ids => slice)
end
end
end
end

This function seemed way too specific to put on the Enumerator class,
but if we had the Thrush combinator we could do something like this:

orig_data.into(&method(:slice_by_ids))

This is also still easily chainable and maintains laziness:

orig_data.
lazy_map(&method(:do_stuff)).
into(&method(:slice_by_ids)).
lazy_map(&method(:do_other_stuff))

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      06-03-2011
On 03.06.2011 15:30, Brian Maddy wrote:
> Hello,
>
> I'd like to get some peoples' opinion on this. I recently needed to
> make a change to an enumerator that changed the number of items in the
> enumerator. I wanted to be able to chain this function in like map and
> inject, but it needed to operate on the enumerator, not just the items
> in it. Adding functions like this to the Object or Enumerator class
> seemed like poor design (since there could be many of them). I was
> surprised to find out that there isn't a Thrush combinator function on
> the Object class. I ended up copying the code Reganwald
> (https://github.com/raganwald/homoico...arkdown#readme)
> and wanted to see if anyone else thought it would be a good addition
> to the Ruby language. Here's the code:
>
> class Object
> def into(expr = nil)
> expr.nil? ? yield(self) : expr.to_proc.call(self)
> end
> end
>
> Thoughts?
>
>
> Here's my situation in more detail for those who are interested:
>
> I had some data like this:
>
> orig_data = [{:type => roducts, :ids => [1, 2, 3, 4, 5]}, {:type =>
> :stores, :ids => [6, 7, 8, 9]}]
>
> that I needed to split into smaller chunks, like this:
>
> [{:type => roducts, :ids => [1, 2]},
> {:type => roducts, :ids => [3, 4]},
> {:type => roducts, :ids => [5]},
> {:type => :stores, :ids => [6, 7]},
> {:type => :stores, :ids => [8, 9]}]
>
> My function for splitting them was pretty simple, but had to take in
> an enumerator:
>
> def slice_by_ids(hashes)
> Enumerator.new do |yielder|
> hashes.each do |hash|
> hash[:ids].each_slice(2) do |slice|
> yielder.yield hash.merge(:ids => slice)
> end
> end
> end
> end
>
> This function seemed way too specific to put on the Enumerator class,
> but if we had the Thrush combinator we could do something like this:
>
> orig_data.into(&method(:slice_by_ids))
>
> This is also still easily chainable and maintains laziness:
>
> orig_data.
> lazy_map(&method(:do_stuff)).
> into(&method(:slice_by_ids)).
> lazy_map(&method(:do_other_stuff))
>


Hm, that looks quite complex to me. Why not just do this?

require 'pp'

sliced = []

orig_data.each do |h|
h[:ids].each_slice 2 do |sl|
sliced << h.merge(:ids => sl)
end
end

pp orig_data, sliced

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What do you guys think about adding a method "to_json" Juan Pablo Romero Méndez Python 0 09-12-2011 04:34 AM
Ideas for writing a "Combinator" Virchanza C++ 3 04-20-2011 02:08 AM
If you think you must modify the hash, think again David Mark Javascript 17 03-23-2010 08:08 PM
an oddball scary kind of thing you would think would never happen richard Computer Support 4 01-31-2010 06:34 PM
if you guys go non-hybrid blu-ray or hd-dvd.... you guys may be looking at laserdisc/sacd type sales. Doc Martian DVD Video 2 03-23-2006 06:01 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57