基本上,我想要一個函數,我可以調用它,讓我通過指定增量在一系列數字之間的彈跳循環中進行迭代。我已經嘗試過用多種條件來複雜的解決方案,但在我看來,這應該是一個簡單的數學單線程否?需要數學幫助,以CoffeeScript或Javascript編寫振盪迭代器。
我在制定我的問題時遇到了一些麻煩,所以這裏有一些僞coffescript來更好地解釋我的目標。
# Pseudo Coffeescript class
Class OscillatingIterator
constructor: (low, high, increment)->
this.low = low
this.high = high
this.i = increment
iter: ->
### can haz magical math code plz? ###
# Usage
oi = new OscillatingIterator(1, 5 , 1)
# outputs
oi.iter() #=> 1
oi.iter() #=> 2
oi.iter() #=> 3
oi.iter() #=> 4
oi.iter() #=> 5
oi.iter() #=> 4
oi.iter() #=> 3
oi.iter() #=> 2
oi.iter() #=> 1
oi.iter() #=> 2
oi.iter() #=> 3
oi.iter() #=> 4
oi.iter() #=> 5
oi.iter() #=> 4
oi.iter() #=> ...
我開始認爲條件並不是那麼糟糕。見: https://gist.github.com/1793937 – Quickredfox 2012-02-11 00:17:27