Is there an idiom for a loop that executes some block of instructions between iterations? (In Ruby in particular)

I often need to do some operations in a loop and some other operations between the iterations. A simple example would be collecting words from an array into a string, spelled backwards and separated with commas. Is there an idiom or language support for this in any language? (For now i am mostly interested in Ruby.)

I usually do something like

a = ['foo', 'bar', 'baz']
s = ''
n = a.size - 1
i = 0
loop do
  s << a[i].reverse
  break if i == n
  s << ', '
  i += 1
end

But i know no way to save this half iteration if using a ruby iterator:

s = ''
['foo', 'bar', 'baz'].each do |w|
  s << w.reverse
  # ???
end

3

This is called the loop-and-a-half problem. You’ve described a popular solution.

People who are into functional programming solve many instances of this with a function that is usually called reduce, which takes a list and performs a binary operation for every element but the first with the result of the previous calculation, and the next value. This function is available in Ruby under both the names inject and reduce. See http://ruby-doc.org/core-2.0/Enumerable.html#method-i-reduce for more.

4

This is how you could do it in haskell:

let a = ["foo", "bar", "baz"]  --I've defined my "a" just like you defined it
Data.List.intercalate "," (map reverse a)  --here's the call that does what you want
"oof,rab,zab"  --this is the output

The code in the parenthesis calls the reverse function on every element of “a” (eg: “foo” becomes “oof”). You take the result of that, and in another function you concatenate the elements together with “,” in between them.

Here’s the key thing to take away here: Sometimes you have functions that change every element and returns another list (eg: map reverses every element, but it still returns a list). Other times you have functions that take a list and returns one element (eg: intercalate will generate one string out of the list of strings you pass in).

These are two separate steps and your code will be easier to reuse if you keep them separate.

I know this isn’t answered in ruby, but I’m hoping it’s somewhat helpful.

0

Unfortunately, there is no intercalate like in @tieTYT’s answer in ruby. Array#reduce can be used but – e.g. in your example – you have to keep an eye on the order of the elements (reversal of the accumulator memo).
an example in ruby could look like this:

arr = ["foo", "bar", "baz", "goo"]
puts arr.inspect
red = arr.reduce {|memo, item, err|
    "#{memo == arr.first ? memo.reverse : memo}, #{item.reverse}" 
}
puts red # -> oof, rab, zab, oog

your example – joining the elements of an array together into a string – can easily accomplished with Array#map followed by Array#join (join is like haskell’s intercalate, but it does not accept a block in ruby) like:

arr.map{|ele| ele.reverse}.join(", ")

after your comment to @btilly:
IMHO, both look acceptably clear as one-liners in e.g. erb, though the second one is clearly more readable.

<%= arr.reduce{|memo, item| "#{memo == arr.first ? memo.reverse : memo}, #{item.reverse}" } %>
<%= arr.map{|ele| ele.reverse}.join(", ") %>

anyway you can always assign the result to a variable upstream.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật