Live data from Hacker News

What learning APL taught me about Python

mathspp.com

1–10 of 104 posts

Re: What learning APL taught me about Python

#4
post #2

The only thing this does for me is ask why its not named count instead of sum.

In Python a Boolean true and false are often used in mathematical formulas, so they will often implicitly be coerced into the integers 1 and 0. Sum is the sum function, you can sum a sequence of numbers, but in this case it’s summing a bunch of Boolean values which are coerced to 1 and 0.

Re: What learning APL taught me about Python

#6
post #2

The only thing this does for me is ask why its not named count instead of sum.

Because what is happening I the list of ages is being transformed into a list of booleans where it's true if the age is greater than 17. This list of booleans is then turned into a list of integers where it's 1 if true, 0 if false. This list of integers is then being summed.

Re: What learning APL taught me about Python

#8
post #3

I know nothing about APL. But I think I would write it the same way as the OP. I also think use len is better to convey counting operation: len(age for age in ages if age > 17)

I don’t think you can do that with a generator expression. You would have to write:

  sum(1 for age in ages if age > 17)

Re: What learning APL taught me about Python

#9
post #5

I find that the more language you learn the better you can utilize all of them. Also, Python is a wonderful functional language when used functionally.

It really is a strong lesson. Every language will shift and twist your mind and expand your horizon. You might hate your colleagues then though.

Re: What learning APL taught me about Python

#10
post #8
post #3

I know nothing about APL. But I think I would write it the same way as the OP. I also think use len is better to convey counting operation: len(age for age in ages if age > 17)

I don’t think you can do that with a generator expression. You would have to write: sum(1 for age in ages if age > 17)

It would eat ram at scale but you could wrap the gen expression with [] for a list comprehension and that would work.
Post reply on HN