News for hackers. Stuff that matters.
Re:Guido on Python by spinningarrow on Friday July 31, 2015 @04:31AM (#9980587)
"If you were to design a new language today, he said, you would make it without mutable (changeable) objects, or with limited mutability."
Re:Guido on Python by marcosdumay on Friday July 31, 2015 @11:13AM (#9982250)
Let's just keep in mind that the entire reason languages with immutable data are trending is because we already have all those languages with mutable data to use where they are fit.

A new language today shouldn't really implement Python metaprogramming, because Python already does that.

Re:Guido on Python by amyjess on Friday July 31, 2015 @05:01PM (#9984473)
Yes, and I think the reason why Nim got so much attention was because it filled the niche of metaprogramming in a statically-typed, AOT language, which is probably the last frontier of mutability and metaprogramming. It's probably the last major new language with metaprogramming front and center we'll see for a very long time.

Honestly, I don't think I'll ever create my own general-purpose language precisely _because_ I already know languages that are perfectly tailored to my use. If I start thinking of "what would my dream language look like", I start mentally re-creating Python. And sometimes, I might be in a different mood, and I'll think "wait, I'll make a statically-typed, AOT version of Python that I can use as a systems programming language instead of C/C++", and I end up mentally re-creating Nim, even to the point of going "well, I'll borrow the static typing part from Modula-3 because Modula-3 is cool and Python has a lot of Modula-3 in it already...", which is basically what Nim did.

Re:Guido on Python by estava on Friday July 31, 2015 @04:35AM (#9980604)
Indeed. That may mean fewer opportunities for "meta-programming" if by meta-programming we mean mutation of core structures. It could also be a good chance to turn around the default of declaring variables to declaring constants instead. Complex data structures would still have to be mutable by default I guess, but they would be the obvious next target to becoming immutable by default.
Re:Guido on Python by eru on Friday July 31, 2015 @08:25AM (#9981269)
Why would your complex data structures be mutable? Wouldn't immutability pay off especially in complex cases?
Re:Guido on Python by cillian64 on Friday July 31, 2015 @10:32AM (#9981978)
If you make my huge NumPy arrays immutable, I will be very unhappy (unless I am significantly misunderstanding how this everything-is-immutable strategy works).

There might be a lot to be said for rust-style immutable-by-default.

Re:Guido on Python by eru on Sunday August 02, 2015 @08:32PM (#9993108)
First, you'd still have optional mutability. Second, arrays are rather simple data structures, but ones where you often care about performance--small tight loops are common. Compare that to the data structure that eg holding your options and flags for the computation: you don't do much computation on that, but you have lots of invariants that you want to uphold---ie lots of code, but each bit executed roughly once, no tight loops.
Re:Guido on Python by juliangregorian on Friday July 31, 2015 @02:43PM (#9983521)
I assume using some form of copy-on-write.
Re:Guido on Python by DasIch on Friday July 31, 2015 @03:56PM (#9984008)
Algorithms on immutable data structures tend to have very bad worst case peformance.
Re:Guido on Python by zeckalpha on Friday July 31, 2015 @10:45PM (#9985670)
Naïve algorithms on immutable data structurs tend to have very bad worst case performance.
Re:Guido on Python by eru on Sunday August 02, 2015 @08:28PM (#9993097)
And there would always be the optional mutability. Your complex data structures are seldom the ones driving performance.

Ie often you have to get the complex data structures right (lots of logic, not much computation) to set up your computations on the simple ones (eg lots of number crunching on your arrays).

Re:Guido on Python by zeckalpha on Monday August 03, 2015 @10:43PM (#10001345)
Yes, it's easier to reason about correctness in immutability land and add mutability in after the fact as a compiler optimization than to remove mutability for safety as a compiler pass.
Re:Guido on Python by eru on Tuesday August 04, 2015 @02:47AM (#10002013)
Haskell sort-of does this. Laziness can be seen as a very disciplined form of mutability.

(Haskell also allows mutability that is visible to the semantics of the program, that's where the famous monads come in.)

Re:Guido on Python by amyjess on Friday July 31, 2015 @09:54AM (#9981748)
Honestly, I found that kind of disappointing, because one of the things I like most about Python is its metaprogramming capabilities.

I'm honestly not fond of languages like Rust that try to restrict mutability. The only one of the new breed of languages I've found that I really like is Nim.

Re:Guido on Python by ptx on Friday July 31, 2015 @03:17PM (#9983753)
Doesn't Nim go a bit too far in that direction, though? Mutable strings seems a bit dangerous to me – won't you have to copy them all the time to make sure they don't get modified somewhere else?
Re:Guido on Python by bitL on Friday July 31, 2015 @07:43AM (#9981155)
In other words you'd make it slow without the ability to allow it to be fast.
Re:Guido on Python by gphil on Friday July 31, 2015 @08:34AM (#9981307)
> without the ability to allow it to be fast

Practically speaking, Python is still slower than a lot of languages that use predominantly immutable data structures. I think you'd want to go to a systems programming language for raw speed anyway.

Re:Guido on Python by bitL on Friday July 31, 2015 @08:53AM (#9981384)
Imagine immutable dict. How many programs in Python you know would still work?
Re:Guido on Python by mynegation on Friday July 31, 2015 @09:09AM (#9981464)
"Immutable" data structures is rather confusing name. Here it is a synonym for persistent data structure [1], which means that you operate on data structures by creating new "versions" of them, which share a lot of memory with the old versions. Parts of the program that refer to the old version see it as immutable.

[1] https://en.wikipedia.org/wiki/Persistent_data_structure

Re:Guido on Python by tikhonj on Friday July 31, 2015 @02:05PM (#9983282)
Modern radix trees[1] can be as fast as the hash maps you'd find in a language's standard library while providing additional capabilities (ie fast ordered traversal). Making them immutable and persistent[2] is easy and only adds a bit of performance overhead.

Obviously programs that rely on communicating by mutating parts of a dictionary would have to be rewritten to use an immutable, persistent map—but they could still maintain similar performance.

[1]: Namely "adaptive radix trees" as described in “The adaptive radix tree: Artful indexing for main-memory databases” by V. Leis, A. Kemper, and T. Neumann <http://www3.informatik.tu-muenchen.de/~leis/papers/ART.pdf>

[2]: A persistent adaptive radix tree in Java: https://github.com/ankurdave/part

Re:Guido on Python by bitL on Friday July 31, 2015 @02:37PM (#9983473)
Thanks for the radix tree reference!

On the other hand, stating "they could still maintain similar performance" for immutable dictionaries is misleading at best (you are right for dictionary sizes approaching 0). Anyone can look up benchmarks on the Internet showing real-world performance of immutable data structures and make their mind up if they actually can deliver in their particular use cases.

Mutability could be always used to boost performance on Von Neumann architecture if you know what you are doing. It's just becoming impractical due to increased complexity, pervasive security paranoia and the fact that there are very few people that can get things done right. If you want to contribute to advance your case, please create a competing computer architecture as fast as Von Neumann's, physically possible, but based on immutability.

Re:Guido on Python by semi-extrinsic on Friday July 31, 2015 @04:02PM (#9984051)
> Mutability could be always used to boost performance (...)

For the extreme case, see the EQUIVALENCE statement in old Fortran.

Re:Guido on Python by wyldfire on Friday July 31, 2015 @12:29PM (#9982675)
Well, opt-in immutable dict would be awesome. I've found that many times my usage of set and list is more appropriate as their immutable counterparts (frozenset, tuple). Lots of times I create sets or lists with the intention that they be immutable and it would be clearer if I could specify to future me/maintainers/extenders that this should remain as initialized.
Re:Guido on Python by bitL on Friday July 31, 2015 @01:46PM (#9983134)
Re:Guido on Python by bpicolo on Friday July 31, 2015 @11:23PM (#9985781)
Switch to namedtuples for lightweight data structures. No regrets.
Re:Guido on Python by porker on Friday July 31, 2015 @08:01AM (#9981203)
Is that true? It doesn't match many of the comments on HN around React, Om, Clojure etc...
Re:Guido on Python by bitL on Friday July 31, 2015 @08:15AM (#9981243)
Yes, it is slow. Immutability is allowing you to write elegant concurrent algorithms at the expense of speed and memory. Imagine you are forced to never change an array of numbers for an algorithm computing matrices, rather creating new matrices for each step and specifying numbers only during initialization. But hey, you can cache them and keep track of every single copy without any confusion now! Now imagine a non-concurrent language like JavaScript - what exactly are you going to get by using immutable structures except for the feeling you are going with the times?

Reference counting and caching are probably the only areas where immutability can help for most programs.

Immutability is in fashion nowadays due to functional programming being in fashion despite the inability to ever achieve "pure" functional programming in reality - I/O is by default "dirty" (I really hate these charged words in computing), no amount of flatmapping (i.e. monads) is going to help with that.

Re:Guido on Python by mtanski on Friday July 31, 2015 @03:05PM (#9983662)
Everything immutable (every data structure) is a really convenient way of thinking about processing data in the abstract sense. You can think about processing your (semi) structure data (records) in terms of high level pipelines and processing steps. And, for a whole class of problems this works well.

Certainly this abstraction can stretch pretty well. Esp. when your language / framework can optimize immutable data structures under the covers (mutable and reuse.) But the abstraction is just that an abstraction for a mutable platform.

The platforms of today (VMs, operating systems, hardware, storage) are not immutable and notoriously full of side effects and undefined behavior. So these immutable language / frameworks end up being good tools at one extreme (high level data processing), bad at the other (system level languages) and vary on a case by case basis.

In today's todays platforms if you want to allocate an object in your language think of the layers and layers of side effects that will happen. 1. The allocator/GC system allocates it from some kind of pool possibly. Lots of side effects there (like operating on a pool, triggering some kind of collection / cleanup, requesting a large mapping from the OS. 2. Then at the OS level accessing mapped memory has all sorts of side effect (paging, lazy initialization, etc). 3. At the hardware levels there's lots of side effects: instruction cache, multiple layers of data caches, TLB, DMA.

In conclusion, for some kinds of problems you don't need to worry about these details and for others you can't afford to ignore them.

Re:Guido on Python by porker on Friday July 31, 2015 @08:29AM (#9981285)
Thanks, that's a great answer and helps me understand.

I guess there are subsets of problems where immutability is a good idea (anything needing state, 'Undo' capabilities), neutral, and a bad idea (number crunching, data processing, 'traditional'(??) programming).

I have been wowed by the beauty of the immutable approach in web apps, but definitely subscribe to the right technique for a task. I can't imagine doing the data processing in physics using an immutable language...

Re:Guido on Python by bitL on Friday July 31, 2015 @08:44AM (#9981342)
Indeed it boils down to what you need to achieve. You might want to use both approaches and combine them for best performance/readability.

There are specialized high-performance structures that are mutable (e.g. disruptor in HFT trading) optimizing all the way down to CPU cache lines to achieve unbelievable speeds, yet they work nicely if the objects they refer to are immutable.

So just take all advices to switch to immutability with a grain of salt and learn to figure out which approach is suitable for what part of your system to have it both performing well and being able to conceptualize and maintain the code nicely ;-)

Re:Guido on Python by rakoo on Friday July 31, 2015 @11:38AM (#9982394)
> Reference counting and caching are probably the only areas where immutability can help for most programs.

There is another use spurred by React: when you have a new state to represent, you have to diff it against current state to know exactly what changed and work only on this diff. "Immutable" structures make this trivial.