4 ms·
> -lambdas can thus contain any expression why would you want to use lambdas if you have generator expressions? eg: map(lambda c: c.strip(), countries.split('
by codesuela 15y ago
> -lambdas can thus contain any expression
why would you want to use lambdas if you have generator expressions? eg:
map(lambda c: c.strip(), countries.split(','))
vs
[c.strip() for c in countries.split(',')]
IMHO the latter one is way more readable and generators cover almost all the use cases for lambdas. If you could not do it with a generator expression and choose to use a anonymous function it should have been a method anyway.
> -much bigger range of built-in methods and classes (all permutations of a list, removing duplicates from a list, Rational class, Regexp class, Range class, etc.)
you mean sets, dictionaries(hashtables) etc.? those are built in as well in Python.
Remove duplicates: mylist = list(set(mylist))
Range class: [i for i in range(10)]
you can import the rational class via a simple from fractions import Fraction
same for regex
> -more flexible syntax and "control operators" like each_with_index
you can iterate over dictionaries with for k,v in dict.items()
if you need the index of a list you can use for i,v in enumerate(list)
personally I don't think there is much of a difference between Python and Ruby. One is more mature the other is way hipper and it comes down personal taste which one you choose.
- haberman 15y ago> why would you want to use lambdas if you have generator expressions? Lambdas (blocks) in Ruby are used for way, way more than just building or manipulating lists. For example, there is no need for the "with" keyword in Ruby, because you can just use blocks for that. As another example, you can specify a block to the Hash constructor to specify code that runs when a key is accessed that is not present. > Range class: [i for i in range(10)] That is not a range class, that is an expression for constructing a contiguous list of numbers. The Range class in Ruby is an actual class that you can instantiate and call methods on. ("a".."z").cover?("c") #=> true ("a".."z").cover?("5") #=> false > personally I don't think there is much of a difference between Python and Ruby. One is more mature the other is way hipper and it comes down personal taste which one you choose. They have similar capabilities but make very different choices in a lot of ways.
- thehotelambush 15y agoThanks for mentioning sets - this is another thing that I would like to be built-in in Ruby. But "list(set(mylist))" seems like kind of a hack. > you can import the rational class via a simple from fractions import Fraction Sure, you can write a library for anything and by now Python has libraries for most everything. But it still makes a difference to have things in the core language, plus a nice syntax for them. > why would you want to use lambdas if you have generator expressions? This is a case of making up for odd semantic choices with special syntax, which then becomes an unnecessarily fixed-in-stone part of the language. Range objects in Ruby are actually like slice objects in Python. But Ruby separates the syntax for ranges from the get-item method, since there are plenty of other places where you would want to use ranges (then you can iterate and map over them, etc.). Ruby uses blocks to implement things like an each method that fulfills the same function as Python's for statement. (Ruby has a for statement but it is hardly ever used.) In Ruby your example would be countries.split(',').map &:strip which is obviously more concise and no less readable. Compare with countries.split(',').each &:foo where foo is some side-effectful method. IMO such a small change in behavior should result in a similarly small change in how the program is written.
- nathan_f77 15y agoRuby does have sets: require 'set' Set.new([1,2,3])
- wging 15y ago>Range class: [i for i in range(10)] How does this differ from list(range(10)) (besides being highly suggestive of the power of list comprehensions)? >IMHO the latter one is way more readable and generators cover almost all the use cases for lambdas. If you could not do it with a generator expression and choose to use a anonymous function it should have been a method anyway. This is actually why Guido van Rossum wanted to remove lambda from the core language: http://www.artima.com/weblogs/viewpost.jsp?thread=98196 http://www.artima.com/weblogs/viewpost.jsp?thread=98196 (He didn't, thankfully. I still want reduce back in 3.x, though.)