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)
What learning APL taught me about Python
11–20 of 104 posts
Re: What learning APL taught me about Python
#12I 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
#13The only thing this does for me is ask why its not named count instead of sum.
Re: What learning APL taught me about Python
#14Like this line of JS feels so much easier to read than that line of python:
ages.filter(age => age > 17).length
Directly translating this approach to python: len(list(filter(lambda age: (age > 17), ages)))
Although a better way to write this in python I guess would be using list comprehensions: len([age for age in ages if age > 17])
which I feel is more readable (but less efficient) than the APL inspired approach. Overall, none of these python versions seem as readable to me as my JS one liner. Obviously if the function is on a hot path iterating and summing with a number is far more efficient versus filtering. In that case i'd probably still use something like reduce instead of summing booleans because the code would be more similar to other instances where you need to process a list to produce a scalar value but need to do something more complex than simply adding.Re: What learning APL taught me about Python
#15The only thing this does for me is ask why its not named count instead of sum.
Not what actually happens but conceptually:
ages = [17, 13, 18, 30, 12]
sum(age > 17 for age in ages)
=> sum([False, False, True, True, False])
=> sum([0, 0, 1, 1, 0])
=> 2 # via conventional summing
Since True and False are 1 and 0 for arithmetic in Python, this is just a regular sum which also happens to produce a count.Re: What learning APL taught me about Python
#16I think APL's ability to lift loop patterns into tensor patterns is interesting. It certainly results in a lot less syntax related to binding single values in an inner loop.
Re: What learning APL taught me about Python
#17I 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.
Re: What learning APL taught me about Python
#18The only thing this does for me is ask why its not named count instead of sum.
If your input was a numpy array to begin with you could skip the array comprehension, and shorten it to numpy.count_nonzero(ages > 17), since numpy automatically broadcasts the comparison operation to each element of the array.
Re: What learning APL taught me about Python
#19I feel like this kind of operation on a list feels more naturally expressed by filtering the list and taking the length of the filtered list. Like this line of JS feels so much easier to read than that line of python: ages.filter(age => age > 17).length Directly translating this approach to python: len(list(filter(lambda age: (age > 17), ages))) Although a better way to write this in python I guess would be using lis…
Re: What learning APL taught me about Python
#20The only thing this does for me is ask why its not named count instead of sum.
from collections import Counter
total = Counter(range(10)).total()
assert total == 10