Live data from Hacker News

What learning APL taught me about Python

mathspp.com

21–30 of 104 posts

Re: What learning APL taught me about Python

#21
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.

Python's lack of multi-line anonymous functions is a hindrance to using it as a functional language, IMO.

Most functional languages don't have statements at all, and Python's anonymous functions can, as most, handle any single expression, regardless of complexity or size.

Python having a statement heavy syntax and making complex expressions (while possible) awkward is the problem with its anonymous functions, not the fact that its anonymous functions are limited to a single expression.

Re: What learning APL taught me about Python

#22
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)

ah, yes, of course, forgot the generator.

Re: What learning APL taught me about Python

#23
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)

If you're going to go that route, I think this makes more sense:

    count_over_17 = [age > 17 for age in ages].count(True)

Re: What learning APL taught me about Python

#24
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.

Python's lack of multi-line anonymous functions is a hindrance to using it as a functional language, IMO.

I take the Beyonce approach to functions: if you like it you should have put a name on it.

Re: What learning APL taught me about Python

#25
Kenneth E Iverson, the inventor of APL was truly a genius and his primary mission was about how to bridge the world of computing and mathematics.

To do this he invented the APL notation.

If you find the article interesting, you might enjoy my curation of his work "Math For The Layman" [0] where he introduces several math topics using this "Iversonian" thinking.

[1] Look this up to install the J interpreter.

[0]: https://asindu.xyz/math-for-the-lay-man/

[1]: https://code.jsoftware.com/wiki/System/Installation/J9.4/Zip...

Re: What learning APL taught me about Python

#26
post #14

I 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…

I didn’t see it in the article so I thought I would add,

The actual apl implementation: +/age>17

Apl implementation of taking the length(shape) of the filtered list: ⍴(age>17)/age

Re: What learning APL taught me about Python

#27
post #14

I 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…

I didn’t see it in the article so I thought I would add, The actual apl implementation: +/age>17 Apl implementation of taking the length(shape) of the filtered list: ⍴(age>17)/age

It's in there but near the end (80% or so of the way down the page). The article would benefit from moving that to the top and drawing the comparison to the APL code earlier.

Re: What learning APL taught me about Python

#29
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)

You can't call len on a gen exp, though you could define a count function. For an unsafe variant:

  def count(it):
    return sum(1 for _ in it)
Which is basically just putting a friendly name on the approach from the article.

For a safe version, you probably want to wrap it in another generator that bails with an exception at a specified size so you don’t risk an infinite loop.

  def safe_count(it, limit=100):
    # returns None if actual length > limit
    nit = zip(range(limit+1), it)
    if (l := sum(1 for _ in nit)) 
Of course, you can just convert to a list and return the length, but sometimes you don’t want to build a list in memory.

Re: What learning APL taught me about Python

#30
post #14

I 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…

If ages is a numpy array instead of a list:

    (ages > 17).sum()
Post reply on HN