428 points · by andybak · 7y ago · 169 comments
I’m not 100% sure what that would look like, but I would ditch classes (and thus inheritance) for named tuples, make most things immutable by default, include enums and pattern matching (that the type system would check; I hardly see the point in dynamic pattern matching). If possible, typed code would compile to C extensions or similar. Oh, and exceptions would go away in favor of a Rust-like Result type.
https://en.wikipedia.org/wiki/Cython
There's also RPython which is restrictive and what PyPy is done in.
https://en.wikipedia.org/wiki/PyPy#RPython
Edit: on an unrelated note, sounds like you'd love Go.
Unless you know C it's not always worth using. Without doing any "C stuff" (i.e. just writing Python and compiling with Cython) you get about a 50% speedup in tight loops; sometimes that's good enough, other times it's not.
To go any faster than that, you basically need to start writing C code, using the CPython API, using Cython's badly under-documented features for doing so.
* Poor syntax support in the language. E.g., you have to define TypeVars as variables in a scope outside of the unit you want to use them in, potentially conflicting with other generic functions. No clear guidance from the documentation about how the type system interprets these, either.
* No recursive types; e.g., can't define a JSON type
* Some callables can't be typed. I'm thinking they're those with kwargs and/or args, but I forget the particulars.
* Unions aren't true ADTs; the only way to define single-value variants (e.g., "null" in a JSON type) is to do something verbose and hokey, like creating a type that can only have one value.
* Lots of common libraries can't be spec'ed and therefore can't be typechecked. Things like SQLAlchemy which generate their types/classes at runtime, for example.
* Generally, the implementation is buggy even for mundane cases
I think this issue is way overblown. The reasons for choosing a particular string representation in Haskell are analogous to the reasons to choose among e.g. byte arrays, streams, string builders, etc. in mainstream programming languages.
> Which prelude?
There is a standard prelude. Unless you actively choose another one, that's the one you'll get.
> Which compiler extensions?
If you want to use certain advanced language features, enable the appropriate extensions.
> Which testing library?
Many mainstream programming platforms (e.g. .NET) have multiple popular testing libraries to choose from. Is this a bad thing?
Some of those are very needed constructions (like algebraic data types with pattern matching), but it's adding an entire sub-language into a language that is built on the goal of being simple. I'm not sold into it either.
Everyone feels like X language is so great but if only we could add a feature from Y language to it. They then attempt to trivialize what is needed for that feature not realizing that the benefits of Y are due, in part, to the non-trivial amount of functionality. They almost always turn into a façade of the feature they truly want to support and they never gain traction because their usefulness just really isn't there.
The sum types are create by multiple data declarations joined by directives. It's ugly, messy, error-prone and as your sibling comment says, misses the point of enabling static verification.
That said, all of these things are symptoms of the preferences of the community around the language, and not restrictions imposed by the language design itself. Core Haskell is actually comprised of some pretty easily understandable functions. You can craft synonyms for more advanced abstractions such as applicative fairly easily once you understand the type system. Haskell more or less gives you everything you need to write functional programs in a way that reads like plain english--this is very uncommon however, given the language's origin and close connection with mathematics.
On the other hand, once I did get used to some of the infix operators, I started to really like some of them, i.e. how:
f data data
becomes
f <$> fancyData <*> fancyData
where what makes the data fancy might be validation, being in container like list, or even some sort of reactive-ui-component, like Flare library for Purescript does: http://try.purescript.org/?backend=flare
Beyond that, some particulars:
* Being terse is a lower priority than usual
* Cool features are a lower priority than usual
* Be careful how you use and then re-use symbols (Haskell can be confusing this way).
* Use English (or whatever spoken language) in your symbols
https://www.python.org/dev/peps/pep-0020/
"Explicit is better than implicit."
My understand was that the big issue was the fact that [third party] libraries assume that strings were made of 8bits characters.
How does coconut solve that issue?
Seems a little misleading....
if you import enough things from `six` or `__future__` then you code will run the same on both python 2/3.
some things like fancy tuple expansion aren't available if you work with that subset, but yeah, most things are OK.
https://coconut.readthedocs.io/en/master/DOCS.html#compatibl...
I wonder how they are doing tail call optimization in python
https://github.com/evhub/coconut/blob/07e311fb8f69861d30e58f...
That said, it looks a lot more pleasant than Python. The addition of pattern matching alone makes this seem like a very worthwhile project, a big improvement on Python.
def reduce(enumerable, fun) do
result =
Enumerable.reduce(enumerable, {:cont, :first}, fn
x, :first -> {:cont, {:acc, x}}
x, {:acc, acc} -> {:cont, {:acc, fun.(x, acc)}}
end)
|> elem(1)
case result do
:first -> raise Enum.EmptyError
{:acc, acc} -> acc
end
end
Seems pretty straightforward to me.https://github.com/elixir-lang/elixir/blob/v1.7.4/lib/elixir...
(define (reduce-left fn init lst)
(if (null? lst)
init
(reduce-left fn (fn init (car lst)) (cdr lst))))
It's clear from that quote that Guido (at least at that time) doesn't have real experience with functional programming and is letting his personal biases cloud his judgement.Okay maybe '"scheme isn't a functional language because it allows side effects and only Haskell and family are allowed to call themselves functional", or whatever. He seems to perhaps hint at that mentality. But the fact remains that a language like scheme makes this much easier than python.
foldl _ acc [] = acc
foldl f acc (x:xs) = foldl f (f acc x) xs
And in Python something like def foldl(f, acc, l):
for x in l:
acc = f(acc, x)
return acc
Which doesn't seem much harder to read or write than any other version, to me (it's all just syntax around the same algorithm).In fact Python taught a generation of programmers to love functional code, because of the big speedup available from stringing together C-native builtins with higher order functions.
Furthermore over the years Guido has made it pretty clear that he doesn't particularly like or care about functional programming: https://python-history.blogspot.com/2009/04/origins-of-pytho...
One of the reasons I love Python is because of it's syntactic brevity and conciseness, once you get to intermediate python there are some weird forms of syntactic sugar (magic functions and unpacking for instance) but visually the language has less clutter than most popular languages. Coconut really just seems to remove that aspect.
JS and Java... well they're not something I think a lot of programmers want to be. JS has a pretty complicated ecosystem that a lot of developers don't like. Java is good at what it's good at, but it's not very fun.
What I see a lot of is "lisp" or "lisp-like" and "pythonic". Those languages are notable for being considered fun by a lot of programmers.
But yes, it's idiomatic. Python is an idiomatic programming language that has sacrificed a lot for that ideology. Now that guido is gone it's less ideological, and we're getting things like type-hinting.
When people try to apply that same ideology to other fields, sometimes they call it pythonic. When they try to apply the lisp ideology to other fields, they just call it a lisp dialect instead of lisp-ish.
I actually much prefer modern JS to modern python. JS is flexible enough to allow a functional-ish style, which greatly improves code clarity in my opinion. And of course you can still write python-style code if you want to. Python is a lot more opinionated!
Learnt this in Pycon 2014 if I recall correctly.
Are type hints Pythonic? I personally don't think so - to me, they don't match the philosophy of the rest of the language. On the other hand, one could argue that the definition of Pythonic is simply "whatever Guido likes".
I've seen it for other languages, including those two, and denying the term wont deny the concept.
I've seen perl, python, and javascript written like C, java written like perl, etc. Its always a bad idea - each language has strengths in the ability to express concepts to other devs, part in syntax and part in conventions, and swimming upstream against those never helps anyone. (Evolution is great, chaos is not)
None of which says that these terms CAN'T be used in an elitist way, but I dont see that as the primary purpose or use (at least in the circles I'm exposed to)
Meaning, as an exmaple Java and JS aren't known for their constant references and inside jokes relating to Monty Python. That is part of the culture and community around Python.
Saying something is Pythonic instead of saying it is idiomatic, is itself, Pythonic. It is part of the culture grown up around the language. And that's okay.
Edit: Of course I should have read TFA before replying. Of course this is not a Python like language, but a language that compiles to Python. I agree the cute name is confusing, but if the point is to compile to a specific language, saying what that language is is pretty important ;-)
Whenever I write Python I try to keep it 'pythonic' and that mainly means keeping things simple, very explicit, and concise. These are obviously traits that all developers want their code to have, but sometimes the syntactic sugar of a language can make it difficult, I think the Pythonic philosophy provides guardrails against this.
That said, it immediately told me that I wouldn't be interested in Coconut, so I also found the label valuable as a non-Python person.
And I don't see how that's a problem? Every language has a subculture around it, language specific jargon, even inside jokes.
Pythonic stems from the idea of simple ways to do "stuff", and that's pretty much it.
"Pythonic" doesn't just mean "idiomatic". Python has design principles[1] that influence new idioms.
I hate to be negative and I appreciate this may be due to the requirement to make Coconut a superset of Python, but the pattern matching and the partial application look anything but elegant (or Pythonic) to me.
x -> x * 2
It looks much cleaner than the traditional :
lambda x : x * 2
Would a PEP adding this syntax have any chance to pass ?
So where in javascript you would write:
const double = x => x * 2
In python you can simply write: from operator import mul
double = mul(2)
[1]: https://docs.python.org/2/library/operator.html def double(x): return x * 2
Python's `lambda` construct creates anonymous functions - its limitations mean it should only be used when anonymity is required. If a function is to be given a name, there is no reason to use a lambda over standard function syntax. from functools import partial
import operator
double = partial(operator.mul, 2)
And at that point I think it would way simpler to just define a normal function.I'm a big fan of the way Haskell manages operators and partial application.
double :: Num a => a -> a
double = (2 *)foo = a -> b -> a+2*b
It's pretty lispy language.
Python wants to be broadly good enough for everyone, not perfect for any one person's specific domain and expertise level. I think this might be the right attitude.
You docs should show it, not tell it.
[0] https://xon.sh/ [1] https://github.com/xonsh/xonsh/issues/1336
Really really curious, what are those reasons?. Not being sarcastic, just want to know.
Are there performance implications?
Overall, for the majority of use cases I have found Coconut code to be approximately as performant as Python code written by hand. The compiler speed can leave a little to be desired though.
This seems like a cool syntactic layer for people that are already writing Python and wishing for a more functional style. However to call this a functional language would betray the definition.
But I can't help but wonder if companies ever go for these kind if extensions / frameworks / languages.
I will play around with this one though!
Should the order of the second list be reversed?
i.e. |> corresponds to f(args) and |> corresponds to f(args)
http://docs.hylang.org/en/stable/
"hy - A dialect of Lisp that's embedded in Python"