3 ms·
That's how you think a Rubyist would write Fibonacci? I think one might follow the Python "idea" but avoid defining a function . . . fib = lambda do |n|
by tuke 13y ago
That's how you think a Rubyist would write Fibonacci? I think one might follow the Python "idea" but avoid defining a function . . .
fib = lambda do |n|
return n if n < 2
fib[n-1] + fib[n-2]
end
puts fib[8]
- nwmcsween 13y agoYes his examples are very very unrubyish, it looks like javaish ruby.
- bhrgunatha 13y agoThe biggest thing that jumps out at me is that the Ruby code is defining a Hash object (map or dict) and not a function. To say a Ruby programmer would create a Hash object that uses a block instead of write a function seems plain wrong, despite how the algorithm for generating items was implemented.
- ioquatix 13y agoThe ruby code [in the linked article] is complete rubbish. In addition, the Ruby implementation is memoized while the Python example isn't. Clearly that means there is a significant performance difference. I'd like to see the memoized Python implementation in one line.
- doctoboggan 13y agoIt's just one more line - wrapping the function with the memoization decorator from the stdlib: http://docs.python.org/3/library/functools.html#functools.lru_cache http://docs.python.org/3/library/functools.html#functools.lr...
- panzi 13y agoAnd it's much clearer on what it actually does.