First time here? Check out the FAQ!
x

What would be the most elegant way to countTriggersMod 'forwards-backwards'

0 votes
409 views
I'd like to countTriggers up to a mod value, then count in reverse back to zero (or perhaps another integer) and then the counting flips direction again...

 

What would be a really neat way to do that in Capytalk?
asked Apr 27, 2018 in Capytalk & Smalltalk by cristian-vogel (Master) (8,410 points)

1 Answer

+1 vote
 
Best answer
((((1 bpm: !BPM) nextIndex + !Beats) mod: (!Beats * 2)) - !Beats)  abs
answered Apr 27, 2018 by ssc (Savant) (126,620 points)
selected Apr 28, 2018 by cristian-vogel
That is very elegant! Thankyou.

One of the things I love about programming in Capytalk, an event driven language, is how the optimal solution to something does not usually involve 'Conditionals' as in other programming languages... such as  If this condition is true then do this...   Its encourages a more holistic form of thinking, less concerned with forcing behaviours and more about understanding the nature of fundamental processes, like 'different ways to count numbers' in this case.
I was just trying to implement this idea.... does nextIndexReset: understand the reverse: message like nextIndexMod: ?
I found this works for me...



|  index clock |

clock := (1 bpm: !bpm).

index :=

(
        (
            (
                ((clock nextIndexMod: (!beats * 2) reverse: !reverse reset: !reset ) + !beats )                
            ) mod: (!beats * 2)
        ) - !beats
)
 
abs.



index
...