Skip to content

Instantly share code, notes, and snippets.

@BytesAndCoffee
Created September 29, 2016 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BytesAndCoffee/94db04a99000b730ab92d044648b8607 to your computer and use it in GitHub Desktop.
Save BytesAndCoffee/94db04a99000b730ab92d044648b8607 to your computer and use it in GitHub Desktop.
switch = lambda x: [lambda x: x // 2, lambda x: x * 3 + 1][x % 2](x)
print(switch(5))
~~~
16
switch = lambda { |x| [lambda { |x| x / 2 }, lambda { |x| x * 3 + 1}][x % 2](x)}
puts switch(5)
~~~
collatz.rb:1: syntax error, unexpected '(', expecting '}'
...ambda { |x| x * 3 + 1}][x % 2](x)}
... ^
collatz.rb:3: syntax error, unexpected end-of-input, expecting '}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment