Podcast Lookup

Processing with Kotlin 4/4 - custom loop

Processing in Idea

  • March 18, 2021
Episode page

About this episode

After converting an existing Processing+Java program to Processing+Kotlin, we implement a new loop operation I call fancyRepeat (only because naming is hard, and naming while recording videos is even harder).

The program shown is based on this one:
https://github.com/hamoid/Fun-Programming/tree/master/processing/ideas/2020/03/irregularLineBetweenTwoPoints

00:00 Write fancyRepeat()
05:10 Use fancyRepeat() again, define default values for the arguments
06:32 Use a named argument to skip an argument with default value

Note 1: I called the first argument of fancyRepeat() times because that was the name it had in repeat() but I think in this case steps would be more readable.

Note 2: At 03:35 I entered (times - 1) in the code. I would say this is good for the second use of fancyRepeat, that is, when drawing the line with 350 steps, so it starts and ends exactly where we want. BUT it's not right for the first use of fancyRepeat because we don't want the first angle (0.0) to be equal to the last angle (two pi), otherwise the first line will overlap with the last line.

There are at least 3 solutions to this.

  1. Add an argument to fancyRepeat to be able to choose if we want a looping behavior or not (in other words, if we want that -1 or not).
  2. create two different functions, one meant for loops without -1 (loopRepeat?), and for non-loopy situations, with -1 (doubleRepeat?).
  3. don't attempt to write such a fancyRepeat function. Maybe it wasn't a great idea? 😃

Main take-aways of this episode are:

  • You can ctrl+click on Kotlin methods and see how they are implemented.
  • Functions can have default values for arguments
  • When calling a function you can skip arguments by using named ones.

Kotlin documentation: https://kotlinlang.org/docs/home.html
You can play with Kotlin without downloading anything at https://play.kotlinlang.org/ (but only text output, no graphics).

More Processing & Idea videos: https://tubedu.org/c/processing_in_idea

Ask questions and access extra content at https://patreon.com/funprogramming