Live data from Hacker News

KlongPy: High-Performance Array Programming in Python

github.com

21–30 of 90 posts

Re: KlongPy: High-Performance Array Programming in Python

#21

I thought I knew python, but then I saw this: ?> sum::{+/x} :" sum + over / the array x :monad ?> sum([1 2 3]) 6 ?> count::{#x} :monad ?> count([1 2 3]) 3 What?

oooooooh I think I'm starting to see... sum::{} is the creation of a monadic function (which I just learned about because I don't have a CS background) :" is a comment sum([...]) is calling the function with a list, and the function will iterate over the list adding each of the elements count::{#x} is another monadic function that returns the number of elements, which then gets called

I just briefly reviewed the README docs, I believe the KlongPy is a custom language which transpiles to Python. The REPL block you are trying to interpret is the KlongPy REPL not a Python one.

Embedding KlongPy in a Python block would look more like this (also from the docs):

    from klongpy import KlongInterpreter
    import numpy as np

    data = np.array([1, 2, 3, 4, 5])
    klong = KlongInterpreter()
    # make the data NumPy array available to KlongPy code by passing it into the interpreter
    # we are creating a symbol in KlongPy called 'data' and assigning the external NumPy array value
    klong['data'] = data
    # define the average function in KlongPY
    klong('avg::{(+/x)%#x}')
    # call the average function with the external data and return the result.
    r = klong('avg(data)')
    print(r) # expected value: 3
Note the calls to "klong('<some-str-of-klong-syntax')".

Re: KlongPy: High-Performance Array Programming in Python

#22
What are some real-world practical applications of array programming? Because as far as I know, APL did not catch on, and its subsequent versions like J and K and BNQ also did not become useful in industry. Maybe there's something I'm missing. Why would you want to do array programming in Python? What are the advantages over regular Python programming?

Re: KlongPy: High-Performance Array Programming in Python

#23

I thought I knew python, but then I saw this: ?> sum::{+/x} :" sum + over / the array x :monad ?> sum([1 2 3]) 6 ?> count::{#x} :monad ?> count([1 2 3]) 3 What?

Sorry if this is a dumb question, why is this weird in Python? I use R as my main language and this is exactly how I would expect it to work. Does Python not do this naturally?

> sum::{+/x}

> count::{#x}

These two expressions are not valid in R or Python. They are implicitly declaring a function without declaring the arguments, it is implicitly iterating over the array and returning the result (all features of array languages to make it more concise).

Python’s + and / are a function of two objects (dyadic) and are syntactic sugar (they can’t be treated as function values, thats what the operator module is for): https://docs.python.org/3/reference/datamodel.html#object.__...

The array languages optimize for concise code so a lot of things are done implicitly that are done explicitly in Python.

The python equivalent:

> _sum = lambda x : functools.reduce(operator.add, x)

> _count = lambda x: functools.reduce(lambda acc, _: acc + 1, x, 0)

Of course in python and R there are built ins for these already (sum(x) and len(x)) but this is just to show what array languages are doing for you.

Re: KlongPy: High-Performance Array Programming in Python

#24

I thought I knew python, but then I saw this: ?> sum::{+/x} :" sum + over / the array x :monad ?> sum([1 2 3]) 6 ?> count::{#x} :monad ?> count([1 2 3]) 3 What?

Sorry if this is a dumb question, why is this weird in Python? I use R as my main language and this is exactly how I would expect it to work. Does Python not do this naturally?

Klong is a different language than Python. Python using numpy would look broadly similar for the first few examples, yes. However, this is a way of executing a different language on numpy arrays. Array languages are a different paradigm. Klong is an array language, while Python + numpy allows some array paradigms, but isn't an array language.

Notice how they're _defining_ the "sum" operation there. Instead of being something builtin, they defined "sum" as {+/x}.

{+/x} is the interesting part. That's Klong. It's not being able to define a "sum" operation, it's that "sum" can be expressed as {+/x}. That's very different than both R and python.

Re: KlongPy: High-Performance Array Programming in Python

#25

I thought I knew python, but then I saw this: ?> sum::{+/x} :" sum + over / the array x :monad ?> sum([1 2 3]) 6 ?> count::{#x} :monad ?> count([1 2 3]) 3 What?

oooooooh I think I'm starting to see... sum::{} is the creation of a monadic function (which I just learned about because I don't have a CS background) :" is a comment sum([...]) is calling the function with a list, and the function will iterate over the list adding each of the elements count::{#x} is another monadic function that returns the number of elements, which then gets called

I'm new too, so take my opinion with a grain of salt, but I think you're right with one minor correction:

    name::{body} :" function declaration
This isn't necessarily niladic or monadic or dyadic on it's own, it depends on the body. The bodies from the example all just happened to be monadic.

    +/x          :" monadic : sum over argument x
    #x           :" monadic : count of the argument x
    +            :" dyadic  : sum
    !10          :" niladic : an array from 0 to 9
I had to look up how calling a dyadic function looks in Klong:

    add::{+}
    add(2;3)

Re: KlongPy: High-Performance Array Programming in Python

#26

What are some real-world practical applications of array programming? Because as far as I know, APL did not catch on, and its subsequent versions like J and K and BNQ also did not become useful in industry. Maybe there's something I'm missing. Why would you want to do array programming in Python? What are the advantages over regular Python programming?

I thought they had niche in finance.

Re: KlongPy: High-Performance Array Programming in Python

#28

What are some real-world practical applications of array programming? Because as far as I know, APL did not catch on, and its subsequent versions like J and K and BNQ also did not become useful in industry. Maybe there's something I'm missing. Why would you want to do array programming in Python? What are the advantages over regular Python programming?

[deleted]

Re: KlongPy: High-Performance Array Programming in Python

#29

What are some real-world practical applications of array programming? Because as far as I know, APL did not catch on, and its subsequent versions like J and K and BNQ also did not become useful in industry. Maybe there's something I'm missing. Why would you want to do array programming in Python? What are the advantages over regular Python programming?

APL did catch on to some extent, see https://news.ycombinator.com/item?id=39471718 .

Without getting into any discussion of the array paradigm itself, the reason commercial programming is such a winner-take-all system now is hiring and organizational difficulties, not that popular languages are so much more productive than unpopular ones. Building a working application is the easy part of running a business.

Post reply on HN