Turing Mod 1 and Recursion
Recursion - Favorite Technical Concept from Mod 1
One of the hardest and most fun concepts I learned during mod 1 was ‘recursion’. This at times can feel like something out of the movie Inception, but it was one of the most powerful tools I learned. It allows you to loop over a part of your program until a condition is met. Each new layer puts a temporary pause on the layer that spawned it and then ruby cycles back up through the layers completing the remaining program at each level before returning to the level that spawned it.
Here is an example of a recursive method used to implement the Sieve of Eratosthenes to find prime numbers:
This creates an array of every number from 2 to the limit (in this case 10) then removes the ones that are multiples of that one then moves on to the next one. The method calls itself (primes(start)
) with a new starting point in the array until we reach the last number in the array (if start != @range.last
). If we were to look at the @range
array through each step we would see:
as it cycled through each level. However if we insert a p @range
after the if start != @range.last ... end
(which contains our recursion point) is called we would see the following output in the terminal:
Each time ruby hits the primes(start)
line we “drop down to a new level of the dream world” (a la Inception). On that level we execute the primes
method from the start and if we hit primes(start)
again, we drop to a level deeper. We keep doing this until primes
returns (i.e. until it hits @range
on the last line). This returns @range
back to the previous level which then runs through the rest of its method (in this example printing @range
to the terminal). It does this for each level until it returns back to the highest level and the program concludes.
Recursion can be very useful for avoiding while
or until
loops as well as dealing with smart node navigation (see “Smart Nodes all the time!” for a perspective on smart nodes).
Mod 1 – a test of character
I must admit, I had my doubts if Turing would be the right fit for me. I am a self-taught WordPress developer and had been successfully running my own web development company for years. I was worried that I might not get the most out of the program, because I already had a fair amount of coding knowledge. And yet, I was at a point where I wanted to dive deeper into coding and be immersed into a community of similar minded people. So I took the plunge.
Quickly it became clear, that I had to find my own way of succeeding at Turing. By that I don’t mean just getting the projects done on time but going further to be able to challenge myself and at the same time contribute to the community in the best way I can.
It’s a hard path and a difficult balance to strike. And I am grateful for the support I got from the instructors and the feedback from my cohort and other Turing students. Family and friends who know me well have commented that I look fulfilled and have rediscovered an intrinsic sense of motivation. I take that as a good reality check and as a confirmation that starting the program has so far been a good decision.
Most importantly though is the feeling that I am looking forward to starting Mod 2.