News for hackers. Stuff that matters.
Re:Why People Should Learn Python by dood on Wednesday August 24, 2016 @08:50AM (#12351611)
Could you give an example of that?
Re:Why People Should Learn Python by K0nserv on Wednesday August 24, 2016 @10:02AM (#12352060)
Imagine you have a list of products with prices and you want to calculate the cost of all products that are of a certain type. In ruby you can write this as

products.select { |product| product.type == 'x' }.map(&:price).reduce(:+)

Re:Why People Should Learn Python by minikomi on Wednesday August 24, 2016 @10:11AM (#12352126)
And in Python you can write something like:

    sum([p.price for p in products if p.type == "x"])
Excuse untested code from phone, but I like it better than the ruby version.
Re:Why People Should Learn Python by lmm on Wednesday August 24, 2016 @10:32AM (#12352308)
I find the order of iteration in Python for comprehensions totally confusing every time. If you want to convert:

    for a in x: pass
into a comprehension, you write:

    [pass for a in x]
Simple, right? So to convert

    for a in x: for b in y: pass
I obviously write:

    [pass for b in y for a in x]
Bzzt. Nope, you have to write it in middle-endian order to get the same behaviour.
Re:Why People Should Learn Python by amyjess on Wednesday August 24, 2016 @11:30AM (#12352737)
FYI, you can leave off the square brackets for a small performance boost. That turns it from a list comprehension into a generator expression, which is lazily evaluated and doesn't create a list container (which would immediately be thrown out anyway). It's like the difference between range(n) and xrange(n).
Re:Why People Should Learn Python by minikomi on Thursday August 25, 2016 @12:33AM (#12356986)
Nice, thanks for the tip.
Re:Why People Should Learn Python by K0nserv on Wednesday August 24, 2016 @10:27AM (#12352261)
I guess it's a personal style choice, the Python code is more performant(less iterations over the data set), but I like the ruby version better.
Re:Why People Should Learn Python by ezrast on Wednesday August 24, 2016 @08:59PM (#12356293)
It's only one iteration if you throw a .lazy in there:

products.lazy.select { |product| product.type == 'x' }.map(&:price).reduce(:+)

Re:Why People Should Learn Python by barrkel on Wednesday August 24, 2016 @09:50AM (#12351963)
Think: bash shell pipes. Transforming input to output through successive filters and projections, applied in a concatenated fashion and developed interactively in a REPL. Model algorithms as transformations or compositions of the same, where possible, applied to lazy or eager iterators or consumers (the dual of an iterator, that acts as a sink for output) as required. Model data sources and sinks (like databases and network streams) as iterators and consumers. Program by building a data flow graph out of increasingly sophisticated (by composition) stateless widgets, rather than imperative loops etc.