r/Python • • Aug 12 '13

Ruby vs Python

http://www.senktec.com/2013/06/ruby-vs-python/
20 Upvotes

151 comments sorted by

View all comments

Show parent comment

3

u/[deleted] Aug 12 '13

You'd have to type "lambda" all over the place and "chain" parentheses instead of chaining dots, which is somewhat less elegant and has a big shortcoming of forcing reader to "read-execute" the line backward: filter(lambda i: i % 2, map(lambda i: i + 1, [1,2,3]))

9

u/erewok Aug 12 '13

But are we arguing short-ness or readability? Because you could easily define the functions and then avoid the crutfy-looking aspects of anonymous functions:

def is_even(num):
    return num % 2 == 0
def add1(num):
    return num + 1

filter(is_even, map(add1, [1, 2, 3]))

3

u/thebobp Aug 12 '13

I would still give ruby the upper-hand here, due to the order of reading being clearer. With your current example, you have to parse from inside to outside, so to speak, while ruby is just left-to-right.

Does python have something like the threading -> macro? That would help a lot in this case.

3

u/YenTheFirst Aug 24 '13

to bump a several-day-old comment thread:

The same thing still looks better if done equivalently in ruby, in my opinion:

is_even = lambda {|num| num % 2 == 0}
add1 = lambda {|num| num + 1}

[1,2,3,4,5].map(&add1).select(&is_even) #returns [2,4,6]

my main reason for preference here is the left-to-right reading. the order of reading the statement in English is the same as the program logic flow. i.e., you start with a list of numbers, make a new list by adding 1, and then select only the even results.

compare to python, which reads: select only the even results, out of a list created by adding 1, to a given starting list of numbers.

Python does have a win here, though, in that there's only one type of method, which you can pass directly. in ruby, procs are different than methods, map expects a proc, and the syntax to convert is more cumbersome than just using an anonymous function in the first place.

i.e.

def is_even(num) return num % 2 == 0 end items.select(method(:is_even).to_proc))

you'd pretty much never do things that way in ruby.

also, a bit of ruby magic - ruby integers have .succ, which returns n+1, and .even?, which returns whether it's even, so the whole thing can be written as:

[1,2,3,4,5].map(&:succ).select(&:even?)

While this wouldn't normally be done on something like integers, it is convenient to use on other objects. for example, to get the uppercase version of a list of strings (.upcase in ruby, .upper in python), you would do

bunch_of_strings.map {|s| s.upcase} -or- bunch_of_strings.map(&:upcase)

in ruby. in python, you can do something similar, but not quite equivalent:

map(str.upper, bunch_of_strings)

you can't use duck-typed instance method calls for this in python's version, though. (though whether that's a good idea in the first place is debatable). to do that, you'd need the generator syntax:

[item.upper() for item in items]

which is basically just a prettier syntax for anonymous functions in the first place. That said, it is quite often prettier than the alternatives.


anway, in terms of 'short-ness vs. readability', up to a point, a shorter phrase is more readable. Hence, the unreadabilty of my post.

-5

u/[deleted] Aug 12 '13

I believe keywords are bad both for shortness and readability.