Last active
May 8, 2018 11:38
-
-
Save YanhaoYang/74ee837a594d1efff5c9d891821b8b18 to your computer and use it in GitHub Desktop.
Compare some details between Ruby and Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> def hi(): | |
... def h2(): | |
... print('h2') | |
... h2() | |
... | |
>>> hi() | |
h2 | |
>>> h2() | |
Traceback (most recent call last): | |
File "<stdin>", line 1, in <module> | |
NameError: name 'h2' is not defined |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
irb(main):006:0> def hi | |
irb(main):007:1> def h2 | |
irb(main):008:2> puts 'h2' | |
irb(main):009:2> end | |
irb(main):010:1> h2 | |
irb(main):011:1> end | |
=> :hi | |
irb(main):012:0> hi | |
h2 | |
=> nil | |
irb(main):013:0> h2 | |
h2 | |
=> nil |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> import functools | |
>>> functools.reduce(lambda x,y: x + y, [1,2,3]) | |
6 | |
>>> functools.reduce(lambda x,y: x + y, [1,2]) | |
3 | |
>>> functools.reduce(lambda x,y: x + y, [1]) | |
1 | |
>>> functools.reduce(lambda x,y: 0, [1]) | |
1 | |
>>> functools.reduce(lambda x,y: 0, [1,2]) | |
0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
irb(main):001:0> [1].reduce(0) {|x| x} | |
=> 0 | |
irb(main):002:0> [1,2].reduce(0) {|x| x} | |
=> 0 | |
irb(main):003:0> [1,2].reduce(0) {|x, memo| x + memo} | |
=> 3 | |
irb(main):004:0> [1,2,3].reduce(0) {|x, memo| x + memo} | |
=> 6 | |
irb(main):005:0> [1].reduce(0) {|x, memo| x + memo} | |
=> 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment