Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.


Oh I think I see what you mean. Yes I was thinking of the built-in functions of sum & length


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.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: