Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Last active November 9, 2015 22:48
Show Gist options
  • Save JoshCheek/fa1d93278f7488ef3a79 to your computer and use it in GitHub Desktop.
Save JoshCheek/fa1d93278f7488ef3a79 to your computer and use it in GitHub Desktop.
Dropped this into our slack channel.

If any of you are masochists and want to turn your brains to mush thinking about blocks, here is a challenge for you:


Given this code, do the following steps in order. It is strongly recommended that you do not read ahead (ignorance is bliss)

countdown = lambda do |n, recurse|
  n # => 5, 4, 3, 2, 1, 0
  break if n <= 0
  n # => 5, 4, 3, 2, 1
  puts n
  recurse.call(n-1)
end

enable_recursion.call(countdown).call(5)

# >> 5
# >> 4
# >> 3
# >> 2
# >> 1
  1. Dim the lights and soften the music, this might give ryou a migraine so make the environment hospitable.
  2. Define enable_recursion such that this code does what it shows
  3. Didn't I recommend not reading ahead? Oh... You got this far? O.o I admit, I am impressed. But here's the thing, your code is only allowed to use local variables and lambdas. No methods (other thanuse .call() on a lambda), constants, instance variables, objects, etc. Go fix that.
  4. Alright, alright, 3 was your reprieve, get ready to spend the night huddled in a corner, nursing your brain and murmuring to yourself. Maybe meditate for a bit, make peace with an old enemy, write your will, that sort of thing.
  5. Hug someone you love! Other than enable_recursion, your code is not allowed to create local variables through assignment statements (a = 1... not allowed)
  6. Holy shit, you got through 5? If you need to talk to someone, don't hesitate to call me, okay? Seriously, we're here for you. But alas, the onslaught continues: fix my code, too. No code in your editor window may assign to a local variable (e.g. my countdown, your enable_recursion
  7. Final stretch, lets turn it up to 11! Every lambda in all of this code must take exactly 1 argument.
  8. Alright, lets be honest, you're reading ahead. ...but if not, send me your work and I'll tell you what you did...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment