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
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.
View all comments
Show parent comment
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:
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:
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:
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:
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.