My Secret Life as a Spaghetti Coder
home | about | contact | privacy statement
With a name like each_cons, I thought you were going to iterate through all the permutations of how I could construct a list you operated upon. For example, I thought

[1,2,3,4].each_cons do |x| # I did not notice the required argument
  puts x.inspect

would output:

[[1,2,3,4], []]
[[1,2,3], [4]]
[[1,2], [3,4]]
[[1], [2,3,4]]
[[], [1,2,3,4]]

So when I needed to find the local maxima in an image projection to algorithmically find the staves in sheet music, I found myself wanting a C-style for loop.

I didn't know you'd provide me with a wonderful sliding window!

[1,2,3,4].each_cons(2) do |x|
  puts x.inspect

[1, 2]
[2, 3]
[3, 4]

From now on, I'll turn to you when I need that functionality. Thanks for waiting on me, each_cons. Not everyone would be as patient as you.

Warm Regards,

PS: In case you're interested, the "cons" in "each_cons" is short for "consecutive," not "construct," as Matz informed me:

Matz says 'I am not the one who come up with the name, but I suppose cons is the short form of consecutive.'

Hey! Why don't you make your life easier and subscribe to the full post or short blurb RSS feed? I'm so confident you'll love my smelly pasta plate wisdom that I'm offering a no-strings-attached, lifetime money back guarantee!

Leave a comment

Actually, isn't Enumerable#each_slice(2) a closer equivalent to the original each_pair implementation?

Posted by JayTee on Nov 09, 2012 at 06:13 PM UTC - 5 hrs

Hey JayTee,

I'm guessing you mean this: when you're asking about "the original each_pair implemenation."

Is that right?

All three things do something different:

Let's use [1, 2, 3, 4] as an example.

#each_slice(2): will split the array on every 2nd element, so our example array would go through 2 iterations: one for [1,2] and the other for [3,4].

#each_pair will go through every combination of elements. The iterations using our example array would be:
[1, 2]
[1, 3]
[1, 4]
[2, 3]
[2, 4]
[3, 4]

Finally, #each_cons(2) will have a "sliding window" effect, yielding for each iteration:
[1, 2]
[2, 3]
[3, 4]

Posted by Sammy Larbi on Nov 10, 2012 at 12:17 PM UTC - 5 hrs

Leave a comment

Leave this field empty
Your Name
Email (not displayed, more info?)


Subcribe to this comment thread
Remember my details

Picture of me

.NET (19)
AI/Machine Learning (14)
Answers To 100 Interview Questions (10)
Bioinformatics (2)
Business (1)
C and Cplusplus (6)
cfrails (22)
ColdFusion (78)
Customer Relations (15)
Databases (3)
DRY (18)
DSLs (11)
Future Tech (5)
Games (5)
Groovy/Grails (8)
Hardware (1)
IDEs (9)
Java (38)
JavaScript (4)
Linux (2)
Lisp (1)
Mac OS (4)
Management (15)
MediaServerX (1)
Miscellany (76)
OOAD (37)
Productivity (11)
Programming (168)
Programming Quotables (9)
Rails (31)
Ruby (67)
Save Your Job (58)
scriptaGulous (4)
Software Development Process (23)
TDD (41)
TDDing xorblog (6)
Tools (5)
Web Development (8)
Windows (1)
With (1)
YAGNI (10)

Agile Manifesto & Principles
Principles Of OOD
Ruby on Rails

RSS 2.0: Full Post | Short Blurb
Subscribe by email:

Delivered by FeedBurner