基本上,我想要一个函数,我可以调用它,让我通过指定增量在一系列数字之间的弹跳循环中进行迭代。我已经尝试过用多种条件来复杂的解决方案,但在我看来,这应该是一个简单的数学单线程否?需要数学帮助,以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