所以我很喜歡這句法和Lisp:在Ruby中獲取Lisp樣式(+ *參數)的最簡單方法是什麼?
(+ 1 1 2 3 5 8 13)
=> 33
我想添加在Ruby中的項目清單,並希望儘可能地接近這一點。 現在,我的最佳解決方案涉及一個數組和收集/映射方法。
所以:
sum = 0; [1,1,2,3,5,8,13].collect { |n| sum += n }
但是...
我想方法添加到這裏面可以返回零。
sum = 0; [1, booking_fee, 8,13].collect { |n| n = 0 if n.nil?; sum += n }
而且這將是非常好的要做到這一點,所有的中間線的指可能返回nil的方法,但我不能確切地建立在這樣一個數組。這只是我想讓我的語法看起來像什麼的想法。
def total
Array.new do
booking_fee
rental_charges
internationalization_charges
discounts
wild_nights
end.collect { |n| n = 0 if n.nil?; sum += n }
end
在我試圖破解並實施格林斯潘的規則之前有什麼建議嗎? (編程的確是一個強迫
我一直認爲Ruby上的Lisp語法會很棒。我認爲那就是你想要做的事情。 – Linuxios