Clojure vs. D in creating immutable lists that are almost the same.

QAston via Digitalmars-d digitalmars-d at puremagic.com
Sun Feb 28 10:06:45 PST 2016


On Saturday, 27 February 2016 at 22:31:28 UTC, Brother Bill wrote:
> Clojure supports immutable lists that allow adding and removing 
> elements, and yet still have excellent performance.
>
> For D language, what are the recommended techniques to use 
> functional programming, without massive copying of data and 
> garbage collection, so that it remains immutable.
>
> That is, how to create one-off changes to an immutable data 
> structure, while keeping the original immutable, as well as the 
> one-off change, and maintain good performance.
>
> Thank you

As a user of Clojure i can tell that "excellent" is an 
overstatement. Still persistent data structures are much better 
than naive copy (unless you really, REALLY need all data in 
cache) and copy on write (unless you only very rarely modify an 
object).

That said, I've seen nobody programming with all-immutable 
objects in D. People mostly use immutable objects only for 
objects never modified, and use const as an immutable view of a 
modifiable object. No clojure-style applying method calls with 
reduce.

With some metaprogramming you could probably write update-in 
function which would work with nested class objects. With that 
and having your classes return new object instead of modifying 
existing one you could have a somewhat clojurish experience.

For cool stuff like records clojure uses persistent maps, which 
would be much less convenient to use because of static typing.

I know no implementation of persistent data structures (hell, 
phobos lacks even some regular data structures). I'm implementing 
transducers for D though, which could be useful if someone 
implemented the datastructures.


More information about the Digitalmars-d mailing list